5 Replies - 420 Views - Last Post: 05 March 2011 - 11:01 AM Rate Topic: -----

#1 sreyan32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 sreyan32  Icon User is offline

  • New D.I.C Head

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

Re: Comparison between double and float

Posted 05 March 2011 - 09:49 AM

View PostAtli, 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???????
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6087
  • View blog
  • Posts: 23,597
  • Joined: 23-August 08

Re: Comparison between double and float

Posted 05 March 2011 - 09:55 AM

An interesting and pertinent read.
Was This Post Helpful? 2
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Comparison between double and float

Posted 05 March 2011 - 11:01 AM

View PostAtli, 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1