C语言中%g和%f有什么区别?

发布于 2024-11-05 11:57:14 字数 186 浏览 0 评论 0原文

我正在学习 K&R 的《C 编程语言》。这里,在打印 double 变量的语句中

printf("\t%g\n", sum += atof(line));

,sum 被声明为 double。任何人都可以帮我解决在双精度或浮点情况下何时使用 %g 以及 %g 和 %f 之间的区别吗?

I was going through The C programming Language by K&R. Here in a statement to print a double variable it is written

printf("\t%g\n", sum += atof(line));

where sum is declared as double. Can anybody please help me out when to use %g in case of double or in case of float and whats the difference between %g and %f.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

沫离伤花 2024-11-12 11:57:14

它们都是浮点输入/输出的示例。

%g 和 %G 是科学记数法浮点数 %e 和 %E 的简化。

%g 将采用可以表示为 %f(简单浮点或双精度)或 %e(科学记数法)的数字,并将其返回为两者中较短的一个。

print 语句的输出将取决于 sum 的值。

They are both examples of floating point input/output.

%g and %G are simplifiers of the scientific notation floats %e and %E.

%g will take a number that could be represented as %f (a simple float or double) or %e (scientific notation) and return it as the shorter of the two.

The output of your print statement will depend on the value of sum.

荒路情人 2024-11-12 11:57:14

请参阅任何参考手册,例如手册页

f,F

双精度参数被四舍五入并转换为 [-]ddd.ddd 样式的十进制表示法,其中小数点字符后的位数等于精度规范。如果精度缺失,则取6;如果精度明确为零,则不会出现小数点字符。如果出现小数点,则其前面至少出现一位数字。
(SUSv2 不知道 F,并表示可以提供无穷大和 NaN 的字符串表示。C99 标准指定无穷大为“[-]inf”或“[-]infinity”,以及以“nan”开头的字符串' 对于 NaN,在 f 转换的情况下,以及 '[-]INF' 或 '[-]INFINITY' 或 'NAN*' 在 F 转换的情况下。)

g,G

双精度参数以 f 或 e 样式转换(或 G 转换为 F 或 E)。精度指定有效位数。如果精度缺失,则给出6位;如果精度为零,则将其视为 1。如果转换后的指数小于 -4 或大于或等于精度,则使用样式 e。从结果的小数部分中删除尾随零;仅当小数点后面至少跟着一位数字时,才会出现小数点。

See any reference manual, such as the man page:

f,F

The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it.
(The SUSv2 does not know about F and says that character string representations for infinity and NaN may be made available. The C99 standard specifies '[-]inf' or '[-]infinity' for infinity, and a string starting with 'nan' for NaN, in the case of f conversion, and '[-]INF' or '[-]INFINITY' or 'NAN*' in the case of F conversion.)

g,G

The double argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.

野の 2024-11-12 11:57:14

E = 指数表达式,简单来说就是幂(10, n)或10 ^ n

F = 分数表达式,默认6位精度

G = 一般表达式,以某种方式聪明地以简洁的方式显示数字(但是
真的吗?)

请参阅下面的示例,

代码

void main(int argc, char* argv[])  
{  
        double a = 4.5;
        printf("=>>>> below is the example for printf 4.5\n");
        printf("%%e %e\n",a);
        printf("%%f %f\n",a);
        printf("%%g %g\n",a);
        printf("%%E %E\n",a);
        printf("%%F %F\n",a);
        printf("%%G %G\n",a);
          
        double b = 1.79e308;
        printf("=>>>> below is the exbmple for printf 1.79*10^308\n");
        printf("%%e %e\n",b);
        printf("%%f %f\n",b);
        printf("%%g %g\n",b);
        printf("%%E %E\n",b);
        printf("%%F %F\n",b);
        printf("%%G %G\n",b);

        double d = 2.25074e-308;
        printf("=>>>> below is the example for printf 2.25074*10^-308\n");
        printf("%%e %e\n",d);
        printf("%%f %f\n",d);
        printf("%%g %g\n",d);
        printf("%%E %E\n",d);
        printf("%%F %F\n",d);
        printf("%%G %G\n",d);
}  

输出

=>>>> below is the example for printf 4.5
%e 4.500000e+00
%f 4.500000
%g 4.5
%E 4.500000E+00
%F 4.500000
%G 4.5
=>>>> below is the example for printf 1.79*10^308
%e 1.790000e+308
%f 178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583744.000000
%g 1.79e+308
%E 1.790000E+308
%F 178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583744.000000
%G 1.79E+308
=>>>> below is the example for printf 2.25074*10^-308
%e 2.250740e-308
%f 0.000000
%g 2.25074e-308
%E 2.250740E-308
%F 0.000000
%G 2.25074E-308

E = exponent expression, simply means power(10, n) or 10 ^ n

F = fraction expression, default 6 digits precision

G = gerneral expression, somehow smart to show the number in a concise way (but
really?)

See the below example,

The code

void main(int argc, char* argv[])  
{  
        double a = 4.5;
        printf("=>>>> below is the example for printf 4.5\n");
        printf("%%e %e\n",a);
        printf("%%f %f\n",a);
        printf("%%g %g\n",a);
        printf("%%E %E\n",a);
        printf("%%F %F\n",a);
        printf("%%G %G\n",a);
          
        double b = 1.79e308;
        printf("=>>>> below is the exbmple for printf 1.79*10^308\n");
        printf("%%e %e\n",b);
        printf("%%f %f\n",b);
        printf("%%g %g\n",b);
        printf("%%E %E\n",b);
        printf("%%F %F\n",b);
        printf("%%G %G\n",b);

        double d = 2.25074e-308;
        printf("=>>>> below is the example for printf 2.25074*10^-308\n");
        printf("%%e %e\n",d);
        printf("%%f %f\n",d);
        printf("%%g %g\n",d);
        printf("%%E %E\n",d);
        printf("%%F %F\n",d);
        printf("%%G %G\n",d);
}  

The output

=>>>> below is the example for printf 4.5
%e 4.500000e+00
%f 4.500000
%g 4.5
%E 4.500000E+00
%F 4.500000
%G 4.5
=>>>> below is the example for printf 1.79*10^308
%e 1.790000e+308
%f 178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583744.000000
%g 1.79e+308
%E 1.790000E+308
%F 178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583744.000000
%G 1.79E+308
=>>>> below is the example for printf 2.25074*10^-308
%e 2.250740e-308
%f 0.000000
%g 2.25074e-308
%E 2.250740E-308
%F 0.000000
%G 2.25074E-308
被翻牌 2024-11-12 11:57:14

正如 Unwind 指出的,f 和​​ g 提供不同的默认输出。

粗略地说,如果您更关心小数点后的细节,我会使用 f,如果您想缩放大数,请使用 g。从一些尘封的记忆中,如果你打印数字表格,因为一切都保持排列,那么 f 对于较小的值非常好,但如果你忍受数字变大并且布局很重要,则需要像 g 这样的东西。当您的数字往往非常小或非常大但永远不会接近 10 时,e 会更有用。

另一种方法是指定输出格式,以便每次都能获得相同数量的代表数字的字符。

对于这个模糊的答案感到抱歉,但这是一个主观的输出,只有在生成的字符数很重要或表示值的精度很重要的情况下才能得到硬答案。

As Unwind points out f and g provide different default outputs.

Roughly speaking if you care more about the details of what comes after the decimal point I would do with f and if you want to scale for large numbers go with g. From some dusty memories f is very nice with small values if your printing tables of numbers as everything stays lined up but something like g is needed if you stand a change of your numbers getting large and your layout matters. e is more useful when your numbers tend to be very small or very large but never near ten.

An alternative is to specify the output format so that you get the same number of characters representing your number every time.

Sorry for the woolly answer but it is a subjective out put thing that only gets hard answers if the number of characters generated is important or the precision of the represented value.

谁许谁一生繁华 2024-11-12 11:57:14

%g 删除浮点数中的尾随零,

打印(整数)最高 10**6 ,之后在 e+ 中最高精度 6

123456 给出 123456

1234567 给出 1.23457e+06

打印(float > 10** -4 )最高精度 6 ,之后四舍五入到预。 6

1.23456 给出 1.23456

1.234567 给出 1.23457

print (float < 10** -4 ) up to precision 4 ,否则在 ne-0p

0.0001 中给出 0.0001

0.000001 给出 1e-06

0.12345678 给出0.123457

%G 做同样的事情,但 exp(e) 变成 exp(E)

%g removes trailing zeros in floats,

prints (integer) upto 10**6 , after that in e+ upto precision 6

123456 gives 123456

1234567 gives 1.23457e+06

prints (float > 10** -4 ) upto precision 6 , after that rounds off to pre. 6

1.23456 gives 1.23456

1.234567 gives 1.23457

print (float < 10** -4 ) upto precision 4 , else in ne-0p

0.0001 gives 0.0001

0.000001 gives 1e-06

0.12345678 gives 0.123457

%G does the same , but exp(e) becomes exp(E)

停滞 2024-11-12 11:57:14

所有三个格式说明符 %e、%f 和 %g 用于处理 C 中的 float 和 double 数据类型。

%e 表示指数幂的数据(科学的格式)。

%f 表示正常小数形式的数据,最多六位小数,尽管您可以控制,但您希望输出最多有多少位小数。

%g 表示十进制格式并删除后续的零。

如何在 ac 代码中使用 %e、%f 和 %g 的示例:

#include <stdio.h>

int main() 
{ 
    double ans = 123.45; 
    
    printf("Printing using %%f %f\n",ans); 
    printf("Printing using %%e %e\n",ans); 
    printf("Printing using %%g %g\n\n\n",ans);
    // 
    
    
    ans = 123.45e8; // scientific way of writing 123.45 * 10^8
    
    printf("Printing using %%f %f\n",ans); 
    printf("Printing using %%e %e\n",ans); 
    printf("Printing using %%g %g\n\n\n",ans); 
    
    
    ans = 123.45e-8; // scientific way of writing 123.45 * 10^(-8) i,e. 0.0000012345
    
    // %f has upto 6 digits precision
    // 0.0000012345 converted to 0.000001 (max 6 precision allowed)
    printf("Printing using %%f %f\n",ans); 
    printf("Printing using %%e %e\n",ans); 
    printf("Printing using %%g %g\n",ans); 
    
    return 0;
}

输出将是:

Printing using %f 123.450000
Printing using %e 1.234500e+02
Printing using %g 123.45

Printing using %f 12345000000.000000
Printing using %e 1.234500e+10
Printing using %g 1.2345e+10

Printing using %f 0.000001
Printing using %e 1.234500e-06
Printing using %g 1.2345e-06

All the three format specifiers %e, %f and %g are used to work with float and double data types in C.

%e represents the data in exponential power(scientific format).

%f represents the data in normal decimal form, upto six decimal places, although you can control, that upto how many decimal places did you want your output.

%g represents the decimal format and removes succeeding zeros.

example how to use %e, %f and %g in a c code:

#include <stdio.h>

int main() 
{ 
    double ans = 123.45; 
    
    printf("Printing using %%f %f\n",ans); 
    printf("Printing using %%e %e\n",ans); 
    printf("Printing using %%g %g\n\n\n",ans);
    // 
    
    
    ans = 123.45e8; // scientific way of writing 123.45 * 10^8
    
    printf("Printing using %%f %f\n",ans); 
    printf("Printing using %%e %e\n",ans); 
    printf("Printing using %%g %g\n\n\n",ans); 
    
    
    ans = 123.45e-8; // scientific way of writing 123.45 * 10^(-8) i,e. 0.0000012345
    
    // %f has upto 6 digits precision
    // 0.0000012345 converted to 0.000001 (max 6 precision allowed)
    printf("Printing using %%f %f\n",ans); 
    printf("Printing using %%e %e\n",ans); 
    printf("Printing using %%g %g\n",ans); 
    
    return 0;
}

the output would be:

Printing using %f 123.450000
Printing using %e 1.234500e+02
Printing using %g 123.45

Printing using %f 12345000000.000000
Printing using %e 1.234500e+10
Printing using %g 1.2345e+10

Printing using %f 0.000001
Printing using %e 1.234500e-06
Printing using %g 1.2345e-06
雾里花 2024-11-12 11:57:14

%f 和 %g 做同样的事情。唯一的区别是 %g 是 %f 的缩写形式。也就是说%f中的小数点后的精度比%g中的要大

%f and %g does the same thing. Only difference is that %g is the shorter form of %f. That is the precision after decimal point is larger in %f compared to %g

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文