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

In summary, changing the declaration of a variable from float to double resolved an issue where a statement did not execute for a specific value (6.59) but did for others (2.59). This is due to the inaccuracy of floating point numbers and it is recommended to use a small margin of error instead of strict equality comparisons.
  • #1
Lord Anoobis
131
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
  • #2
It would help if you tell us what the exact value of call_time is in each case when the observed behavior occurs.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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?
 
  • #6
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.
 
  • #7
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
  • #8
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.
 

1. Why does using float or double in an if statement make a difference in C++?

C++ is a strongly-typed language, meaning that the data type of a variable must be explicitly declared. Float and double are both data types used for representing decimal numbers, but they have different sizes and precision. This means that when comparing float and double variables in an if statement, the results may differ.

2. How does using float or double affect the accuracy of an if statement in C++?

Float variables have a smaller size and lower precision compared to double variables. This means that float variables can only represent a limited range of decimal numbers, while double variables can represent a wider range with more precision. When using these variables in an if statement, the accuracy of the comparison may be affected.

3. Can I use either float or double in an if statement in C++?

Yes, you can use both float and double variables in an if statement in C++. However, it is important to understand the differences between these data types and how they may affect the results of the if statement.

4. How can I determine which data type to use in an if statement in C++?

The choice between using float or double in an if statement depends on the specific needs of your program. If you require a wider range of decimal numbers with more precision, then double may be a better choice. However, if memory usage is a concern, then using float may be more appropriate.

5. Are there any other factors to consider when using float or double in an if statement in C++?

Aside from size and precision, another important factor to consider is the potential loss of data when converting between float and double. If a float variable is compared to a double variable, the float value may be automatically converted to a double, potentially losing some precision. It is important to be aware of this when using these data types in an if statement.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
25
Views
14K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top