Problem in my if a == b statement

Hi guys, this is my first post here. I am so used to writing PHP that sometimes when the syntax is different for another language I don't even know where to look :( maybe i'm just doing something dumb. This is my first time writing c++, this is a function I made in my header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  int ask(float test_num){
     //Declare vars
     float val;
     char chr;
     int ret;
     
     //User interation
     cout<< "Please type a number: ";
     cin>> val;
     cin.ignore();
  
     //If and output
     if(val == test_num){
           cout<< "You typed a number equal to "<< test_num << "\n";
     }else if(val > test_num){
           cout<< "You typed a number larger than "<< test_num << "\n";  
     }else if(val < test_num){
           cout<< "You typed a number less than "<< test_num << "\n";
     }
     
     //User interation
     cout<< "\nWould you like to try again (y/n): ";
     cin>> chr;
     cin.ignore();
     
     //If and output
     if(chr == 'y'){
         ret = 1;
     }else{
         ret = 0;     
     }
     
     //Return
     return ret;
}


The part that says '//if and output' is the part that is not working. When val is tested against test_num it returns lower than (<) if it is equal. The greater than and lower than work fine, but if it is the same (==) I would expect it to return the correct cout<<

Any idea's where I'm going wrong, thanks a lot for your help!
Floating point values are approximations. Direct comparisons (==, !=) will almost never work. This is due to the nature of how floating points are represented in the hardware (1.5 might not actually be 1.5, it might be 1.499999999999 or something)
To test if doubles are (pretty much) equal, you'll need to code a little function which tests that the difference between them is less than some suitably small value (epsilon?).
@Disch and @andywestken Thanks I did wonder that :)

Andy, what you said about using a function to test if doubles are pretty much equal, couldn't that be done with a float as well..?

In all honesty (This is embarrassing) I actually have never bothered with doubles in any programs iv'e written in the past, I have always used floats, I must have just never test wether a float was == to a float. As I said I mainly work with PHP and data types aren't exactly an issue there.

So what is the difference between a double and a float? Are doubles just to less significant figures? God I sound so noobish.
A double is twice as big as a float in storage. Other than that they are pretty much the same though.
Unless you have limited memory, or need very, very large arrays, I would suggest you use double rather than float. Esp. when you are doing maths with them.

While it is not always the case, I believe that many compilers promote floats to doubles to do the calculation, and then back to float. I am pretty sure this the case for Intel based systems! So when you write

float a = 1.0f;
float b = 2.0f;
float c = a * b;

The code is actually

float c = (float)((double)a * (double)b);

Which means you calling 3 cast operators in addition to the 2 mathematical operators.

(Though in real life, this example will end up being pretty much optimized away)

Andy

P.S. I think that Visual C++ allows you to control this behaviour. But the only people I've encountered who use it are games programmers (for that bit extra speed, maybe?).
Topic archived. No new replies allowed.