Author 
Message 
Lim Hyoung Ta #1 / 18

What's happen in float!!
#include main(){ float number = 54321; float place; place = 9 / 10.0; number += place; printf("place = %f \\n", place); printf("number = %f \\n", number); Quote: }
I think that this answer is 54321.900000 but result is 54321.898438 what's happen in float variable?

Wed, 24 Nov 2004 05:26:21 GMT 


Dave Vandervi #2 / 18

What's happen in float!!
Quote: >#include >main(){ >float number = 54321; >float place; >place = 9 / 10.0; >number += place; >printf("place = %f \\n", place); >printf("number = %f \\n", number); >} >I think that this answer is 54321.900000 >but result is 54321.898438 >what's happen in float variable?
This is question 14.1 in the FAQ[1]. Short answer: You're running out of precision. dave [1] http://www.eskimo.com/~scs/Cfaq/top.html . Read the whole thing. 
Quote: > Sorry for this thread,
We all are. Stefan Farfeleder and Richard Heathfield in comp.lang.c

Wed, 24 Nov 2004 05:40:59 GMT 


Mark McIntyr #3 / 18

What's happen in float!!
Quote: int main() is correct C. Quote: >float number = 54321; >float place; >place = 9 / 10.0; >number += place; >printf("place = %f \\n", place); >printf("number = %f \\n", number);
return 0; or something to be correct C. Quote: >} >I think that this answer is 54321.900000 >but result is 54321.898438 >what's happen in float variable?
This is a FAQ. Hint: can you store 5431.9 precisely in floating point binary format?  Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/Cfaq/top.html>

Wed, 24 Nov 2004 06:09:40 GMT 


Default Use #4 / 18

What's happen in float!!
Quote:
> I think that this answer is 54321.900000 > but result is 54321.898438 > what's happen in float variable?
Please read the FAQ, in particular: 14.1 When I set a float variable to 3.1, why is printf printing it as 3.0999999? Brian Rodenborn

Wed, 24 Nov 2004 06:28:13 GMT 


Lawrence Kir #5 / 18

What's happen in float!!
On 7 Jun, in article
Quote: >#include
#include <stdio.h> Quote: >main(){
To be consistent with C99 and good programming practice make that int main(void){ Quote: >float number = 54321; >float place; >place = 9 / 10.0;
Here you are performing a division using double arithmetic and then converting the result to float precision, which is a bit odd. Consider place = 9 / 10.0F; Quote: >number += place; >printf("place = %f \\n", place); >printf("number = %f \\n", number);
I assume you mean \n rather than \\n. return 0; Quote: >} >I think that this answer is 54321.900000 >but result is 54321.898438 >what's happen in float variable?
Floating point types have finite precision. The most common float format represents values in binary so cannot represent most decimal fractions exactly. It also gives you approximately 7 decimal significant digits worth of precision. The value you displayed is off by less than 2 parts in the 8th digit so is well within the expected error bounds of the correct result. The main problem is that you are trying to display more digits than than are available with flost precision. Try printf("number = %.2f\n", number); which will display to 2 decimal places giving 7 overall digits in this case.  


Thu, 25 Nov 2004 18:18:11 GMT 


Dan P #6 / 18

What's happen in float!!
Quote: >Floating point types have finite precision. The most common float format >represents values in binary so cannot represent most decimal fractions >exactly. It also gives you approximately 7 decimal significant digits >worth of precision. The value you displayed is off by less than 2 parts >in the 8th digit so is well within the expected error bounds of the >correct result. ^^^^^^
^^^^^^^^^^^^^^ Of the *mathematically* correct result. The value he has displayed is *the* correct result, in context. Dan  Dan Pop DESY Zeuthen, RZ group

Fri, 26 Nov 2004 20:32:50 GMT 


Lawrence Kir #7 / 18

What's happen in float!!
Quote:
> (Lawrence Kirby) writes: >>Floating point types have finite precision. The most common float format >>represents values in binary so cannot represent most decimal fractions >>exactly. It also gives you approximately 7 decimal significant digits >>worth of precision. The value you displayed is off by less than 2 parts >>in the 8th digit so is well within the expected error bounds of the >>correct result. ^^^^^^ > ^^^^^^^^^^^^^^ >Of the *mathematically* correct result. The value he has displayed is >*the* correct result, in context.
Right.  


Sat, 27 Nov 2004 07:10:50 GMT 


Andrew Simmon #8 / 18

What's happen in float!!
 Quote:
> ^^^^^^^^^^^^^^ > The value he has displayed is *the* correct result, in context.
Does your use of the word "the" in "*the* correct result" imply that all conforming implementations have to print the string "54321.898438" as a result of running the OP's program?

Sun, 28 Nov 2004 15:39:13 GMT 


Mint #9 / 18

What's happen in float!!
Quote:
> On 7 Jun, in article
> >#include > #include <stdio.h> > >main(){ > To be consistent with C99 and good programming practice make that > int main(void){ > >float number = 54321; > >float place; > >place = 9 / 10.0; > Here you are performing a division using double arithmetic and then > converting the result to float precision, which is a bit odd. Consider > place = 9 / 10.0F; > >number += place; > >printf("place = %f \\n", place); > >printf("number = %f \\n", number); > I assume you mean \n rather than \\n. > return 0; > >} > >I think that this answer is 54321.900000 > >but result is 54321.898438 > >what's happen in float variable? > Floating point types have finite precision. The most common float format > represents values in binary so cannot represent most decimal fractions > exactly. It also gives you approximately 7 decimal significant digits > worth of precision. The value you displayed is off by less than 2 parts > in the 8th digit so is well within the expected error bounds of the > correct result.
Or even 1 digit at the 7th digit. Quote: > The main problem is that you are trying to display more digits than > than are available with flost precision. Try > printf("number = %.2f\n", number); > which will display to 2 decimal places giving 7 overall digits in this > case.
Just out of curiosity how does the standard define this printf("Number = %.22234343f\n", number); Specifically what are the limit of asking for precision.

Sun, 28 Nov 2004 22:33:24 GMT 


Mint #10 / 18

What's happen in float!!
Quote:
> On 7 Jun, in article
> >#include > #include <stdio.h> > >main(){ > To be consistent with C99 and good programming practice make that > int main(void){ > >float number = 54321; > >float place; > >place = 9 / 10.0; > Here you are performing a division using double arithmetic and then > converting the result to float precision, which is a bit odd. Consider > place = 9 / 10.0F; > >number += place; > >printf("place = %f \\n", place); > >printf("number = %f \\n", number); > I assume you mean \n rather than \\n. > return 0; > >} > >I think that this answer is 54321.900000 > >but result is 54321.898438 > >what's happen in float variable? > Floating point types have finite precision. The most common float format > represents values in binary so cannot represent most decimal fractions > exactly. It also gives you approximately 7 decimal significant digits > worth of precision. The value you displayed is off by less than 2 parts > in the 8th digit so is well within the expected error bounds of the > correct result.
Or even 1 digit at the 7th digit. Quote: > The main problem is that you are trying to display more digits than > than are available with flost precision. Try > printf("number = %.2f\n", number); > which will display to 2 decimal places giving 7 overall digits in this > case.
Just out of curiosity how does the standard define this printf("Number = %.22234343f\n", number); Specifically what are the limit of asking for precision.

Sun, 28 Nov 2004 22:33:25 GMT 


Mint #11 / 18

What's happen in float!!
Quote:
> On 7 Jun, in article
> >#include > #include <stdio.h> > >main(){ > To be consistent with C99 and good programming practice make that > int main(void){ > >float number = 54321; > >float place; > >place = 9 / 10.0; > Here you are performing a division using double arithmetic and then > converting the result to float precision, which is a bit odd. Consider > place = 9 / 10.0F; > >number += place; > >printf("place = %f \\n", place); > >printf("number = %f \\n", number); > I assume you mean \n rather than \\n. > return 0; > >} > >I think that this answer is 54321.900000 > >but result is 54321.898438 > >what's happen in float variable? > Floating point types have finite precision. The most common float format > represents values in binary so cannot represent most decimal fractions > exactly. It also gives you approximately 7 decimal significant digits > worth of precision. The value you displayed is off by less than 2 parts > in the 8th digit so is well within the expected error bounds of the > correct result.
Or even 1 digit at the 7th digit. Quote: > The main problem is that you are trying to display more digits than > than are available with flost precision. Try > printf("number = %.2f\n", number); > which will display to 2 decimal places giving 7 overall digits in this > case.
Just out of curiosity how does the standard define this printf("Number = %.22234343f\n", number); Specifically what are the limit of asking for precision.

Sun, 28 Nov 2004 22:33:43 GMT 


Mint #12 / 18

What's happen in float!!
Quote:
> On 7 Jun, in article
> >#include > #include <stdio.h> > >main(){ > To be consistent with C99 and good programming practice make that > int main(void){ > >float number = 54321; > >float place; > >place = 9 / 10.0; > Here you are performing a division using double arithmetic and then > converting the result to float precision, which is a bit odd. Consider > place = 9 / 10.0F; > >number += place; > >printf("place = %f \\n", place); > >printf("number = %f \\n", number); > I assume you mean \n rather than \\n. > return 0; > >} > >I think that this answer is 54321.900000 > >but result is 54321.898438 > >what's happen in float variable? > Floating point types have finite precision. The most common float format > represents values in binary so cannot represent most decimal fractions > exactly. It also gives you approximately 7 decimal significant digits > worth of precision. The value you displayed is off by less than 2 parts > in the 8th digit so is well within the expected error bounds of the > correct result.
Or even 1 digit at the 7th digit. Quote: > The main problem is that you are trying to display more digits than > than are available with flost precision. Try > printf("number = %.2f\n", number); > which will display to 2 decimal places giving 7 overall digits in this > case.
Just out of curiosity how does the standard define this printf("Number = %.22234343f\n", number); Specifically what is the limit of asking for precision.

Sun, 28 Nov 2004 22:43:25 GMT 


John Bo #13 / 18

What's happen in float!!
Quote: > #include > main(){ > float number = 54321; > float place; > place = 9 / 10.0; > number += place; > printf("place = %f \\n", place); > printf("number = %f \\n", number); > } > I think that this answer is 54321.900000 > but result is 54321.898438 > what's happen in float variable?
On most systems, floats are stored as a combination of a fraction and an exponent, sort of like scientific notation. For a value like 3.0, the encoding would (usually) be .75 * 2^2, so what actually gets stored is a binary representation of .75 (1/2 + 1/4, or 1100...), along with a binary representation of the exponent (the exact details of how the fraction and the exponent are encoded vary from system to system, but this is generally true AFAIK  if I'm wrong I'm sure Dan Pop will correct me ;). So, how would 54321.9 be stored? First, Divide 54321.9 by successive powers of 2 until you get a mantissa below 1.0: 54321.9 = 27160.95 x 2^1 = 13580.475 x 2^2 = 6790.2375 x 2^3 ~ 0.828886414 x 2^16 Second, try to represent 0.828886414 as a sum of powers of 2 (1/2 + 1/4 + 1/8 + ...). This should take at least 53 terms (which is where I gave up), corresponding to 53 bits. The float type typically doesn't have that many bits available for the mantissa (on most systems I've used, the float type has about 24 bits available for the mantissa), meaning that the value that actually gets stored is close to, but not exactly, 54321.9. Add up those first 24 bits, multiply by 2^16, and you'll get the value you got above: 54321.898438. M{*filter*}of the story: most floating point values cannot be represented exactly. IIRC, you can only trust the first 68 significant digits of a float to be correct; for doubles, you can trust the first 15 or so significant digits. Use double for everything, unless you're *really* tight on memory.

Sun, 28 Nov 2004 23:46:51 GMT 


John Bo #14 / 18

What's happen in float!!
Quote: > #include > main(){ > float number = 54321; > float place; > place = 9 / 10.0; > number += place; > printf("place = %f \\n", place); > printf("number = %f \\n", number); > } > I think that this answer is 54321.900000 > but result is 54321.898438 > what's happen in float variable?
On most systems, floats are stored as a combination of a fraction and an exponent, sort of like scientific notation. For a value like 3.0, the encoding would (usually) be .75 * 2^2, so what actually gets stored is a binary representation of .75 (1/2 + 1/4, or 1100...), along with a binary representation of the exponent (the exact details of how the fraction and the exponent are encoded vary from system to system, but this is generally true AFAIK  if I'm wrong I'm sure Dan Pop will correct me ;). So, how would 54321.9 be stored? First, Divide 54321.9 by successive powers of 2 until you get a mantissa below 1.0: 54321.9 = 27160.95 x 2^1 = 13580.475 x 2^2 = 6790.2375 x 2^3 ~ 0.828886414 x 2^16 Second, try to represent 0.828886414 as a sum of powers of 2 (1/2 + 1/4 + 1/8 + ...). This should take at least 53 terms (which is where I gave up), corresponding to 53 bits. The float type typically doesn't have that many bits available for the mantissa (on most systems I've used, the float type has about 24 bits available for the mantissa), meaning that the value that actually gets stored is close to, but not exactly, 54321.9. Add up those first 24 bits, multiply by 2^16, and you'll get the value you got above: 54321.898438. M{*filter*}of the story: most floating point values cannot be represented exactly. IIRC, you can only trust the first 68 significant digits of a float to be correct; for doubles, you can trust the first 15 or so significant digits. Use double for everything, unless you're *really* tight on memory.

Sun, 28 Nov 2004 23:47:12 GMT 


Dan P #15 / 18

What's happen in float!!
Quote: >
>> ^^^^^^^^^^^^^^ >> The value he has displayed is *the* correct result, in context. > ^^^^^^^^^^ >Does your use of the word "the" in "*the* correct result" imply that all >conforming implementations have to print the string "54321.898438" as a >result of running the OP's program?
Nope: it means that for his particular conforming implementation it was the correct result. This was the purpose of the "in context" qualifier, appended to my statement. Dan  Dan Pop DESY Zeuthen, RZ group

Sun, 28 Nov 2004 23:52:19 GMT 

