Discrepency between JPython and CPython

Tim Peters tim_one at email.msn.com
Sun Apr 30 21:48:05 EDT 2000


[Venkatesh Prasad Ranganath]
> Consider the expression str(1.14 * 200).
> In CPython it yields 228.0
> In JPython it yields 227.99999999999997
>
> Shouldn't this result in consistent values across variuos
> implementation?

The values computed are identical; it's only the choice of display format
that differs.  Under 1.6a2 CPython,

>>> repr(1.14 * 200)
'227.99999999999997'
>>> str(1.14 * 200)
'228.0'
>>>

> If so, which of the implementation is buggy?
> I think it is JPython, may be I am wrong.

It's fuzzy, really.  Java rigidly defines what float->string conversion
does, and JPython appears to play along with Java's rules.  In 1.5.2
CPython, repr(float) and str(float) are identical, and don't follow any
comprehensible rules (they inherit random platform libc decisions).  In 1.6
CPython, str(float) is the same as in 1.5.2, but repr(float) produces enough
digits so that float(repr(x)) == x for all finite x on any IEEE-754
conformant platform (but not necessarily the *same* string across all 754
platforms).

If you're starting to get the feeling this is more involved than you think,
you're underestimating your confusion <wink>.  Over time, I expect the
JPython rules will gain favor, as they're the only ones on the table that
actually define the result precisely across platforms.

if-you-want-exactness-stick-to-integers-ly y'rs  - tim






More information about the Python-list mailing list