how to use abs() in C++

10 views
Skip to first unread message

Ondrej Certik

unread,
Jan 26, 2011, 1:30:56 AM1/26/11
to hermes2d
Hi,

this is a very common mistake, so I wanted to share my current
understanding with you (as I am learning this myelf):

One should *always* use std::abs().

In particular, one should *never* use abs(), because that returns an
integer. So for example this patch:

https://github.com/hpfem/hermes/commit/1faa5c5f0576b9440c299648e7aca62ad935cc5b

in my opinion doesn't work. I have already fixed this for the tests 50
and 52 and it should be fixed for all the other tests.

Ondrej

David Pugal

unread,
Jan 26, 2011, 11:21:04 AM1/26/11
to herm...@googlegroups.com
Hi,

And just a note. If std:: is not used, then fabs() should be always used for doubles.
I've learned it hard way...

David


--
You received this message because you are subscribed to the Google Groups "hermes2d" group.
To post to this group, send email to herm...@googlegroups.com.
To unsubscribe from this group, send email to hermes2d+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hermes2d?hl=en.


Ondrej Certik

unread,
Jan 26, 2011, 12:16:04 PM1/26/11
to herm...@googlegroups.com
On Wed, Jan 26, 2011 at 8:21 AM, David Pugal <david...@gmail.com> wrote:
> Hi,
> And just a note. If std:: is not used, then fabs() should be always used for
> doubles.
> I've learned it hard way...

The problem with fabs() is that it is defined in C, not C++. It works
in g++, but not in MSVC (at least Ivo said so).

So it seems that std::abs() is the only option.

Ondrej

Kristjan Robam

unread,
Mar 29, 2020, 12:26:34 PM3/29/20
to hermes2d
Hello David(Deivid)

You could have used replace in text editor, not typing everything in character by character.
Or if you used delete and paste, then replace would be again better.

Just a friendly advice.

Kristjan Robam

Tel: 372 6861327

kolmapäev, 26. jaanuar 2011 18:21.04 UTC+2 kirjutas David:
Hi,

And just a note. If std:: is not used, then fabs() should be always used for doubles.
I've learned it hard way...

David
On Tue, Jan 25, 2011 at 10:30 PM, Ondrej Certik <ond...@certik.cz> wrote:
Hi,

this is a very common mistake, so I wanted to share my current
understanding with you (as I am learning this myelf):

One should *always* use std::abs().

In particular, one should *never* use abs(), because that returns an
integer. So for example this patch:

https://github.com/hpfem/hermes/commit/1faa5c5f0576b9440c299648e7aca62ad935cc5b

in my opinion doesn't work. I have already fixed this for the tests 50
and 52 and it should be fixed for all the other tests.

Ondrej

--
You received this message because you are subscribed to the Google Groups "hermes2d" group.
To post to this group, send email to herm...@googlegroups.com.
To unsubscribe from this group, send email to herm...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages