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

Int32.rem floating point exception #4740

Closed
vicuna opened this issue Mar 8, 2009 · 2 comments
Closed

Int32.rem floating point exception #4740

vicuna opened this issue Mar 8, 2009 · 2 comments
Assignees
Labels

Comments

@vicuna
Copy link

vicuna commented Mar 8, 2009

Original bug ID: 4740
Reporter: delorimier
Assigned to: @xavierleroy
Status: closed (set by @xavierleroy on 2011-05-29T10:14:18Z)
Resolution: fixed
Priority: normal
Severity: crash
Version: 3.10.2
Fixed in version: 3.11.1+dev
Category: ~DO NOT USE (was: OCaml general)
Related to: #5513
Monitored by: delorimier

Bug description

(Int32.rem Int32.min_int Int32.minus_one) gives a machine generated floating point exception, and exits the toplevel interpretor.
The expression should return Int32.zero.

My machine is a 32 bit x86.
This bug occurs in version 3.10.2 bytecode and native.
It also occurs in version 3.10.0 bytecode, and I did not try native.

Here's an example:
% ocaml
Objective Caml version 3.10.2

open Int32;;

rem min_int minus_one;;

zsh: floating point exception ocaml
%

I couldn't find any other bad Int32.rem cases.

@vicuna
Copy link
Author

vicuna commented Mar 8, 2009

Comment author: delorimier

I also get the same error with Int32.div

@vicuna
Copy link
Author

vicuna commented Mar 28, 2009

Comment author: @xavierleroy

Let's start with division first, since this is where the real problem lies. Dividing min_int by (-1) gives a result (- min_int) which is not representable in two's complement. Some processors (x86) raise a hardware exception in this case, other return min_int, yet others produce unspecified results.

Concerning remainder, min_int rem (-1) is perfectly representable since it is 0, but processors like x86 have only one instruction that computes the quotient and the remainder, so the same problems as with division occur.

I agree this undefined behavior in OCaml is not nice, therefore I implemented a fix whereas Int32.div Int32.min_int Int32.minus_one = Int32.min_int and Int32.rem Int32.min_int Int32.minus_one = Int32.zero. (Likewise for Int64 and Nativeint.) This behavior is consistent with that of "/" and "mod" at type int.

The fix is in the 3.11 bugfix branch and will be in release 3.11.1.

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

2 participants