caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Printing floating point numbers & precision
@ 2004-09-30 16:52 mff
  2004-09-30 19:25 ` Remi Vanicat
  0 siblings, 1 reply; 4+ messages in thread
From: mff @ 2004-09-30 16:52 UTC (permalink / raw)
  To: caml-list


Greetings,

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 
digits
of precision.    I could be more precise about the number of fractional 
digits:

# 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 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Printing floating point numbers & precision
  2004-09-30 16:52 [Caml-list] Printing floating point numbers & precision mff
@ 2004-09-30 19:25 ` Remi Vanicat
  2004-10-01  7:56   ` Christian Lindig
  0 siblings, 1 reply; 4+ messages in thread
From: Remi Vanicat @ 2004-09-30 19:25 UTC (permalink / raw)
  To: caml-list

On Thu, 30 Sep 2004 12:52:00 -0400, mff <mff@research.att.com> wrote:
> 
> Greetings,
> 
> 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 digits
> of precision.    I could be more precise about the number of fractional digits:
> 
> # 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.

This is not a ocaml problem, this a problem with base 2 represention
of decimal number : 1/10 is not representable exactly using standard
ieee number, the more exact precicsion you could have is 0.100000004
(with may be some more 0 between the 1 and the 4).

By the way it is a FAQ, and you could find dicsution about it int hte
archive of this very mailing-list.

-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Printing floating point numbers & precision
  2004-09-30 19:25 ` Remi Vanicat
@ 2004-10-01  7:56   ` Christian Lindig
  0 siblings, 0 replies; 4+ messages in thread
From: Christian Lindig @ 2004-10-01  7:56 UTC (permalink / raw)
  To: Remi Vanicat; +Cc: caml-list


On Sep 30, 2004, at 9:25 PM, Remi Vanicat wrote:

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

A very good source for understanding printing and reading of 
floating-point numbers are these two articles; they appeared in the 
same PLDI '90 proceedings.

G. L. Steele Jr. and J. L. White. How to print floating-point numbers 
accurately. In Proceedings of the ACM SIGPLAN '90 Conference on 
Programming Language Design and Implementation, pages 112--126, White 
Plains, New York, June 1990.

W. D. Clinger, How to read floating point numbers accurately, 
Proceedings of the {ACM} {SIGPLAN} '90 Conference on Programming 
Language Design and Implementation, SIGPLAN Notices, pages 92--101, 
White Plains, New York, June 1990.

-- Christian

-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Caml-list] Printing floating point numbers & precision
@ 2004-09-30 16:53 mff
  0 siblings, 0 replies; 4+ messages in thread
From: mff @ 2004-09-30 16:53 UTC (permalink / raw)
  To: caml-list, mff


Greetings,

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 
digits
of precision.    I could be more precise about the number of fractional 
digits:

# 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 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-10-01  7:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-30 16:52 [Caml-list] Printing floating point numbers & precision mff
2004-09-30 19:25 ` Remi Vanicat
2004-10-01  7:56   ` Christian Lindig
2004-09-30 16:53 mff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).