# Comparison between double and float

Page 1 of 1

## 5 Replies - 537 Views - Last Post: 05 March 2011 - 11:01 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=220331&amp;s=e54fa42d3a703e02220eaba26effdc36&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sreyan32

Reputation: 0
• Posts: 40
• Joined: 15-December 10

# Comparison between double and float

Posted 05 March 2011 - 09:44 AM

I am using Dev C++ IDE.
the following if block executes to true I need ot know why???
float c=0.6;
if(c>0.6)
printf("A");
else printf("b");
----------------------------
Output=A
Plz i need to know why the if condition is true.I already know that any decimal constant is treated as a double in C but how does that apply here?????
Is This A Good Question/Topic? 0

## Replies To: Comparison between double and float

### #2 Atli

Reputation: 3885
• Posts: 6,453
• Joined: 08-June 10

## Re: Comparison between double and float

Posted 05 March 2011 - 09:48 AM

Try adding a f behind the number you want compared, to cast it to float.

```if (c > 0.6f)
```

This post has been edited by Atli: 05 March 2011 - 09:50 AM

### #3 sreyan32

Reputation: 0
• Posts: 40
• Joined: 15-December 10

## Re: Comparison between double and float

Posted 05 March 2011 - 09:49 AM

Atli, on 05 March 2011 - 09:48 AM, said:

Try adding a f behind the number you want compared, to cast it to float.

```if (c > 0.6f)
```

I already know that I want to know why float is being created greater than double???????

• Saucy!

Reputation: 6207
• Posts: 23,952
• Joined: 23-August 08

## Re: Comparison between double and float

Posted 05 March 2011 - 09:55 AM

### #5 Atli

Reputation: 3885
• Posts: 6,453
• Joined: 08-June 10

## Re: Comparison between double and float

Posted 05 March 2011 - 09:57 AM

O, sorry. So used to people asking for a solution rather than a reason

I'm not 100% sure why it's evaluated as higher (math is not my thing), but I suspect it has to do with how the fractions are rounded. Floating point values are not 100% precise -- something to do with the way they are stored (see here) -- so I can only assume that 0.6 as a double is stored as fractionally larger than the 0.6 float.

In practice it is generally not a good idea to assume floating point numbers of different types are accurate to the point that == will evaluate as true. Better to deal in ranges when that is the case.

### #6 David W

• DIC supporter

Reputation: 281
• Posts: 1,791
• Joined: 20-September 08

## Re: Comparison between double and float

Posted 05 March 2011 - 11:01 AM

Atli, on 05 March 2011 - 11:57 AM, said:

O, sorry. So used to people asking for a solution rather than a reason

I'm not 100% sure why it's evaluated as higher (math is not my thing), but I suspect it has to do with how the fractions are rounded. Floating point values are not 100% precise -- something to do with the way they are stored (see here) -- so I can only assume that 0.6 as a double is stored as fractionally larger than the 0.6 float.

In practice it is generally not a good idea to assume floating point numbers of different types are accurate to the point that == will evaluate as true. Better to deal in ranges when that is the case.

The issue here isn't really about double vs float, although double storage will certainly permit much greater precision... As both the above provided links point out, the problem all has to do with the fact that numbers are stored as binary bits of code. If the memory for a number is, say 32 bits, how many different numbers can be coded? As long as the numbers you are using are integers, within the range allowed by the number of bits to hold those numbers, exact comparison of numbers is ok. But if the number is a decimal approximation to a certain real value, numbers can only be compared up to the limits of that approximation.
You may think of this, similarly to a scientific measurement to so many significant digits ... Say the limits of precision of measurement were 3 sig.figures.
Now you make measurements ... (estimated as)
1. 7.355
2. 7.357
3. 7.345
...

The first 2 above are really the same ... 7.36 +- 0.005
The 3rd, 7.35, you can only say was about 0.01+- 0.005 smaller

This post has been edited by David W: 05 March 2011 - 11:22 AM