From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id CAA27895; Sun, 5 May 2002 02:30:26 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA27896 for ; Sun, 5 May 2002 02:30:25 +0200 (MET DST) Received: from smtp10.atl.mindspring.net (smtp10.atl.mindspring.net [207.69.200.246]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g450UOX26682 for ; Sun, 5 May 2002 02:30:24 +0200 (MET DST) Received: from sdn-ap-030caburbp0236.dialsprint.net ([65.177.240.236] helo=dylan) by smtp10.atl.mindspring.net with esmtp (Exim 3.33 #1) id 1749up-0007on-00 for caml-list@inria.fr; Sat, 04 May 2002 20:30:16 -0400 Message-ID: <003f01c1f3cc$2ed280f0$210148bf@dylan> From: "David McClain" To: References: <20020504105324.A15588@pauillac.inria.fr> Subject: Re: [Caml-list] string_of_float less accurate than sprintf "%f" ? Date: Sat, 4 May 2002 17:31:20 -0700 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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