0

Hi there,

I am stuck in a problem in which my function rounds off a decimal point number upto 4 decimal place and return a 0.0000 number. But when i compare this returned number to 0 it did not return true in if condition.

Please check my code below and tell me if something i am doing wrong. Its sounds simple but i don't know why that condition is skipping. Thanks !

#include <iostream>
#include <math.h>   //to use sin function
#include <iomanip>

using namespace std;

double funct(double x)
{
    double fx;

    fx = sin(x) - (5*x) + 2;

    cout<<fixed <<setprecision(4);

    return fx;
}

int main()
{
    double no;

    no = funct(0.495);
    cout<<"returned no = " <<no <<endl;

    if(no == 0)
        cout<<"\nzero";
    else
        cout<<"\nnot zero";

    return 0;
}
5
Contributors
6
Replies
36
Views
2 Years
Discussion Span
Last Post by rubberman
0

Please i need a simple justification i have searched alot but i did not understand. Thanks !

1

I'm not c++ buff, but it looks to me like you have set the precision for printing numbers, but that has no effect on the numbers themselves.

fx could still be 0.000001, you are just printing it to 4 decimal places

0

Line 25 from above could be

25 if(no < 0.00001) // close enough for us to call it zero.

As James noted, you printed it to 4 places but didn't do same for the numbers themselves. Your code, your choice on what to set the small number to or to change it to print zero, not zero and "really close to zero."

0

One way to handle your problem of comparing decimal values to see if they are equal could be ...

If your largest number to handle is (about) 99999.9999 ...
then if you multiply all decimals by 10000 ...
add 0.5 to round up
then truncate to an int
then note that 1000000000 will easily fit into a 32 bit int (on any 32 or 64 bit PC)
then when comparing numbers, you can just make easy int comparisons

Then divide these scaled up values ... i.e. divide each by 10000.0
to convert them back to a type double of the original scale.

But the example below shows a more common way to handle ...

// closeEnough.cpp //


#include <iostream>
#include <iomanip>
#include <cmath>   //to use sin function

using namespace std;


const double EPSILON = 5.00e-5;

bool closeEnough( const double test, const double cmp )
{
    return fabs( cmp - test ) < EPSILON ;
}


double fx( const double x )
{
    return sin(x) - 5*x + 2;
}




int main()
{
    double y = fx(0.495);

    cout << fixed << setprecision(4);
    cout<< "fx(0.495) = " << y << ' ';

    if( closeEnough( y, 0 ) ) cout << "'i.e. close enough to' zero";
    else cout << "i.e. NOT 'close enough to' zero";
}

Edited by David W

1

This is a well known problem with floating point numbers. Even if your software displays 0.0000, it is probably a truncated value that may really be 0.00001, hence the mismatch when compairing to 0. Also, 0 is an integer, not a floating point, value which just exacerbates the issue.

So, if you want to compare a float to integer 0, cast the float to an integer first.

Edited by rubberman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.