开发者

printf changing the value while using %.22f

I have a issue with basic typecasting.

#include<stdio.h>

int main()
{
     printf("%.22f",0.30);
     return 1;
}
开发者_如何转开发

The output I am getting is 0.2999999999999999888978

Why is 0.3 converted to a lesser value

Any help much appreciated

Nandish


This is because 0.30 cannot be exactly represented in binary floating-point. Internally, only an approximation to 0.30 can be stored. Therefore, when you print it all out, you'll get a slightly different number.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜