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 LAA17936; Sun, 13 Oct 2002 11:25:39 +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 LAA18132 for ; Sun, 13 Oct 2002 11:25:38 +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 g9D9Pc518845; Sun, 13 Oct 2002 11:25:38 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA18212; Sun, 13 Oct 2002 11:25:37 +0200 (MET DST) Date: Sun, 13 Oct 2002 11:25:37 +0200 From: Xavier Leroy To: "Scott J," Cc: caml-list@inria.fr Subject: Re: [Caml-list] Runtime overflow and what to do Message-ID: <20021013112537.J13771@pauillac.inria.fr> References: <00bd01c2706d$e7cd68e0$0100a8c0@janxp> <200210101521.RAA0000032708@beaune.inria.fr> <20021010181323.GA1922@iliana> <20021010205716.A8990@pauillac.inria.fr> <021501c270aa$ca400660$0100a8c0@janxp> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <021501c270aa$ca400660$0100a8c0@janxp>; from jscott@planetinternet.be on Fri, Oct 11, 2002 at 12:17:15AM +0200 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > So it is not easy, perhaps even impossible if the processor doesn't set a > flag . It is possible, but slow and not pretty! For example, here is Caml code that performs integer addition and detects overflows: exception Integer_overflow let no_overflow_add a b = if (a lxor b) lor (a lxor (lnot (a+b))) < 0 (* no kidding! *) then a + b else raise Integer_overflow Multiplication is left as an exercise for the readers :-) My advice would be to use bignums; they are a bit slower, but at least you get the exact result in all cases. - Xavier Leroy ------------------- 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