3 Replies - 1762 Views - Last Post: 08 October 2010 - 10:56 AM Rate Topic: -----

#1 spsen16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 10

comparison of floating point numbers

Posted 08 October 2010 - 09:13 AM

//code

int testFunc(double value)

double test = 0.0;

if(val<=test)
std::cout<<"Both are Equal"<<std::endl;


///

The if loop may result to true if value ==0 but comparison of floating point values are not proper and violate the coding standard.
How can I carry out such a comparison??????????

Regards,
Sanjib
Is This A Good Question/Topic? 0
  • +

Replies To: comparison of floating point numbers

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: comparison of floating point numbers

Posted 08 October 2010 - 09:36 AM

take a look at this
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: comparison of floating point numbers

Posted 08 October 2010 - 09:41 AM

Decide how much of an error (rounding error) you are willing to accept -- let's call it epsilon -- and then you can do something like this:

int testFunc(double value) {

    double test = 0.0, epsilon = 0.00001;

    if(value < test + epsilon && value > test - epsilon)
        std::cout<<"Both are Equal"<<std::endl;
}


Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1635
  • View blog
  • Posts: 3,111
  • Joined: 30-May 10

Re: comparison of floating point numbers

Posted 08 October 2010 - 10:56 AM

More duplicated effort

http://cboard.cprogr...int-values.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1