Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2007-02-10 (14:58)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Multiplication of matrix in C and OCaml
On Saturday 10 February 2007 14:41, 
> 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 

> 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:

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

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