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: 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
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;;
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".
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
The text was updated successfully, but these errors were encountered: