printf在使用%.22f时更改值

Nan*_*h A 2 c c++ type-conversion

我有基本类型转换的问题.

#include<stdio.h>

int main()
{
     printf("%.22f",0.30);
     return 1;
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出是0.2999999999999999888978

为什么0.3转换为较小的值

任何帮助非常感谢

Nandish

Mys*_*ial 9

这是因为0.30无法在二进制浮点中精确表示.在内部,只能0.30存储近似值.因此,当你全部打印出来时,你会得到一个略有不同的数字.

http://en.wikipedia.org/wiki/Floating_point