[MLton-user] Real32.realRound is broken

Matthew Fluet fluet at tti-c.org
Thu Apr 24 07:48:56 PDT 2008


On Wed, 23 Apr 2008, John Reppy wrote:
> The Real32.realRound function in mlton does not work.  Here is a simple test
> program:
>
> 	 fun snap f = 0.001 * Real32.realRound(f * 1000.0);
> 	 fun g x = print(concat["x = ", Real32.toString x, ", snap(x) = ",
> 	 Real32.toString(snap x), "\n"]);
> 	 g 45.0000038147;
>
> The Real64 version of this program produces the correct output
>
> 	x = 45.0000038147, snap(x) = 45
>
> but the Real32 version produces
>
> 	x = 45.0000038147, snap(x) = 45.0000038147
>
> This is using MLton 20070826 on Mac OS X 10.5.2/Intel.

Isn't this simply a consequence of IEEE floating point values being 
represented with radix 2, so the base 10 value 0.001 cannot be represented 
exactly?

The following program:

  structure Real = Real32
  fun r2s x = Real.toString x
  val s = 0.001
  val z = 45000.0
  val sz = s * z
  val () = print(concat["s = ", r2s s, ", z = ", r2s z, ", s * z = ", r2s sz, "\n"])

produces

  s = 0.0010000000475, z = 45000, s * z = 45.0000038147

so, if there is a bug, it isn't with Real32.realRound.



More information about the MLton-user mailing list