Why and how, does this program C show 7.21 for sure?

I am familiar with the fact that decimal fractions often do not convert accurately to binary, and therefore the approximation is preserved, and when converting back to decimal, a bit from the original number. I believe 0.21 will be an example of such a share.

How and why is this program compiled in gcc with an accuracy of 7.21?

And (an important second part of this question), why, given that it shows 7.21 accurately, does it show 837.21 inaccurate?

I can understand why this would show 0.21 inaccurate or any 21 digit inaccurate, since it takes a lot of bits to fit it in binary exactly, if at all. But I expect it to consistently show inaccurate show n.21, no matter what integer n

printf("%.100f\n", 7.21);  
printf("%.100f\n", 839.21); 
produces 7.210000000000000000000... 
839.2100000000000400000....

Any idea why she is doing one thing exactly and one thing is inaccurate? (on my gcc compiler anyway!)

if appropriate gcc --version shows gcc.exe (rubenvb-4.5.4) 4.5.4 Copyright (C) 2010 Free Software Foundation, Inc.

I talked to one person who said that he received both as inaccurate, which is more than what might be expected. So I did a screen showing behavior

enter image description here

Note. I notice that my implementation of gcc cygwin is not the latest version of cygwin gcc. I just did where gcc, she ran it from C:\Perl64\site\bin\gcc.exe, so she did not run cygwin alone. He probably controlled the old ming (which was what R suggested). chux is cygwin n GNU C11 (GCC) version 6.4.0 (i686-pc-cygwin) `.

+4
source share
2 answers

How and why is this program compiled in gcc with an accuracy of 7.21?

printf() .

printf("%.100f\n", x); x . C , , , DBL_DIG ( 15) - "" .

, , printf() , , DBL_DECIMAL_DIG ( 17). , . . . "" , , - . , OP. 17 , .

printf() double x = 839.21; x = 7.21; . :

839.2100000000000363797880709171295166015625...
839.210000000000000019984014443252817727625370025634765625.... (as long double)
vs OP's
839.2100000000000400000....

123.4567890123456789 // digit place

7.20999999999999996447286321199499070644378662109375...
7.210000000000000000034694469519536141888238489627838134765625000... (as long double)
7.210000000000000000000

OP printf() 16 .

7.210000000000000000000.... , printf() , . . @Eric Postpischil


. long double (research FLT_EVAL_METHOD), long double x86 .

Barlop

OP printf 7.21. OP printf 7.20999999999999996447286321199499070644378662109375, 7.210..., . , , OP printf , , 7.21 .

Eric correct, printf ( "%. 100f\n", 7.20999999999999996447286321199499070644378662109375); 7.210000000

, , 7.20999999999999996447286321199499070644378662109375, , - , .

, , C, . ( macOS.) Maple (math software), , . , , , , .

+4

.

, , 839.21

, printf cygwin gcc. R , , printf mingw, , Windows C/++. Chux , cygwin , cygwin .

- C, , . https://www.tutorialspoint.com/compile_c_online.php

, , - , 7.21 839.21 , , .

printf ( "%. 100f\n", 7.21);
printf ( "%. 100f\n", 839.21);

printf, .

printf("%f\n",7.21);//7.210000

printf("%f\n",839.21);//839.210000

,

printf("%.60f\n",7.21);

7.209999999999999964472863211994990706443786621093750000000000


printf("%.20a\n",7.21);  

0x1.cd70a3d70a3d70000000p+2


printf("%.60f\n",839.21);

839.210000000000036379788070917129516601562500000000000000000000


printf("%.20a\n",839.21);

0x1.a39ae147ae1480000000p+9

, , , .. , , 7.21, 839.21. , . , , . 7.21 8.3921. 839 , 1101000111. , 1.A , 1, nibble 1010, A. 7.21

, , 7.21 839.21

Chux , float % a. , float, float f; printf %.60f %.20a, printf.

printf("%.60f\n",f); //float f=7.21  
7.210000038146972656250000000000000000000000000000000000000000

printf("%.20a\n",f); //float f=7.21  
0x1.cd70a400000000000000p+2 <-- 7.21 in float, in hex.

printf("%.60f\n",f); //float f=839.21  
839.210021972656250000000000000000000000000000000000000000000000
0x1.a39ae200000000000000p+9 <-- 839.21  in float, in hex

, , Chux , , float % a.

, , ( ) cygwin, , double x=7.21; printf("%a\n", x); , , float. 0x1.cd70a4p+2

, % f , printf cygwin gcc.exe(rubenvb-4.5.4) 4.5.4 Copyright (C) 2010 Free Software Foundation, Inc. ( , ).

printf mingw printf, C/++ iostreams MinGW cygwin gcc, chux. ( cygwin, ).

0

Source: https://habr.com/ru/post/1689984/


All Articles