You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The reason that these numbers are rounded down, not up like Python 2 generally should do, is that they cannot be represented exactly and, for example, 1.15 is actually '1.149999999999999911182158029987476766109466552734375 which is closer to 1.1 than 1.2. This issue with representing numbers is due to how floats are represented internally in binary format and affects IronPython exactly like it affects CPython. With IronPython round works differently, though. Using IronPython 2.7.9rc1:
Not sure could this issue cause any real problems with real code. The latter example above is, however, directly against IronPython documentation for round (which seems to be directly copied from CPython).
Notice also that it's not only exactly these exact numbers that are surprisingly rounded up with IronPython:
The underlying cause for the rounding issue is the rounding algorithm which essentially does round(1.1499999999999999 * 10) / 10. Since 1.1499999999999999 * 10 == 11.5 (and 11.5 is exactly representable), the midpoint rounding makes it go up to 12.
It looks like CPython actually uses double to string (via dtoa) conversion to properly do its rounding. There is fallback code in there which is used when dtoa isn't available which does the exact same rounding as we have.
Probably not worth spending any more time on this issue since as mentioned it probably doesn't cause any issues with real code.
Using Python 2.7:
The reason that these numbers are rounded down, not up like Python 2 generally should do, is that they cannot be represented exactly and, for example,
1.15
is actually'1.149999999999999911182158029987476766109466552734375
which is closer to1.1
than1.2
. This issue with representing numbers is due to how floats are represented internally in binary format and affects IronPython exactly like it affects CPython. With IronPythonround
works differently, though. Using IronPython 2.7.9rc1:Not sure could this issue cause any real problems with real code. The latter example above is, however, directly against IronPython documentation for round (which seems to be directly copied from CPython).
Notice also that it's not only exactly these exact numbers that are surprisingly rounded up with IronPython:
The text was updated successfully, but these errors were encountered: