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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5913102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:35:11  来源:igfitidea点击:

What is the difference between %g and %f in C?

cvariables

提问by Shashi Bhushan

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

我正在阅读 K&R 的 C 编程语言。这里在一个打印双变量的语句中写成

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.

其中 sum 被声明为 double。任何人都可以帮助我在双倍或浮动的情况下何时使用 %g 以及 %g 和 %f 之间有什么区别。

采纳答案by unwind

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.

f,F

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

克,克

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

回答by Daniel Nill

They are both examples of floating point input/output.

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

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

%g 和 %G 是科学记数法浮点数 %e 和 %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.

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

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

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

回答by TafT

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

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

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.

粗略地说,如果你更关心小数点后的细节,我会用 f 做,如果你想缩放大数字,用 g。从一些尘土飞扬的记忆中,如果您的数字打印表保持排列,那么 f 的值很小,但如果您的数字变大并且布局很重要,则需要像 g 这样的东西。当您的数字往往非常小或非常大但绝不会接近十时,e 会更有用。

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.

抱歉,这个答案很模糊,但这是一个主观的结果,只有在生成的字符数很重要或表示值的精度很重要时才会得到很难的答案。

回答by rancho

%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

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