Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strange floating point exception with ocamlopt #3170

Closed
vicuna opened this issue Jan 26, 2002 · 2 comments
Closed

strange floating point exception with ocamlopt #3170

vicuna opened this issue Jan 26, 2002 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Jan 26, 2002

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;

@vicuna
Copy link
Author

vicuna commented Feb 11, 2002

Comment author: administrator

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

  • Xavier Leroy

@vicuna
Copy link
Author

vicuna commented Feb 11, 2002

Comment author: administrator

Works as documented.

@vicuna vicuna closed this as completed Feb 11, 2002
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant