From: Olivier Andrieu <oandrieu@nerim.net>
To: Christophe Raffalli <Christophe.Raffalli@univ-savoie.fr>
Cc: Sean McLaughlin <seanmcl@cmu.edu>, caml-list@inria.fr
Subject: Re: [Caml-list] float rounding
Date: Tue, 3 Oct 2006 23:05:32 +0200 [thread overview]
Message-ID: <17698.53404.408141.945571@karryall.dnsalias.org> (raw)
In-Reply-To: <4522C131.9010400@univ-savoie.fr>
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
next prev parent reply other threads:[~2006-10-03 21:05 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-09-30 18:25 Sean McLaughlin
2006-10-01 12:44 ` [Caml-list] " Florent Monnier
2006-10-01 13:36 ` Gerd Stolpmann
2006-10-01 16:36 ` Xavier Leroy
2006-11-23 23:00 ` David Monniaux
2006-10-03 13:41 ` Christophe Raffalli
2006-10-03 19:59 ` Christophe Raffalli
2006-10-03 21:05 ` Olivier Andrieu [this message]
2006-11-26 1:51 ` David Monniaux
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=17698.53404.408141.945571@karryall.dnsalias.org \
--to=oandrieu@nerim.net \
--cc=Christophe.Raffalli@univ-savoie.fr \
--cc=caml-list@inria.fr \
--cc=seanmcl@cmu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox