Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] string_of_float less accurate than sprintf "%f" ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Oliver Bandel <oliver@f...>
Subject: Re: [Caml-list] string_of_float less accurate than sprintf "%f" ?


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