Browse thread
float rounding
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Olivier Andrieu <oandrieu@n...> |
| Subject: | Re: [Caml-list] float rounding |
Christophe Raffalli [Tuesday 3 October 2006] :
>
> Sean McLaughlin a écrit :
> > Hello,
> >
> > I'm using Ocaml for an interval arithmetic application. I"m
> > curious about what the Ocaml parser/compiler does to float
> > constants. May I assume that for any constant I enter,
> > eg. 3.1415... (for N digits of pi), that the compiler will give
> > me a closest machine representable number? i.e., if I bound a
> > constant by the previous and next floating point value to that
> > given me by the compiler, will it always be the case that my
> > original (mathematical) constant lies in that interval?
> >
>
> By the way, float constants need to be written in hexadecimal and
> this is missing to the printf/scanf functions (it is what man
> printf says at least) ... just compute how many decimals you need
> to write the exact value of 2^{-n} as a decimal float constant (0,5
> 0,25 0,125 0,625e-1 0,3125e-1 ...).
float_of_string (which uses strtod) already knows how to read a
hexadecimal float and there's a hack to have the C printf do the writing:
,----
| # external format_float: string -> float -> string = "caml_format_float" ;;
| external format_float : string -> float -> string = "caml_format_float"
| # let hex_float = format_float "%a" ;;
| val hex_float : float -> string = <fun>
| # hex_float 1.25 ;;
| - : string = "0x1.4p+0"
| # float_of_string "0x1.4p+0" ;;
| - : float = 1.25
| # hex_float 0.1 ;;
| - : string = "0x1.999999999999ap-4"
`----
--
Olivier