Thursday 6 March 2014

Re: Changing default CFLAGS on i386

Ryan Lortie [2014-03-06 16:06 -0500]:
> double a, b;
>
> a = get_value ();
> b = get_value ();
>
> if (a != b)

I know it's a bit tangential, but isn't it taught in like every book
and tutorial that one should never compare floating point numbers for
(in)equality? That's why e. g. Python has assertAlmostEqual(), gtest
has EXPECT_FLOAT_EQ, etc. Unfortunately glib has g_assert_cmpfloat()
which is broken (it doesn't test up to some precision if you use it
with == or !=).

I think code which expects this is just broken. It's never going to
work reliably across architectures, compilers, or implicit type
conversion. So even if we'd fine-tune our compiler to cover this
particular case, it may only falsely lead people into thinking that
this kind of code is okay.

floats/doubles are *not* rational numbers (which don't have rounding
errors and are always precise), they are what is usually called "real"
numbers in maths). There is not even a mathematical definition of
equality for real numbers, so it shouldn't be a concept in programming
either.

Martin

--
Martin Pitt | http://www.piware.de
Ubuntu Developer (www.ubuntu.com) | Debian Developer (www.debian.org)

--
ubuntu-devel mailing list
ubuntu-devel@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-devel