为什么C或C++ “ -0”不产生浮点− 0?
我正在尝试收集一些边缘案例以进行浮点算术。问题是,我尝试printf
没有向我展示我想要的东西:
#include <cmath>
#include <stdio.h>
#include <complex>
int main() {
double x = -0;
auto y = sqrt(x);
printf("x %f y %f \n", x, y);
return 1;
}
per ieee,squareroot(-0)
is is
-0
,但这将把x和y都打印为0。
关于如何实现自己想要的东西的任何建议?是通过编译器标志还是不同的东西?
I am trying to collect some edge cases for floating-point arithmetic. The thing is, my attempt with printf
is not showing me what I want:
#include <cmath>
#include <stdio.h>
#include <complex>
int main() {
double x = -0;
auto y = sqrt(x);
printf("x %f y %f \n", x, y);
return 1;
}
Per IEEE, squareRoot(-0)
is -0
, but this will print out both x and y to be 0.
Any suggestions on how I can achieve what I want? Would it be through compiler flags or something different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
0
是一个整数常数,因此-0
也是一个仍然是0
的整数。要使用浮点常数获得负零。
0
is an integer constant, so-0
is also an integer which is still0
.To get a negative zero, using a floating point constant.