[英]what is -0.0000 in c when using floats and double?
#include <stdio.h>
#include <stdlib.h>
#define answer 3.141593
void main(int argc, char **argv) {
float a = (argc - 2)?: strtod(argv[1], 0);
printf("double = %lf ,float = %f", a-answer , a-answer);
}
when I run it like that: 当我这样运行时:
./a.out 3.141593
the output is 输出是
double = -0.000000 ,float = -0.000000
Why does it -0.00000
? 为什么它是
-0.00000
? how can I make it output 0.000000
? 如何使它输出
0.000000
?
How can I make a == answer
? 我如何做出
a == answer
?
How comes there is -0 value if it uses 2's complement? 如果使用2的补码,怎么会有-0值?
Floating point numbers doesn't use 2's complement. 浮点数不使用2的补码。 They have sign, exponent and mantissa and your numbers have just zero with sign, or more probably, you have some number like -1.0e-15, which is printed as -0.0000.
它们具有正负号,指数和尾数,并且您的数字正负号只有零,或者更可能的是,您具有-1.0e-15之类的数字,其打印为-0.0000。 try %e instead of %f.
尝试%e而不是%f。 The small difference is made by inability to store numbers with infinte precission in finite precission data type (some rounding occured) and when you change double to float, additional rounding have to occur.
微小的差异是由于无法将有限整数的数字存储在有限精度数据类型中(发生了一些舍入),并且当您将double更改为float时,必须进行其他舍入。 (take in mind, that 3.141593 is infinite periodic number in binary representation, this it really depends on in which type is this number stored)
(请记住,3.141593是二进制表示中的无限周期数,这实际上取决于存储此数字的类型)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.