From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA08900; Tue, 15 Oct 2002 21:15:12 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA08723 for ; Tue, 15 Oct 2002 21:15:12 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g9FJFB524825; Tue, 15 Oct 2002 21:15:11 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA09316; Tue, 15 Oct 2002 21:15:10 +0200 (MET DST) From: Pierre Weis Message-Id: <200210151915.VAA09316@pauillac.inria.fr> Subject: Re: [Caml-list] Runtime overflow and what to do In-Reply-To: <20021013181458.A28677@pauillac.inria.fr> from Xavier Leroy at "Oct 13, 102 06:14:58 pm" To: xavier.leroy@inria.fr (Xavier Leroy) Date: Tue, 15 Oct 2002 21:15:10 +0200 (MET DST) Cc: pierre.weis@inria.fr, daniel.de_rauglaudre@inria.fr, caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > > [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