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 OAA24122; Sun, 13 Oct 2002 14:38:47 +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 OAA23712 for ; Sun, 13 Oct 2002 14:38:46 +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 g9DCcj523625; Sun, 13 Oct 2002 14:38:45 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA23940; Sun, 13 Oct 2002 14:38:45 +0200 (MET DST) From: Pierre Weis Message-Id: <200210131238.OAA23940@pauillac.inria.fr> Subject: Re: [Caml-list] Runtime overflow and what to do In-Reply-To: <20021013124752.H7639@verdot.inria.fr> from Daniel de Rauglaudre at "Oct 13, 102 12:47:52 pm" To: daniel.de_rauglaudre@inria.fr (Daniel de Rauglaudre) Date: Sun, 13 Oct 2002 14:38:45 +0200 (MET DST) Cc: 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 Hi again, > On Sun, Oct 13, 2002 at 12:29:58PM +0200, Pierre Weis wrote: > > > 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)) > > Does it work with negative numbers? And with multiplication between a > positive and a negative number? Yes it does. > > Just to let people play with the writting of the module :), > > num_bits_int is left as an exercise to the interested readers (3 lines > > of Caml code using trivial bit shuffling). > > I am interested in knowing it, because I already searched that. > Without a loop or a predefined array of powers of two, I don't > know how to do that. Nor did I. I think there exists processors that do have an instruction to implement this function but this is not the general case: you need to write a loop or use a predefined array of powers of two or write a dichotomic algorithm or whatever... For instance: let num_bits_int n = let rec num_bits n = if n = 0 then 0 else succ (num_bits (n lsr 1)) in num_bits (abs n);; (No dichotomy, here. Experiments with a dichotomic algorithm is also interesting, but deciding which is faster is difficult since benchmarking is almost impossible: the results are too much dependant of the value of integers manipulated in the program.) 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