Browse thread
[Caml-list] Runtime overflow and what to do
-
Scott J,
- Maxence Guesdon
- Maxence Guesdon
- Florian Hars
[
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: | Florian Hars <florian@h...> |
| Subject: | Re: [Caml-list] Runtime overflow and what to do |
Scott J, wrote:
> Because an integer number is represented by a fixed number of bytes I
> will get a runtime overflow if n is chosen too large.
You have to check explicitly:
# exception Overflow;;
exception Overflow
# let fact n =
let rec fact_r acc n =
if n = 1 then acc
else if max_int / acc > (n-1) then fact_r (acc * n) (n - 1)
else raise Overflow
in
fact_r 1 n;;
val fact : int -> int = <fun>
# fact 12;;
- : int = 479001600
# fact 13;;
Exception: Overflow.
Yours, Florian
-------------------
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