C语言中%g和%f有什么区别?
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它们都是浮点输入/输出的示例。
%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.
请参阅任何参考手册,例如手册页:
See any reference manual, such as the man page:
请参阅下面的示例,
代码
输出
See the below example,
The code
The output
正如 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.
%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)
所有三个格式说明符 %e、%f 和 %g 用于处理 C 中的 float 和 double 数据类型。
%e
表示指数幂的数据(科学的格式)。%f
表示正常小数形式的数据,最多六位小数,尽管您可以控制,但您希望输出最多有多少位小数。%g
表示十进制格式并删除后续的零。如何在 ac 代码中使用 %e、%f 和 %g 的示例:
输出将是:
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:
the output would be:
%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