Floats and Ints

Perhaps someone can explain this behaviour of ROOT to me.

I have a macro , which I am using to draw a TGraph. I use

[color=red]Float_t[/color] x[10], y[10];
for(Float_t i =0 ; i< 10; i++){
x[i] = 0.8 * i;
y[i] = 0.9;

Note the i is a float! There is no complaint from the intrepreter but when I try to draw a TGraph using
TGraph *gr = new TGraph(10, x, y);
I get a TGraph consisting of only 2 points (and not the ones I am expecting)/

I can print out the array contents in the loop and they are what they should be.

Now if you use
x[color=red[/color] i] = 0.8*i;
then it works perfectly.

Does anyone know what happens when you pass a float as an array index?


Hi Heather,
the float is losing its decimal digits, that’s what happens to a float converted to an int. So .99999998 becomes 0. That’s why people usually use ints as the array index :-] Try adding 0.5 to your float, which resembles rounding. If you still get the same result it might be a problem with cint.