Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string_of_float is 1e9 more precise in 3.07beta1 versus 2.06 #8230

Closed
vicuna opened this issue Jul 25, 2003 · 2 comments
Closed

string_of_float is 1e9 more precise in 3.07beta1 versus 2.06 #8230

vicuna opened this issue Jul 25, 2003 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Jul 25, 2003

Original bug ID: 1778
Reporter: administrator
Status: closed
Resolution: fixed
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

String_of_float is now %.17g versus the old %.12g. You might want to
note this behavior in the Changelog. (My log file is lots bigger now
that every number is 19 characters wide versus the previous 4 or 5.)

    Objective Caml version 3.06

string_of_float 0.14000000000000001;;

  • : string = "0.14"

string_of_float 0.1400000000001;;

  • : string = "0.14"

string_of_float 0.140000000001;;

  • : string = "0.140000000001"

0.14000000000000001 = 0.14 ;;

  • : bool = true

      Objective Caml version 3.07+beta 1
    

string_of_float 0.14000000000000001;;

  • : string = "0.14000000000000001"

string_of_float 0.1400000000000000000000000000000001;;

  • : string = "0.14000000000000001"

0.14000000000000001 = 0.14 ;;

  • : bool = true
@vicuna
Copy link
Author

vicuna commented Jul 29, 2003

Comment author: administrator

String_of_float is now %.17g versus the old %.12g. You might want to
note this behavior in the Changelog. (My log file is lots bigger now
that every number is 19 characters wide versus the previous 4 or 5.)

Right. That wasn't a great idea.

The problem we tried to address is as follows: the toplevel prints
floats using string_of_float, and several unsophisticated users don't
understand that the printed value of a float may not have as many
digits as the machine representation, so two floats can print the same
and be different. (I guess they never used a pocket calculator...)

Using %.17g in string_of_float doesn't really solve this issue,
because there are many cases where we get too many digits in the
resulting string (fewer digits would result in the same float). This
depends a lot on the C library implementation of printf, but
apparently many get it wrong and don't implement the "print enough
digits so that the float can be read back identically" rule.

A better approximation of this rule is to convert floats to strings
with increasing precision, until the strings read back as the original
floats. This works quite well, but is computationally expensive --
probably too much for string_of_float.

What we'll do is decouple string_of_float and toplevel printing of
floats. The former will revert to %.12g as in 3.06, while the latter
will use the "increasing precision" approach, since toplevel printing
isn't performance-critical.

Thanks for your feedback,

  • Xavier Leroy

@vicuna
Copy link
Author

vicuna commented Jul 29, 2003

Comment author: administrator

Fixed 2003-07-29 by XL

@vicuna vicuna closed this as completed Jul 29, 2003
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant