# comparison of floating point numbers

Page 1 of 1

## 3 Replies - 1995 Views - Last Post: 08 October 2010 - 10:56 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=194085&amp;s=8a00eb2045cf9da54f1b29fd705a2719&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 spsen16

Reputation: 0
• 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

• spelling expret

Reputation: 1676
• Posts: 5,836
• Joined: 03-August 09

## Re: comparison of floating point numbers

Posted 08 October 2010 - 09:36 AM

take a look at this

### #3 r.stiltskin

• D.I.C Lover

Reputation: 1833
• 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;
}

```

### #4 Salem_c

• void main'ers are DOOMED

Reputation: 2031
• Posts: 3,996
• 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