Tree values output different respect to the input

Dears,

I have a strange behaviour of tree… So I have a simple files with 2 columns, like this:

3.5	6.9
6.8	5.2
9.6	3.4
10	10

In simple way I read the columns with tree, And then print the values::

tree1= TTree ("tree1","tree1")			

tree1.ReadFile("myfile.csv","a/F:b/F",'\t') 

N=tree1.Draw("a:b","","goff")
a=tree1.GetV1()

cont=0

while cont < N:
	print a[cont]
	cont=cont+1

But the output is:

3.5
6.80000019073
9.60000038147
10.0

You see that is different respect to the input from the file, from where came the last numbers?? if the number is integer or the decimal is 5 it is ok, but if the values are different I have this a lot digits… why??

Thanks in advance for your help :slight_smile:

Try: tree1.ReadFile("myfile.csv", "a/D:b", '\t')

The answer to “why” is: an IEEE 754 floating point variable cannot store 6.8 precisely.

Try this:

$ root
root [0] cout << setprecision(15) << 6.8f << '\n';
6.80000019073486
root [1] cout << setprecision(20) << 6.8 << '\n';
6.7999999999999998224

You can store 0.5, 0.25, 0.125, … and multiples losslessly.

For more information, see
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html if you want to see all details
http://floating-point-gui.de/ if you just want an overview

As you can see in the cout above, Pepe’s solution does not solve your problem, 6.7999999999999998 is just “more equal” to 6.8 than 6.80000019. But then: how precise is the measurement the 6.8 comes from? Is the small uncertainty introduced by the Float_t or Double_t really relevant?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.