Comp Sci C++, float and double having different effects on an if statement?

AI Thread Summary
The discussion centers on the behavior of float and double data types in C++, particularly regarding conditional statements. A specific example showed that a float variable did not execute as expected for the value 6.59, while changing it to a double resolved the issue. This inconsistency is attributed to how floating-point numbers are stored in binary, leading to precision errors. It is advised to avoid strict equality checks with floats and to use a small tolerance when comparing floating-point values. The conversation emphasizes the importance of understanding numerical representation in programming to prevent similar issues in the future.
Lord Anoobis
Messages
131
Reaction score
22
Homework Statement
Write a program that asks for the starting time and the number of minutes of the call,
and... pretty straightforward
Relevant Equations
None. Checked with both MS Visual Studio and DEV C++.
So I used this statement,
C++:
if (call_time > 0 && call_time <= 6.59)
where the variable was declared as float. What is strange is that it didn't execute for call_time = 6.59, but for some other values, like
C++:
if (call_time > 0 && call_time <= 2.59)
the end value of the range does not cause any issues. However, when I changed the declaration to type double, the issue disappeared entirely. Why is this happening?
 
Physics news on Phys.org
It would help if you tell us what the exact value of call_time is in each case when the observed behavior occurs.
 
Checking for strict equality in floats is a bad idea. Doubles only slightly better. The various "6.59"'s in the problem may not be strictly equal.
 
FactChecker said:
It would help if you tell us what the exact value of call_time is in each case when the observed behavior occurs.
I noted it for 1.59, 3.59, 5.59 and 6.59 where the given range in the problem is from 0 to 6.59.
 
Vanadium 50 said:
Checking for strict equality in floats is a bad idea. Doubles only slightly better. The various "6.59"'s in the problem may not be strictly equal.
Is this to do with the way the numbers are stored in memory? Also, how would I go about avoiding this in future?
 
Lord Anoobis said:
I noted it for 1.59, 3.59, 5.59 and 6.59 where the given range in the problem is from 0 to 6.59.
We need more accuracy than that for the value of call_time. When you say 1.59, do you mean 1.59000 or 1.59100? What exactly is it? As a general rule, when your algorithm is depending on the equality of floats, it is best to give the program a little leeway for how it stores the fractional part. Use call_time < 2.59001 if that would not cause a problem with what you want it to do.
 
Lord Anoobis said:
Is this to do with the way the numbers are stored in memory? Also, how would I go about avoiding this in future?
Yes.

I've been explaining for about 5 decades now why the test "is 1.4 + 1.6 equal to 2.0" fails in computers (early on it always failed, now it depends on the data type).

Data in computers is stored in BINARY, not decimal so what YOU see as 1.4 or 1.6, the computer may see as 1.39999998 or 1.60000000000001 for example

As @Vanadium 50 said, do NOT use exact comparisons for non-integer numbers. Do something like take the ABS of the difference and check that it is less than some small amount.
 
  • Like
Likes Vanadium 50 and gneill
You have

call_time <= 6.59

If you replaced it with

call_time < 6.59

would it matter? If so, your code is in trouble.
 

Similar threads

Replies
3
Views
1K
Replies
12
Views
2K
Replies
6
Views
3K
Replies
23
Views
3K
Replies
3
Views
3K
Replies
6
Views
4K
Replies
19
Views
3K
Replies
3
Views
2K
Back
Top