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 loosing precision #3352

Closed
vicuna opened this issue May 15, 2002 · 3 comments
Closed

string_of_float loosing precision #3352

vicuna opened this issue May 15, 2002 · 3 comments

Comments

@vicuna
Copy link

vicuna commented May 15, 2002

Original bug ID: 1154
Reporter: administrator
Status: resolved (set by @damiendoligez on 2012-01-20T13:26:27Z)
Resolution: suspended
Priority: normal
Severity: feature
Category: ~DO NOT USE (was: OCaml general)
Related to: #8279 #4345
Monitored by: @dbuenzli @Chris00

Bug description

Full_Name: mattias waldau
Version: 3.04
OS: linux
Submission from: h175n2fls34o849.telia.com (217.208.235.175)

It seems that string_of_float is only working with float internally,
and not using double.

string_of_float (86928280876. +. 0.9513)

results in

86928280877

@vicuna
Copy link
Author

vicuna commented Jun 27, 2002

Comment author: administrator

string_of_float is just printf "%.12g", hence limited precision. An "exact"
conversion would be nice but isn't provided by the C standard library.

@vicuna
Copy link
Author

vicuna commented Jan 20, 2012

Comment author: @damiendoligez

At this point, string_of_float is a quick-and-dirty function. If you need more precision, you should use printf and specify the number of digits.

Maybe someday, someone will implement [Clinger1990] and [Steele1990] for OCaml and submit a patch, but don't hold your breath.

@inproceedings{Clinger1990,
author = {Clinger, William D.},
title = {How to read floating point numbers accurately},
booktitle = {Proceedings of the ACM SIGPLAN 1990 conference on Programming language design and implementation},
series = {PLDI '90},
year = {1990},
isbn = {0-89791-364-7},
location = {White Plains, New York, United States},
pages = {92--101},
numpages = {10},
url = {http://doi.acm.org/10.1145/93542.93557},
doi = {http://doi.acm.org/10.1145/93542.93557},
acmid = {93557},
publisher = {ACM},
address = {New York, NY, USA},
}

@inproceedings{Steele1990,
author = {Steele,Jr., Guy L. and White, Jon L.},
title = {How to print floating-point numbers accurately},
booktitle = {Proceedings of the ACM SIGPLAN 1990 conference on Programming language design and implementation},
series = {PLDI '90},
year = {1990},
isbn = {0-89791-364-7},
location = {White Plains, New York, United States},
pages = {112--126},
numpages = {15},
url = {http://doi.acm.org/10.1145/93542.93559},
doi = {http://doi.acm.org/10.1145/93542.93559},
acmid = {93559},
publisher = {ACM},
address = {New York, NY, USA},
}

@vicuna vicuna closed this as completed Jan 20, 2012
@vicuna
Copy link
Author

vicuna commented May 4, 2012

Comment author: @dbuenzli

Somehow I still think it would be nice if :

(float_of_string (string_of_float i)) = i

could be true for all the integers i that can be represented exactly in OCaml's float value. These integers are in the interval [-2^53;2^53]. This could be achieved by using "%.16g", i.e. is true for :

(float_of_string (Printf.sprintf "%.16g" i))

Best,

Daniel

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

No branches or pull requests

1 participant