Home     About     Download     Resources     Contact us

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Browse thread
[Caml-list] float pretty-printing precision, once more.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: Damien Doligez Subject: Re: [Caml-list] float pretty-printing precision, once more.
```On Tuesday, Dec 10, 2002, at 00:04 Europe/Paris,
jeanmarc.eber@lexifi.com wrote:

> caml 3.06+1:
[string_of_float loses precision on floating-point numbers]

In the current working version (3.06+18), the precision used by
string_of_float has been increased to 17 digits.  I *think* this
is enough to represent all the double-precision floating-point
numbers:

Objective Caml version 3.06+18 (2002-11-07)

# let f = 1. /. 86400.;;
val f : float = 1.1574074074074073e-05
# let s = string_of_float f;;
val s : string = "1.1574074074074073e-05"
# let f1 = float_of_string s;;
val f1 : float = 1.1574074074074073e-05
# f1 = f;;
- : bool = true
# f1 -. f;;
- : float = 0.

However, it has the unfortunate side-effect of revealing this
awful truth about FP numbers: many "interesting" numbers are
impossible to represent in floating-point.  For example:

# 0.1;;
- : float = 0.10000000000000001

There is no floating-point number equal to 0.1 and the
best approximation you can get is 0.10000000000000001.
I expect this will quickly become a much-used FAQ entry...

I don't know if we will keep this change for the next release
of O'Caml.  But you can always use (Printf.sprintf "%.17g")
instead of string_of_float, and you'll get all the digits,
significant or not.

-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

```