Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006040OCamlOCaml otherlibspublic2013-06-15 19:192013-06-16 17:38
Reporterdondub 
Assigned Togasche 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionno change required 
PlatformOSlinuxOS Version3.2.0
Product Version4.00.1 
Target VersionFixed in Version 
Summary0006040: Incorrect bigarray values
DescriptionOn 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 Reproduceba.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
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0009504)
gasche (developer)
2013-06-15 19:28

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
(0009505)
dondub (reporter)
2013-06-15 21:32

Thank you for the quick answer and all apologies for the undue report.
(0009513)
xleroy (administrator)
2013-06-16 17:38

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".

- Issue History
Date Modified Username Field Change
2013-06-15 19:19 dondub New Issue
2013-06-15 19:28 gasche Note Added: 0009504
2013-06-15 19:29 gasche Status new => resolved
2013-06-15 19:29 gasche Resolution open => no change required
2013-06-15 19:29 gasche Assigned To => gasche
2013-06-15 21:32 dondub Note Added: 0009505
2013-06-15 23:10 gasche Severity major => minor
2013-06-16 17:38 xleroy Note Added: 0009513


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker