Version française
Home     About     Download     Resources     Contact us    

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

Browse thread
[Caml-list] Printing floating point numbers & precision
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-09-30 (16:54)
From: mff <mff@r...>
Subject: [Caml-list] Printing floating point numbers & precision


I have a question regarding the printing of floating point numbers.  
Here's an example:

lt-mff: /usr/bin/ocaml
        Objective Caml version 3.07+2
# max_float;;
- : float = 1.79769313486231571e+30

# Printf.sprintf "%e" max_float;;
- : string = "1.797693e+308"

So using the default %e format to print a floating point number loses 11 
of precision.    I could be more precise about the number of fractional 

# Printf.sprintf "%.17e" max_float;;
- : string = "1.79769313486231571e+308"

But this does not seem to work in general:

# Printf.sprintf "%.17e" 12345.1;;
- : string = "1.23451000000000004e+04"

I've combed the documentation to try to understand the relationship between
printing and precision (and accuracy!) but have come up empty handed.

Help is appreciated!
Thanks, Mary

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: