From: "David McClain" <dmcclain1@mindspring.com>
To: <caml-list@inria.fr>
Subject: Re: [Caml-list] string_of_float less accurate than sprintf "%f" ?
Date: Sat, 4 May 2002 17:31:20 -0700 [thread overview]
Message-ID: <003f01c1f3cc$2ed280f0$210148bf@dylan> (raw)
In-Reply-To: <20020504105324.A15588@pauillac.inria.fr>
Xavier Leroy writes:
> There might be a more philosophical issue behind this. For a
> numerical analyst, or physicist, or experimental scientist in general,
....
I am such a scientist, and in fact the numeric representation I seek
generally depends heavily on the experimental precision available. So I
ended up writing a flexible printf routine in OCaml to support printing only
as many decimal places as afforded by the standard deviation of my
measurements. It was easy to do, but it did require the use of magic...
Here is the general formatting routine...
let fmt_val_sigma title value sigma =
let rec iter sf x =
if x >= 1.0 then
if sf > 4 then
printf "%g (%g)\n" value sigma
else
printf (U.as_format
(sprintf "%%%d.%df (%%%d.%df)\n"
(sf + 3) sf (sf + 2) sf))
value sigma
else
iter (sf + 1) (x *. 10.0)
in
printf "%20s = " title;
if sigma = 0.0 then printf "%g\n" value
else
iter 0 sigma
Here is a sample of its use...
fmt_val_sigma "psf_scale" sys.parms.psf_scale sigma_scale;
where the value of interest is psf_scale, and its standard deviation is
sigma_scale. The rouitne above examines the standard deviation and
determines the appropriate print format, typically in the form "title 1.03
(5)" where the value is 1.03 and the standard deviation is 0.05, or (5) in
the last place...
And here is the "magic" in module Utility.ml and Utility.mli (the
U.as_format in the above code)
Utility.mli:
(* convert a dynamic string to a printf format *)
val as_format : string -> ('a, out_channel, unit) format
Utility.ml:
(* as_format allows to build a string that can later be used *)
(* as a printf format string *)
let as_format (str : string) =
(Obj.magic str : ('a,out_channel,unit) format)
Cheers,
- David McClain, Sr. Scientist, Raytheon Missile Systems Co., Tucson, AZ
-------------------
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
next prev parent reply other threads:[~2002-05-05 0:30 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-04-30 8:21 Beck01, Wolfgang
2002-05-02 12:44 ` John Max Skaller
2002-05-02 12:54 ` Francois Thomasset
2002-05-05 17:33 ` John Max Skaller
2002-05-02 13:28 ` David Chase
2002-05-05 18:19 ` John Max Skaller
2002-05-02 13:46 ` jeanmarc.eber
2002-05-03 14:41 ` Oliver Bandel
[not found] ` <Pine.LNX.3.95.1020503162341.541E-100000@first.in-berlin.de >
2002-05-03 18:28 ` David Chase
2002-05-04 8:53 ` Xavier Leroy
2002-05-05 0:31 ` David McClain [this message]
2002-05-06 14:19 ` David Chase
2002-05-06 18:21 ` David McClain
2002-05-03 19:25 David Chase
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='003f01c1f3cc$2ed280f0$210148bf@dylan' \
--to=dmcclain1@mindspring.com \
--cc=caml-list@inria.fr \
/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