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 MAA20632; Sun, 13 Oct 2002 12:19:09 +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 MAA20618 for ; Sun, 13 Oct 2002 12:19:08 +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 g9DAJ7520825; Sun, 13 Oct 2002 12:19:07 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA20723; Sun, 13 Oct 2002 12:19:07 +0200 (MET DST) From: Pierre Weis Message-Id: <200210131019.MAA20723@pauillac.inria.fr> Subject: Re: [Caml-list] Runtime overflow and what to do In-Reply-To: <20021013112537.J13771@pauillac.inria.fr> from Xavier Leroy at "Oct 13, 102 11:25:37 am" To: xavier.leroy@inria.fr (Xavier Leroy) Date: Sun, 13 Oct 2002 12:19:07 +0200 (MET DST) Cc: jscott@planetinternet.be, 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 > > So it is not easy, perhaps even impossible if the processor doesn't set a > > flag . > > It is possible, but slow and not pretty! I must precise that even if the code to implement arithmetics with overflow checks can be considered as ``not pretty'', its usage is absolutely easy and transparent in Caml: just write a specialized module for arithmetics with overflow checks (say ``Cautious_arith'') that redefines ( + ), ( - ), ( * ), ( / ), min, etc, along the lines sketched by Xavier for addition (by the way, writing such a module is a very pleasant home exercise); once you have written the Cautious_arith module, write your application code as usual. Now, if you want overflow checks just add open Cautious_arith at the beginning of your module and recompile. If you changed your mind, and want to recover the normal speed of integer arithmetic operations, just remove the open directive and recompile! This is an elegant and clean application of very basic operators redefinition that not so many programming languages offer. 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