Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Runtime overflow and what to do
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <pierre.weis@i...>
Subject: Re: [Caml-list] Runtime overflow and what to do
> > [Overflow checking in integer multiplication]
> 
> > (* [lenght_of_int] is the maximum number of bits in an integer. *)
> > let length_of_int = Sys.word_size - 2;;
> > 
> > let ( * ) n m =
> >  if num_bits_int n + num_bits_int m < length_of_int
> >  then n * m else raise (Overflow ("*", string_of_int n, string_of_int m))
> > 
> > (* [num_bits_int n] returns the number of significant bits of an
> >    integer. *)
> 
> Hmmph... this definition raises Overflow when computing
> min_int * 1, or (min_int / 2) * 2, while these products are actually
> representable within a machine integer...
> 
> Apologies for nit-picking; I couldn't resist!
> 
> - Xavier Leroy

Well spotted, thank you very much!

As a reward, note that your overflow test on addition raises overflow
when computing (max_int + 1) - 1, while this sum is actually
representable within a machine integer...

Sure, computer arithmetics is tricky!

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


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