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:
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";
}elseif(val > test_num){
cout<< "You typed a number larger than "<< test_num << "\n";
}elseif(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.
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?).