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 QAA03378; Fri, 3 May 2002 16:45:07 +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 QAA03161 for ; Fri, 3 May 2002 16:45:07 +0200 (MET DST) Received: from hirsch.in-berlin.de (hirsch.in-berlin.de [192.109.42.6]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g43Ej5X20782 for ; Fri, 3 May 2002 16:45:05 +0200 (MET DST) Received: from hirsch.in-berlin.de (localhost [127.0.0.1]) by hirsch.in-berlin.de (8.12.1/8.12.1/Debian -2) with ESMTP id g43Ej3H0028203 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NOT); Fri, 3 May 2002 16:45:03 +0200 Received: (from uucp@localhost) by hirsch.in-berlin.de (8.12.1/8.12.1/Debian -2) with UUCP id g43Ej20F028158; Fri, 3 May 2002 16:45:02 +0200 X-Envelope-From: oliver@first.in-berlin.de X-Envelope-To: caml-list@inria.fr Received: from localhost (oliver@localhost) by first.in-berlin.de (8.7.6/8.7.3) with SMTP id QAA00844; Fri, 3 May 2002 16:41:25 +0200 Date: Fri, 3 May 2002 16:41:25 +0200 (MET DST) From: Oliver Bandel To: John Max Skaller cc: "Beck01, Wolfgang" , caml-list@inria.fr Subject: Re: [Caml-list] string_of_float less accurate than sprintf "%f" ? In-Reply-To: <3CD134B0.8050100@ozemail.com.au> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Thu, 2 May 2002, John Max Skaller wrote: > Beck01, Wolfgang wrote: > > >Hello, > > > >while doing some time measurements with Unix.gettimeofday() I > >discovered a problem with string_of_float: > > > ># string_of_float 123456789.123456789;; > >- : string = "123456789.123" > > > There's another problem too: > > # string_of_float 42.0;; > - : string = "42" > > The result isn't an ocaml float literal. Well... but the type is not necessarily an int though. The type is a float, even if the representation looks like an int. The only problem here is that it's confusing for human readers. But is it necessary that a type can have only one possible representation? A problem only occurs with type inference... you can not say that 42 is a float-value: the type inference always says, that 42 is an int: # 42;; will not yield a float. This is confusing. I had problems with it too. But when you look at the following example, you see, that the type will be used correct and is not derived from it's representation: ################################################## oliver@first:/home/oliver > ocaml Objective Caml version 3.04 # type mytype = Nothing | Value of float;; type mytype = Nothing | Value of float # let x = Nothing;; val x : mytype = Nothing # let y = Value 42;; This expression has type int but is here used with type float # let y = Value (float_of_string "42");; val y : mytype = Value 42 # let z = Value (float_of_string (string_of_float 24.0));; val z : mytype = Value 24 # let zz = Value (float_of_string (string_of_float 33));; This expression has type int but is here used with type float # ################################################## Value is of type float and Ocaml works correctly with it. So this is not really a problem. It seems to be only a problem, when type inference is used - so it's a problem in human-machine-interfacing. But a problem can occur too, when the type is not known from within the program: When writing the string-representation of a float to a file and read it back from the file, then the non-float representation of the float yields in parsing-/type-problems. So that is a problem within the program and it's data (I/O)... So this seems to be actually a problem. If (string_of_float 42.0) yields "42.0" (instead of "42"), this maybe is not the right value, because it maybe is only correct as "42.0000000000000"? Well.... philosophical question? Ciao, Oliver ------------------- 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