Click here to Skip to main content
15,745,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My doubt is :
we write
float a = 25.33f;

which is of course a normal way of initializing a float variable...

now we can also do the same thing in this way
float a = 25.33;

now my doubt is whether their is a difference between the above two statements as far as i know both are going to perform the same thing but in the first statement their is 'f' written at the end of 25.33 so what does that 'f' indicate i know it indicates that the data stored will be of floating type but the second statement also means the same so why do we write 'f' at the end of any float number

i tried this piece of code
void main()
float a= 0.7;
if (a < 0.7)

Output of the above program is c. Why? so
i googled it i found that 0.7 is a constant and the float 0.7 is rounded to 0.699999988 how it is rounded to 0.699999988 and in case of 0.8
a=0.800000011 and
constant 0.8 is 0.8000000000000000 whats the actual logic behind that, please if anyone can help me out i would be thankful

Thanks & Regards
Radix :rose:

floating point numbers on computers can represent numbers exactly that are sums of powers of two. You can get close to an arbitrary decimal number with so many binary bits but you might not be able to get it exactly.

So, if we're aiming to represent 0.7 in decimal in binary:

0.1 (where the . is the binary point) => 1 / 2 decimal => 0.5
0.11 => 3 / 4 decimal => 0.75
0.101 => 5 / 8 decimal => 0.625
0.1011 => 11 / 16 decimal => 0.6875
0.10111 => 23 / 32 decimal => 0.71875
0.101101 => 45 / 64 decimal => 0.703125


0.1011001101 => 717 / 1024 decimal => 0.7001953125 (10 bit encoding)

ad nauseaum until you run out of bits. Each 3 or 4 extra bits might give you another decimal point of accuracy so by the time you get to 20 or so bits you've only got 7 or so digits of accuracy.

(someone better at maths might like to prove it, I can't! :-) )


Share this answer
john_th 25-Jul-13 10:02am    
Great answer Aescleal!My 5!
radix3 wrote:
float a = 25.33;

This implicitly converts the double to a float.

radix3 wrote:
Output of the above program is c. Why?

Floating point numbers in C are not precise. Float is less precise than double.
Share this answer
CPallini 6-Jun-10 17:00pm    
Well, floating point numbers are not precise. It is not a C feature :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900