Skip to content

Incorrect values returned by Real.fromString on strings with many digits #360

Description

@shwestrick

Version

110.99.8 (Latest)

Operating System

  • Any
  • Linux
  • macOS
  • Windows
  • Other Unix

OS Version

macOS Sequoia 15.5

Processor

  • Any
  • Arm (using Rosetta)
  • PowerPC
  • Sparc
  • x86 (32-bit)
  • x86-64 (64-bit)
  • Other

System Component

Basis Library

Severity

Major

Description

Real.fromString returns incorrect values. I've found a few examples, see below.

My best guess at the moment: the bug seems to occur when there are 18 or more decimal digits past the decimal point. All of the examples below have 18 decimal digits. Increasing the number of decimal digits even further causes the outputs to change.

Transcript

A few examples of incorrect outputs from Real.fromString:

Standard ML of New Jersey [Version 110.99.8; 64-bit; April 25, 2025]
- Real.fromString "0.501782303180000055";
val it = SOME 502.812494993 : real option
- Real.fromString "0.211111111111111111";
val it = SOME 9.46111993270 : real option
- Real.fromString "0.311111111111111111";
val it = SOME 418.928139547 : real option
- Real.fromString "0.411111111111111111";
val it = SOME 316.528139547 : real option

Expected Behavior

Not sure about exact values (this will depend on printing conventions), but the above are clearly wrong.

FWIW, here is output from MLton:

$ cat foo.sml
fun dump x =
  print (Real.fmt StringCvt.EXACT (valOf (Real.fromString x)) ^ "\n")
val () = dump "0.501782303180000055"
val () = dump "0.211111111111111111"
val () = dump "0.311111111111111111"
val () = dump "0.411111111111111111"

$ mlton foo.sml

$ ./foo
0.50178230318
0.2111111111111111
0.3111111111111111
0.4111111111111111

Steps to Reproduce

See above.

Additional Information

No response

Email address

github username at gmail

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

basis-libproblem with Standard ML Basis librarybugSomething isn't workingfixed-in-110.99.9issues fixed in the 110.99.9 releasefloating-pointproblem related to floating-point operations

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions