Browse thread
Re: [Caml-list] Runtime overflow and what to do
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Pierre Weis <pierre.weis@i...> |
| Subject: | Re: [Caml-list] Runtime overflow and what to do |
> Hi,
>
> On Sun, Oct 13, 2002 at 11:25:37AM +0200, Xavier Leroy wrote:
>
> > Multiplication is left as an exercise for the readers :-)
>
> Much more complicated... I tried that a long time ago when I was
> writting a PostScript interpreter, but I found no solution.
(* [lenght_of_int] is the maximum number of bits in an integer. *)
let length_of_int = Sys.word_size - 2;;
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))
(* [num_bits_int n] returns the number of significant bits of an
integer. *)
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).
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