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

floating point differences between native code and byte code on any Intel target #4161

Closed
vicuna opened this issue Nov 14, 2006 · 1 comment

Comments

@vicuna
Copy link

vicuna commented Nov 14, 2006

Original bug ID: 4161
Reporter: schneck
Status: closed (set by @damiendoligez on 2006-11-15T11:18:50Z)
Resolution: won't fix
Priority: normal
Severity: minor
Version: 3.09.0
Category: ~DO NOT USE (was: OCaml general)
Duplicate of: #4025

Bug description

Earlier I posted a bug mentioning floating point problems with native code on an Intel Mac; the same problems arise in Windows. Startling that no one else has noticed this! It leads to different behaviors in real programs, but here are some toy examples. Many of them have to do with optimizations, but one shows an actual different result from a single division.

(*
ocamlopt -o intel_bugs intel_bugs.ml
./intel_bugs
ocamlc -o intel_bugs.byte intel_bugs.ml
./intel_bugs.byte

4600877379321698713
4600877379321698714
4616752568008179712
4616752568008179711
4571838168924413755
4571838168924413756
4607182418800017408
4607182418800017407
4602678819172646912

4600877379321698714
4600877379321698714
4616752568008179711
4616752568008179711
4571838168924413756
4571838168924413756
4607182418800017407
4607182418800017407
4602678819172646913
*)

let print x =
print_endline (Int64.to_string (Int64.bits_of_float x))

let bug_1 () =
let x = 1. /. 3. in
let y = 1. /. 2. in
print (x /. (x +. y));
let (/.) = (/.) in
print (x /. (x +. y))

let bug_2 () =
let x = 0.75 in
let y = 0.3 in
let z = 0.05 in
print ((x *. y) /. z);
let (/.) = (/.) in
print ((x *. y) /. z)

let bug_3 () =
let a = 0.1 in
let x = 0.0005 in
let y = 4. /. 900. in
print (x +. (1. -. a) *. y);
let ( *. ) = ( *. ) in
print (x +. (1. -. a) *. y)

let bug_4 () =
let x = Int64.float_of_bits (Int64.of_string "4600105333671292343") in
let y = Int64.float_of_bits (Int64.of_string "4607182418800017407") in
print (x *. y +. (1. -. x));
let (+.) = (+.) in
print (x *. y +. (1. -. x))

let bug_5 () =
let a = 0.5 in
let b = Int64.float_of_bits (Int64.of_string "4607182418800017407") in
print (a /. b)

let _ =
bug_1 ();
bug_2 ();
bug_3 ();
bug_4 ();
bug_5 ()

@vicuna
Copy link
Author

vicuna commented Nov 15, 2006

Comment author: @damiendoligez

See the notes for #4025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant