code not true?
John W. Baxter
jwbnews at scandaroon.com
Sat Apr 22 16:38:35 EDT 2000
In article <Pine.GSO.4.10.10004221525010.8110-100000 at sundial>, Moshe
Zadka <mzadka at geocities.com> wrote:
> On Sat, 22 Apr 2000 jeff_islay at my-deja.com wrote:
> > Why does the second example return false?
> > >>> print 7.0 == 7
> > 1
> > >>> print (.07 * 100) == 7
> > 0
> Because computers represent floats with too little bits.
> Never ever compare two floats for equality!
Not quite. No number of bits is sufficient to represent .07 exactly in
the typical (and IEEE standard) representation.
Various alternate representations are possible which are exact (such as
BCD floating point of sufficient precision, rational numbers with
(potentially) Python longs as numerator and/or denominator, some
interesting recent work I haven't followed, etc etc etc).
John W. Baxter Port Ludlow, WA USA jwbnews at scandaroon.com
More information about the Python-list