cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Problems with int and double variables
Problems with int and double variables
Mar 20, 2014 at 2:59pm UTC
ca2004
(1)
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
Mar 20, 2014 at 4:18pm UTC
LB
(13399)
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
Mar 20, 2014 at 4:18pm UTC
Topic archived. No new replies allowed.