双数据类型变量以6点精度而不是15位小数打印。 [英] double data type variable is getting printed with 6 point precision instead of 15 decimal points.

查看:89
本文介绍了双数据类型变量以6点精度而不是15位小数打印。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main(void){
    double n = 1.523652352;
    printf("%lf", n);

}





在打印时,输出为: -

1.523652

但是由于n是双数据类型,输出必须是: -

1.523652352000000或1.523652352

因为double有15位小数点精度。



为什么会这样?



In this while printing, the output is :-
1.523652
But as n is of double data type the output must be:-
1.523652352000000 or 1.523652352
because double has 15 decimal point precision.

Why is it so?

推荐答案

IEEE double有53个有效位 - 大约是15位小数数字,因为它必须向下舍入。这与始终打印15位数不同!



如果要打印尾随零,请使用:

An IEEE double has 53 significant bits - that's approximately 15 decimal digits because it has to round down. That's not the same as "always printing 15 digits"!

If you want to print the trailing zeros, use:
printf("%.15f", n);

看看会发生什么。

:doh:忘记格式说明符![/ edit]

And see what happens.
[edit]:doh: Forgot the format specifier![/edit]


那是因为 printf 默认使用小数点后6位数作为浮点数。如果您想要更多或更少的数字,可以使用格式说明符。请参阅 printf - C ++参考 [ ^ ]。



请注意,并非每个浮点值都可以表示究竟。因此,在打印许多有效数字时,打印的值可能与指定的值不同。
That is because printf uses by default 6 digits after the decimal point for floating point numbers. If you want more or less digits, you can use format specifiers. See printf - C++ Reference[^].

Note that not every floating point value can be represented exactly. As a result, the printed value may be not identical to the assigned value when printing many significant digits.


这篇关于双数据类型变量以6点精度而不是15位小数打印。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆