(slopeDegrees=90 type long double)
cout<<slopeDegrees<<endl;
if(slopeDegrees==90)cout<<"0"<<endl;
this code doesn't work ... the console print only 90 (its need to be 90 \n 0) why ? i use VS2010
(slopeDegrees=90 type long double)
cout<<slopeDegrees<<endl;
if(slopeDegrees==90)cout<<"0"<<endl;
this code doesn't work ... the console print only 90 (its need to be 90 \n 0) why ? i use VS2010
You try to compare 90 with a float. Since a floating point is now always exactly 90.0000 but can be 89.9998 or 90.0001. When comparing to another int or float value directly the comparison not be true.
This should not be done that way, but like:
if (Math.Abs(slopeDegrees - 90.0) < 0.001)
The 0.001 is some accuracy you can define yourself.
Comparing equality with doubles and other floating point types is frought with peril. 90 in double is an approximation, not exactly the integer 90.
Its better to compare against being within a threshold, not against exact inequality. Something like:
if (slopeDegrees > 89.9 && slopeDegrees < 90.1)
{
cout << "0" << endl;
}
If exact representations are needed of certain important values, you'll need to look into a fixed point way to represent these values.
Because you don't want to compare double
for exact equality. Instead test within a range. Ex:
const double THRESHOLD = 0.005;
double slopeDegrees = 90;
cout<<slopeDegrees<<endl;
if((slopeDegrees <= (90 + THRESHOLD)) && (slopeDegrees >= (90 - THRESHOLD)))
{
cout<<"0"<<endl;
}