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: Michel Quercia <michel.quercia@p...>
Subject: Re: [Caml-list] Runtime overflow and what to do
Le Dimanche 13 Octobre 2002 18:14, Xavier Leroy a écrit :

> > [Overflow checking in integer multiplication]
> > 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))
> 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...

What about this one ?

let mul a b =
  if a = 0 then 0
  else
    let x = a*b in
    let q = x/a and r = x mod a in
    if (q = b) & (r = 0) then x else raise Overflow


-- 
Michel Quercia
23 rue de Montchapet, 21000 Dijon
http://michel.quercia.free.fr (maths)
http://pauillac.inria.fr/~quercia (informatique)
mailto:michel.quercia@prepas.org

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