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 RAA03982; Thu, 10 Oct 2002 17:21:14 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA03796 for ; Thu, 10 Oct 2002 17:21:14 +0200 (MET DST) Received: from beaune.inria.fr (beaune.inria.fr [128.93.8.3]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g9AFLDD01207 for ; Thu, 10 Oct 2002 17:21:13 +0200 (MET DST) Received: by beaune.inria.fr (8.8.8/1.1.22.3/14Sep99-0328PM) id RAA0000032708; Thu, 10 Oct 2002 17:21:13 +0200 (MET DST) From: Luc Maranget Message-Id: <200210101521.RAA0000032708@beaune.inria.fr> Subject: Re: [Caml-list] Runtime overflow and what to do To: jscott@planetinternet.be (Scott J, ) Date: Thu, 10 Oct 2002 17:21:13 +0200 (MET DST) Cc: caml-list@inria.fr In-Reply-To: <00bd01c2706d$e7cd68e0$0100a8c0@janxp> from "Scott J," at oct 10, 2002 05:01:26 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > Hi, > > > suppose I use the following code for fact > > let rec fact n = if n <= 1 then 1 else n * fact (n - 1) ;; > > Because an integer number is represented by a fixed number of bytes I will > get a runtime overflow if n is chosen too large. > > Is there in Ocamel a workaround to cope with this problem . Something like " > Onoverflow goto .. " in imperative languages. > > Thx > > Scott > > There is no such feature as a Onoverflow goto, because Ocaml (and not OCamel...) default integers are not << real >> integers, but machine integers on some size (typically 31 or 63) bits. This means that maxint + 1 is minint and basically this is the behavior of C. I guess the reason why is avoiding putting non-obvious tests everywhere. Most of the time you do not need those tests. * If you want ``real'' integers, you can use some arbitrary precision library (Num, etc.), overflow detection would not help here anyway. * If you want to be warned on overflow, you have to devise the tests by yourself, and then you can use an exception as your Onoverflow goto An easy one : expection Overflow let protected_incr x = let y = x+1 in if y > x then y else raise Overflow try .... incr (..) with | Overflow -> ... do whatever you can ... Hope it helps. --Luc ------------------- 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