C printf 函数说明符

发布于 2025-01-08 19:21:55 字数 168 浏览 0 评论 0原文

我需要打印一个 16 位有效数字的浮点值。

现在,当我打印时,它会打印出

2.555556,

当我希望它打印出时: 2.555555555555556 /// 总共 16 位数字

我的 printf 说明符当前是:“%20.18lf”

想法?

I need to print out a float value with 16 significant digits.

Right now when I print, it prints out

i.e 2.555556

when I want it to print out: 2.555555555555556 /// A total of 16 digits

My printf specifier currently is : "%20.18lf"

ideas?

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

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

发布评论

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

评论(3

赢得她心 2025-01-15 19:21:55

如果值的类型为 float,则小数点后可能不会超过 7 位有效数字。如果它是 double 类型,则可以得到大约 15。根据系统的不同,long double 可能不会比 double 更精确;在我的系统中,小数点后大约有 17 位有效数字。

大多数浮点代码使用类型doublefloat 的精确度要低得多,而且通常速度也不会明显加快。

我有点惊讶你得到的是 2.555556 而不是像 2.555555582046508789 这样的 "%20.18lf" 格式。

顺便说一句,l(小写 L)不是必需的。 double 的格式为 "%f"printffloat 参数提升为 double,因此 "%f" 对于 floatdouble 都是正确的。 long double 的格式为"%Lf"

这是我编写的测试程序:

#include <stdio.h>
int main(void) {
    const float f        = 23.0/9.0;
    const double d       = 23.0/9.0;
    const long double ld = 23.0L/9.0L;
    printf("%20.18f\n%20.18f\n%20.18Lf\n", f, d, ld);
    return 0;
}

以及我在系统上得到的输出:

2.555555582046508789
2.555555555555555358
2.555555555555555556

If the value is of type float, you're probably not going to get more than about 7 significant digits after the decimal point. If it's of type double, you can get about 15. Depending on the system, long double may be no more precise than double; on mine, I get about 17 significant digits after the decimal point.

Most floating-point code uses type double; float is much less precise and often not significantly faster.

I'm a little surprised that you got 2.555556 rather than something like 2.555555582046508789 with a "%20.18lf" format.

Incidentally, the l (lowercase L) is not necessary. The format for double is "%f", and float arguments to printf are promoted to double, so "%f" is correct for both float and double. The format for long double is "%Lf".

Here's a test program I wrote:

#include <stdio.h>
int main(void) {
    const float f        = 23.0/9.0;
    const double d       = 23.0/9.0;
    const long double ld = 23.0L/9.0L;
    printf("%20.18f\n%20.18f\n%20.18Lf\n", f, d, ld);
    return 0;
}

and the output I got on my system:

2.555555582046508789
2.555555555555555358
2.555555555555555556
你在我安 2025-01-15 19:21:55

以下代码运行良好(您必须启用 C99,因为 %lf 说明符不是早期标准的一部分):

#include <stdio.h>

int main(void)
{
        volatile float v = 2.555555555555556;
        printf("%20.18lf\n", v);
        return 0;
}

打印以下内容:

$ gcc -std=c99 -Wall -pedantic -o test ./test.c 
$ ./test 
2.555555582046508789
$ 

如果值较短,例如 2.5,则会用零向右填充 - 2.500000000000000000

所以你在其他地方有错误。一般来说,您必须发布一个最小的自洽代码示例来重现问题。否则你就只能靠自己了。

The following code works well (you have to enable C99 as %lf specifier is not part of earlier standards):

#include <stdio.h>

int main(void)
{
        volatile float v = 2.555555555555556;
        printf("%20.18lf\n", v);
        return 0;
}

Prints the following:

$ gcc -std=c99 -Wall -pedantic -o test ./test.c 
$ ./test 
2.555555582046508789
$ 

If value is shorter, say 2.5, it right-pads it with zeros - 2.500000000000000000.

So you have an error elsewhere. Generally, you have to post a minimal self-consistent code example that reproduces the problem. Otherwise you are on your own.

唔猫 2025-01-15 19:21:55

C float或double不够精确,基本上你需要一个高精度的除法函数。这是我写的。它用于整数除法。您可能需要修改它以进行浮点除法。

int dividend=121, divisor=9;
int quotient,remainder;

//first print out the number before decimal point
quotient=dividend/divisor;
printf("%d", quotient);
remainder=dividend-quotient*divisor;

//print out decimal point
printf(".");

int digit_num=20;//this can be any precision you want

//second we calculate the number after decimal point
for(int i=0;i<digit_num;i++)
{
    remainder=remainder*10;
    quotient=remainder/divisor;
    remainder=remainder-quotient*divisor;
    printf("%d", quotient );
    dividend=remainder;
}

printf("\n");

printf("%d digits after decimal point has been printed out\n", digit_num );

C float or double is not precise enough, basically you need a high precision division function. Here is what I wrote. It is for integer division. You may need to revise it for float division.

int dividend=121, divisor=9;
int quotient,remainder;

//first print out the number before decimal point
quotient=dividend/divisor;
printf("%d", quotient);
remainder=dividend-quotient*divisor;

//print out decimal point
printf(".");

int digit_num=20;//this can be any precision you want

//second we calculate the number after decimal point
for(int i=0;i<digit_num;i++)
{
    remainder=remainder*10;
    quotient=remainder/divisor;
    remainder=remainder-quotient*divisor;
    printf("%d", quotient );
    dividend=remainder;
}

printf("\n");

printf("%d digits after decimal point has been printed out\n", digit_num );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文