Problems with int and double variables

Hello everyone,

I am having a problem when I define a variable as a result of the sum and division of other ones. I have:

1
2
3
double Emin=0.01;
double Emax=400.;
double Delta=0.05;


Now, if I define the variable bin as being:

 
bin=(Emax-Emin)/Delta;


if bin is an int variable the result is 7997 but if I define bin as a double the result is 7998. I don't know where is the problem or how fix it?

Any idea,

Thanks in advance,

Manuel
The problem is that it is not 7998, but actually 7997.9999999999999999..., so it rounds down for integers and rounds up for doubles due to limited precision.
Last edited on
Topic archived. No new replies allowed.