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

Incorrect bigarray values #6040

Closed
vicuna opened this issue Jun 15, 2013 · 3 comments
Closed

Incorrect bigarray values #6040

vicuna opened this issue Jun 15, 2013 · 3 comments
Assignees

Comments

@vicuna
Copy link

vicuna commented Jun 15, 2013

Original bug ID: 6040
Reporter: dondub
Assigned to: @gasche
Status: closed (set by @xavierleroy on 2015-12-11T18:19:46Z)
Resolution: not a bug
Priority: normal
Severity: minor
OS: linux
OS Version: 3.2.0
Version: 4.00.1
Category: otherlibs

Bug description

On my system (linux 3.2.0 32 bits, gcc 4.6.3), a float32 bigarray initialized with values smaller than 0.5 return incorrect values.

Steps to reproduce

ba.ml :

let ba = Bigarray.Array1.of_array Bigarray.float32 Bigarray.c_layout
[|0.1; 0.2; 0.3; 0.4; 0.41; 0.49; 0.5|] in

for i = 0 to Bigarray.Array1.dim ba - 1 do
print_endline(string_of_float(Bigarray.Array1.get ba i))
done

$ocamlopt.opt bigarray.cmxa ba.ml -o ba
$./ba
0.10000000149
0.20000000298
0.300000011921
0.40000000596
0.409999996424
0.490000009537
0.5

@vicuna
Copy link
Author

vicuna commented Jun 15, 2013

Comment author: @gasche

Given that OCaml "float" type corresponds to C's "double" type (or float64 as a Bigarray kind), it is not surprising that values lose some precision when stored as float32.

You can observe that same precision loss by converting to Int32 and back.

let t = [|0.1; 0.2; 0.3; 0.4; 0.41; 0.49; 0.5|];;

val t : float array = [|0.1; 0.2; 0.3; 0.4; 0.41; 0.49; 0.5|]

Array.iter (fun x -> print_endline (string_of_float (Int32.float_of_bits (Int32.bits_of_float x)))) t;;

0.10000000149
0.20000000298
0.300000011921
0.40000000596
0.409999996424
0.490000009537
0.5

@vicuna
Copy link
Author

vicuna commented Jun 15, 2013

Comment author: dondub

Thank you for the quick answer and all apologies for the undue report.

@vicuna
Copy link
Author

vicuna commented Jun 16, 2013

Comment author: @xavierleroy

No problem. If you need background on floating-point, the Wikipedia article
http://en.wikipedia.org/wiki/Floating_point is decent. See esp. the section on "Accuracy problems". For more info, see Goldberg's survey "What every computer scientist should know about floating-point arithmetic".

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

2 participants