ironpython3 icon indicating copy to clipboard operation
ironpython3 copied to clipboard

Unexpected output precision and/or rounding results in IronPython 3.4.0

Open AlGerngro opened this issue 3 years ago • 6 comments

Description

print - whether in conjunction with round or not - does not yield the expected output of decimal values as in 2.7

Steps to Reproduce

While I first observed the behavior in some breaking Unit tests while porting my .NET application to IronPython 3.4.0 using the new nupkg, a simple console example should do:

IronPython 3.4.0 (3.4.0.1000)
[.NETFramework,Version=v4.6.2 on .NET Framework 4.8.4515.0 (64-bit)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print(685.86)
685.86000000000001
>>> f = float(685.86)
>>> print(f)
685.86000000000001
>>> print(round(f,2))
685.86000000000001
>>> print(round(f))
686
>>> print(round(f,1))
685.89999999999998
>>>

Therefore, I'd think of an output rather than a numeric issue.

Expected behavior: While I am aware that not every decimal number can be rendered in full fidelity, I would expect the same output as was the input argument; or in case of round the output being truncated to the specified number of decimal places. This was the case in IronPython 2.7.

Actual behavior: Although the result may be correct from a numeric point of view, the numbers always seem to be output with the same number of decimal places, which is never the one provided as an argument. Especially, rounding to 1 digit should not give me 14 digits.

Versions

  • 3.4.0 Release

If this is indeed expected, then I am not sure how to produce a normalized output.

AlGerngro avatar Dec 23 '22 10:12 AlGerngro

Python 3 changed the str function of numbers to be equivalent to repr and so the values you're seeing is the repr for those numbers in IronPython. With IronPython 2 str was essentially applying %.12g formatting. You could do something like to get a similar representation as IronPython 2 (note that it won't match the trailing .0 if your float is an integer):

def to_str(val):
    return "%.12g" % val

Note that there is an outstanding task to try and match the shortest representation used by CPython: https://github.com/IronLanguages/ironpython2/issues/102.

slozier avatar Dec 23 '22 17:12 slozier

We hit this when moving an application from IronPython 2.6 to 3.4.2 on .NET Framework 4.8.1 (net462 build). Every float that a script prints or formats comes out with 17 significant digits:

>>> repr(0.1), str(1/3), '{}'.format(0.2), repr(1e16), repr(1e-05)
('0.10000000000000001', '0.33333333333333331', '0.20000000000000001', '10000000000000000.0', '1.0000000000000001e-05')

CPython 3 gives ('0.1', '0.3333333333333333', '0.2', '1e+16', '1e-05').

Cause. FloatOps.Repr formats with "%.17g" and only falls back to ToString("R") when that text does not round-trip. On .NET Framework, 17 digits always round-trip, so the fallback never runs. The layout also differs from CPython, which switches to exponent notation when the decimal point lies before -4 or after 16 digits (repr(1e16)).

What we ship for now (patch against v3.4.2, happy to turn it into a PR against main):

  • Digits come from exact BigInteger arithmetic instead of ToString("E15"/"E16") and double.Parse. On .NET Framework those round twice and are not always correctly rounded.
  • Fast path: for normal numbers the correctly rounded 15-digit text ("E14", rounded once) is checked against the exact rounding interval of the double. If it lies inside, it is the shortest text padded with zeros (DBL_DIG). Otherwise the search continues at 16 and 17 digits. Subnormals search from one digit.
  • Round-half-even bounds and the lopsided interval just below a power of two are handled as in CPython's dtoa.
  • CPython's float_repr_style 'short' layout.

Verified against 2,025 values from CPython 3.14 (random bit patterns plus edge cases, including 5e-324, 2.2250738585072014e-308, 1.7976931348623157e308, 1e22, 1e23, 0.1 + 0.2): repr, str, '{}'.format and '%s' % are all identical. Cost on .NET Framework 4.8.1: about 3.3 µs per repr against 1.75 µs before, for a mix of three-decimal values and random.random() values.

If you would take the change, I can open a PR with the patch and the reference test. Should the netcore targets keep the current "R"-based path there?

pm-jd avatar Oct 22 '26 04:10 pm-jd

Hello, @slozier,

We have encountered the unexpected floating-point number representation described in this issue in our .NET 10 application using IronPython 3.4.2. A few years ago, you mentioned issue #102, but it seems that it is still not resolved.

On the other hand, @pm-jd mentioned in this discussion some changes available on a patch branch. Would it be possible to publish these changes to NuGet.org?

Thanks in advance!

ABialchytski avatar Oct 30 '26 16:10 ABialchytski

Thanks for the suggestion. I'm in the process of porting an implementation of the Schubfach algorithm (from C++) which will hopefully resolve this one and for all.

slozier avatar Oct 31 '26 14:10 slozier

Hello, @slozier, i would like to kindly ask you: are there plans to release new nuget package having fix of these unexpected representation results?

ABialchytski avatar Nov 10 '26 09:11 ABialchytski

Hello, @slozier, i would like to kindly ask you: are there plans to release new nuget package having fix of these unexpected representation results?

Probably some time in the next few weeks.

slozier avatar Nov 11 '26 01:11 slozier