Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004345OCamlCamlp4public2007-07-17 11:462013-08-31 12:44
Reporterbrunodefraine 
Assigned Toxclerc 
PrioritynormalSeverityminorReproducibilityalways
StatusclosedResolutionfixed 
PlatformOSOS Version
Product Version3.10.0 
Target VersionFixed in Version3.12.1+dev 
Summary0004345: Reduced precision when converting floats to strings
DescriptionFirst, consider the top-level printing of values:

$ ocaml
# let pi = 3.14159265358979312 ;;
val pi : float = 3.14159265358979312
# float_of_string "3.14159265358979312" = pi ;;
- : bool = true

While:

$ ocaml camlp4rf.cma
# value pi = 3.14159265358979312 ;
value pi : float = 3.14159265359
# float_of_string "3.14159265359" = pi ;
- : bool = False

Similarly, consider `flo antiquotations:

$ ocaml camlp4rf.cma
# open Camlp4.PreCast;
# value _loc = Loc.ghost;
# value pi = 3.14159265358979312;
# <:expr< $`flo:pi$ >> ;
- : Camlp4.PreCast.Ast.expr =
Camlp4.PreCast.Ast.ExFlo <abstr> "3.14159265359"

While it might not be possible to produce string versions whose float_of_string value always exactly equals the original float value, the precision of the currently produced versions is far below the precision of Ocaml's float type.
Additional InformationThis is probably related to the usage of string_of_float to produce the string versions. Consider the following alternative, located inside typing/oprint.ml, and probably used by OCaml's standard toplevel:

let float_repres f =
  match classify_float f with
    FP_nan -> "nan"
  | FP_infinite ->
      if f < 0.0 then "neg_infinity" else "infinity"
  | _ ->
      let s1 = Printf.sprintf "%.12g" f in
      if f = float_of_string s1 then valid_float_lexeme s1 else
      let s2 = Printf.sprintf "%.15g" f in
      if f = float_of_string s2 then valid_float_lexeme s2 else
      Printf.sprintf "%.18g" f
TagsNo tags attached.
Attached Files

- Relationships
related to 0004767closedxclerc Camlp4 prints infinity 
related to 0001154resolved string_of_float loosing precision 

-  Notes
There are no notes attached to this issue.

- Issue History
Date Modified Username Field Change
2007-07-17 11:46 brunodefraine New Issue
2007-11-10 15:00 xleroy Status new => assigned
2007-11-10 15:00 xleroy Assigned To => ertai
2011-02-08 15:05 xclerc Assigned To ertai => xclerc
2011-02-08 15:05 xclerc Relationship added related to 0004767
2011-02-08 15:08 xclerc Status assigned => resolved
2011-02-08 15:08 xclerc Fixed in Version => 3.12.1+dev
2011-02-08 15:08 xclerc Resolution open => fixed
2012-01-20 14:16 doligez Relationship added related to 0001154
2013-08-31 12:44 xleroy Status resolved => closed


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker