我遇到了一些浮点数学的问题,我发现如果我在一行上进行数学运算,我会将-0传递给tan(),如果我在两行中执行,我会将0传递给tan ().看一看:
float theta = PI / 2.f;
float p = (PI / 2.f) - theta;
float result = tan(p);
Run Code Online (Sandbox Code Playgroud)
以上,p = -0,结果= -4.37 ......
float theta = PI / 2.f;
float p = PI / 2.f;
p -= theta;
float result = tan(p);
Run Code Online (Sandbox Code Playgroud)
以上,p = 0,结果= 0.
有人可以解释这个区别吗?我假设-0导致tan()的结果,虽然我在谷歌找不到任何解释原因的东西.为什么在不同的行上分布的完全相同的计算会得到不同的答案?
谢谢