English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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 (00:58)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] Multiplication of matrix in C and OCaml

On Fri, 9 Feb 2007, Andrej Bauer wrote:

> I hate to add to this long discussion, but since when is floating point 
> multiplication associative?
> # 1.3 *. (0.7 *. 2.1) = (1.3 *. 0.7) *. 2.1 ;;
> - : bool = false

Round off error.  You can't represent numbers like 7/10 or 13/10 in binary 
precisely in a finite number of digits, any more than you can represent 
the numbers 1/3 or 4/7 precisely in base-10 in a finite number of digits. 
This is a fundamental problem with floating point numbers, and shows up in 
every language that has them.  But this means reordering the calculations 
will result in slightly different answers.

And no, most of the "obvious" solutions don't work.  Using rational 
arithmetic doesn't help if I ask you to calculate the euclidean length of 
the vector (1, 1) for example (hint: sqrt (1.*.1. +. 1.*.1.)).  Interval 
arithmetic fails miserably on simple algorithms like Newton's method.  And 
so on.

On the other hand, the precision given by double precision floating point 
numbers is generally more than enough- they're precise enough to hold the 
distance from here to the moon in microns, for example.

My advice: if you're doing much of anything with floating point numbers, 
get a book on numerical analysis and follow the algorithms and 
recommendations it gives you (any book except "Numerical Recipies", which 
suck).  And don't use floating point for money.