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 MAA28474; Fri, 11 Oct 2002 12:05:53 +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 MAA27640 for ; Fri, 11 Oct 2002 12:05:52 +0200 (MET DST) Received: from athlon.baretta.com (host94-68.pool80116.interbusiness.it [80.116.68.94]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g9BA5p506276 for ; Fri, 11 Oct 2002 12:05:51 +0200 (MET DST) Received: from baretta.com (localhost.localdomain [127.0.0.1]) by athlon.baretta.com (Postfix) with ESMTP id 7FF70273AE; Fri, 11 Oct 2002 12:16:13 +0200 (CEST) Message-ID: <3DA6A4ED.5050700@baretta.com> Date: Fri, 11 Oct 2002 12:16:13 +0200 From: Alessandro Baretta Organization: Baretta srl -- www.baretta.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: it, en MIME-Version: 1.0 To: "Scott J," , Ocaml Subject: Re: [Caml-list] Runtime overflow and what to do References: <00bd01c2706d$e7cd68e0$0100a8c0@janxp> <200210101521.RAA0000032708@beaune.inria.fr> <20021010181323.GA1922@iliana> <20021010205716.A8990@pauillac.inria.fr> <021501c270aa$ca400660$0100a8c0@janxp> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Scott J, wrote: > Hi, > > So it is not easy, perhaps even impossible if the processor doesn't set > a flag . > > e.g. fact n begins first with a lis of negative numbers (after overflow) > then comes again a list with positive numbers and then it remains = 0 > > Scott If your problem is only the factorial function, then you can calculate statically (i.e. at source-code writing time) the value of the maximum integer whose factorial will fit in O'Caml's representation of integers, and test against that, once and for all, before entering the recursive calculation. exception overflow let max_fact_arg = ... let fact = let rec rec_fact n = if n <= 0 then 1 else n * rec_fact (n-1) in function | x when x <= max_fact_arg -> rec_fact n | _ -> raise Overflow This strategy allows to incur in hardly any runtime costs for the check, and it also allows you prove more easily that your program will not raise runtime Overflow exceptions (the precondition for not entering the exception raising branch does not have to be propagated out of a recursive function call). Alex ------------------- 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