开发者

printf changing the value while using %.22f

开发者 https://www.devze.com 2023-04-12 22:57 出处:网络
I have a issue with basic typecasting. #include<stdio.h> int main() { printf(\"%.22f\",0.30); return 1;

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

精彩评论

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

关注公众号