You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 834 Reporter: administrator Status: closed Resolution: not a bug Priority: normal Severity: minor Category: ~DO NOT USE (was: OCaml general)
Bug description
Hello,
[Ocaml v3.04, Linux 2.4.2]
I've attached below a program that terminates abnormally with a
floating point exception if compiled with ocamlopt, but not with
ocamlc. Maybe I'm doing something wrong, but I can't find the
problem easily since it is not there in the byte-compiled version
so I can't use the debugger. Anyway, I suspect that since the
behavior is different between ocamlopt/ocamlc, then it may indeed
be a bug, so I'm submitting it for your consideration. Thanks.
cheers,
doug
(*
flt.pt.exc.ml
This causes a floating point exception:
ocamlopt flt.pt.exc.ml && ./a.out
i = 2
i = 3
Floating point exception (core dumped)
But if compiled with ocamlc the program runs to completion:
ocamlc flt.pt.exc.ml && ./a.out
i = 2
i = 3
i = 4
*)
let _ =
let ops = [| ( / ); ( * ) |] in
let tab = Hashtbl.create 1000 in
let put op x y =
try (let k = op x y in
try Hashtbl.find tab k with Not_found -> Hashtbl.add tab k ())
with Division_by_zero -> () in
let puts op x y = put op x y; put op (-x) y; put op x (-y) in
Hashtbl.add tab 9 ();
for i = 2 to 4 do
prerr_endline (Printf.sprintf "i = %d" i);
Hashtbl.iter (fun n _ -> Array.iter (fun op -> puts op 9 n) ops) tab
done;
The text was updated successfully, but these errors were encountered:
I've attached below a program that terminates abnormally with a
floating point exception if compiled with ocamlopt, but not with
ocamlc.
This strange behavior is documented (end of chapter 11):
This section lists the known incompatibilities between the bytecode
compiler and the native-code compiler. Except on those points, the two
compilers should generate code that behave identically.
* The following operations abort the program (either by printing
an error message or just via an hardware trap or fatal Unix signal)
instead of raising an exception:
* integer division by zero, modulus by zero;
* [...]
Basically, there is no portable way to convert the
"divide by zero" hardware exception into a Caml exception. The
generated code could check for a zero divisor, of course, but this
isn't done currently because I'm too lazy :-)
In your particular example, I'd suggest replacing
let ops = [| ( / ); ( * ) |] in
by
let ops = [| safe_div; ( * ) |]
where
let safe_div a b = if b = 0 then raise Division_by_zero else a / b
Original bug ID: 834
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)
Bug description
Hello,
[Ocaml v3.04, Linux 2.4.2]
I've attached below a program that terminates abnormally with a
floating point exception if compiled with ocamlopt, but not with
ocamlc. Maybe I'm doing something wrong, but I can't find the
problem easily since it is not there in the byte-compiled version
so I can't use the debugger. Anyway, I suspect that since the
behavior is different between ocamlopt/ocamlc, then it may indeed
be a bug, so I'm submitting it for your consideration. Thanks.
cheers,
doug
(*
flt.pt.exc.ml
This causes a floating point exception:
But if compiled with ocamlc the program runs to completion:
*)
let _ =
let ops = [| ( / ); ( * ) |] in
let tab = Hashtbl.create 1000 in
let put op x y =
try (let k = op x y in
try Hashtbl.find tab k with Not_found -> Hashtbl.add tab k ())
with Division_by_zero -> () in
let puts op x y = put op x y; put op (-x) y; put op x (-y) in
Hashtbl.add tab 9 ();
for i = 2 to 4 do
prerr_endline (Printf.sprintf "i = %d" i);
Hashtbl.iter (fun n _ -> Array.iter (fun op -> puts op 9 n) ops) tab
done;
The text was updated successfully, but these errors were encountered: