Build histogram with binwidth = 0.1

Hello.
I’m trying to draw histogram with 0.1 bin width.

This works correct:

double hdata[10]={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9};
TH1F *h2 = new TH1F("h","h",10,0,1);       
 for(int i = 0; i<10; i++){
        h2->Fill(hdata[i]);
 }
        h2->Draw();
}

but with same data values here it fails:

{
double hdata[10];
for(int i = 0; i<10; i++){
if(i==0)hdata[i]=0;
else hdata[i]=hdata[i-1]+0.1;
}
        TH1F *h2 = new TH1F("h","h",10,0,1);       
        for(int i = 0; i<10; i++){
        h2->Fill(hdata[i]);
        }
        h2->Draw();
}

Why?

Google for “floating point rounding error”, “round-off error”, “floating point arithmetic precision”, and so on.

You could try: TH1F *h2 = new TH1F("h", "h", 10, 0, 0.999999);

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