From: Wolfram Hinderer on
On 8 Jul., 15:10, Ethan Furman <et...(a)stoneleaf.us> wrote:

> Interesting.  I knew when I posted my above comment that I was ignoring
> such situations.  I cannot comment on the code itself as I am unaware of
> the algorithm, and haven't studied numbers extensively (although I do
> find them very interesting).
>
> So while you've made your point, I believe mine still stands --
> comparing floats using == to absolute numbers is almost always a mistake.

JFTR, it works because a+b == a+b (while I don't think that a+b == b+a
holds for all a and b).

In general, I totally agree with you.
From: Paul Rubin on
Wolfram Hinderer <wolfram.hinderer(a)googlemail.com> writes:
> JFTR, it works because a+b == a+b (while I don't think that a+b == b+a
> holds for all a and b).

I'm pretty sure IEEE 754 addition is always commutative (except maybe in
the presence of infinity or NaN and maybe not even then). It differs from
rational or real-number arithmetic in that it is not always associative.
You can have (a+b)+c != a+(b+c).
From: Mark Dickinson on
On Jul 8, 9:52 pm, Wolfram Hinderer <wolfram.hinde...(a)googlemail.com>
wrote:
> JFTR, it works because a+b == a+b (while I don't think that a+b == b+a
> holds for all a and b).

Actually, that's one of the few identities that's safe. Well, for non-
NaN IEEE 754 floating-point, at any rate. And assuming that there's
no use of extended precision to compute intermediate results (in which
case one side could conceivably be computed with different precision
from the other; but that applies to a+b == a+b, too). And assuming
that no FPE signals occur and halt the program... (e.g., for overflow,
or from doing -inf + inf). Okay, maybe not that safe, then. :)

For NaNs, there are two problems: first, if exactly one of a and b is
a NaN then a+b and b+a will both be NaNs, so a + b == b + a will be
false, even though they're identical. Second, if a and b are *both*
NaNs, and you're feeling really fussy and care about signs and
payloads, then a + b and b + a won't even necessarily be bitwise
identical: a + b will have the sign and payload of a, while b + a has
the sign and payload of b. You can see something similar with the
Decimal module:

>>> Decimal('NaN123') + Decimal('-NaN456')
Decimal('NaN123')
>>> Decimal('-NaN456') + Decimal('NaN123')
Decimal('-NaN456')

Or more directly (just for fun), using the struct module to create
particular nans:

>>> import struct
>>> x = struct.unpack('<d', '\xff\xff\xff\xff\xff\xff\xff\xff')[0]
>>> y = struct.unpack('<d', '\xfe\xff\xff\xff\xff\xff\xff\xff')[0]
>>> x
nan
>>> y
nan
>>> struct.pack('<d', x+y) # identical to x
'\xff\xff\xff\xff\xff\xff\xff\xff'
>>> struct.pack('<d', y+x) # identical to y
'\xfe\xff\xff\xff\xff\xff\xff\xff'

Isn't floating-point a wonderful thing. :)

--
Mark


--
Mark
From: Tim Roberts on
David Mainzer <dm(a)tu-clausthal.de> wrote:
>
>All your comments helped me and now i know how it works in python !

This has nothing to do with Python. What you're seeing is the way IEEE-754
floating point arithmetic works. You'd see these same results in any
programming language, including assembly language (assuming it hasn't
changed the rounding mode).
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.
From: Aahz on
In article <mailman.426.1278606698.1673.python-list(a)python.org>,
Chris Rebert <clp2(a)rebertia.com> wrote:
>On Thu, Jul 8, 2010 at 8:52 AM, Giacomo Boffi <giacomo.boffi(a)polimi.it> wrote:
>> "Zooko O'Whielacronx" <zooko(a)zooko.com> writes:
>>>
>>> I'm starting to think that one should use Decimals by default and
>>> reserve floats for special cases.
>>
>> would you kindly lend me your Decimals ruler? i need to measure the
>> sides of the triangle whose area i have to compute
>
>If your ruler doesn't have a [second] set of marks for centimeters and
>millimeters, that's really one cheap/cruddy ruler you're using.

Unless I'm badly mistaken, Giacomo was making a funny about Greek
geometers.
--
Aahz (aahz(a)pythoncraft.com) <*> http://www.pythoncraft.com/

"....Normal is what cuts off your sixth finger and your tail..." --Siobhan