Version française
Home     About     Download     Resources     Contact us    
Browse thread
Multiplication of matrix in C and OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Multiplication of matrix in C and OCaml
On Saturday 10 February 2007 14:41, ls-ocaml-developer-2006@m-e-leypold.de 
wrote:
> Just to be sure: Would the compiler be wrong to optimize
>
>    c * q > c * k
>
> to just
>
>        q > k
>
> (all floats). And why?

Consider c=0, q=3 and k=2:

0 * 3 > 0 * 2  --> false
    3 > 2      --> true

It is just mathematically incorrect.

> If not, why, in the case above?

Floats are not reals, they are just an approximation that happens to be very 
useful. Floats do not obey the same laws (e.g. associativity). However, 
programmers may be relying upon this fact, e.g. when doing exact float 
arithmetic.

> I don't want  
> letter and verse, but a general hand waving in the right direction
> would be nice, since I have the impression, that is exactly what Gcc
> 4.1. is currently doing (though for the integer case).

Ints are completely different because they are exact (as modulo integers). So 
they do obey the same laws and they do not have special constants (like 
infinity, neg_infinity, nan, -0. and so on as floats do).

Xavier has written some enlightening posts here in the past, regarding the 
adoption of strict IEEE compliance in OCaml:

http://caml.inria.fr/pub/ml-archives/caml-list/2004/10/ffa452944f4bb9827f2cdca552f4f823.en.html

For an example of someone using the float constants that lie outside the set 
of reals, look no further than my ray tracer:

http://www.ffconsultancy.com/free/ray_tracer/languages.html

I used an initial intersection parameter of lambda=infinity to represent no 
intersection (or intersecting with the sky). I would be mortified if a 
compiler decided to optimise away my necessary and working code just because 
infinity is not in the set of real numbers.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists