theta=atan2(0,0);
这个语句的输出是0,但是它是0/0的形式,那么它的输出怎么会是0,连维基百科都说应该是undefined,请解释一下为什么编译器给这个语句的输出是0。
最佳答案
what will be atan2 output for both x and y as 0
C 有 2 个相关规范。 @Oliver Charlesworth @Jonathan Leffler
The atan2 functions ... A domain error may occur if both arguments are zero. C11dr §7.12.4.4 2
Treatment of error conditions ...For all functions, a domain error occurs if an input argument is outside the domain over which the mathematical function is defined. ...; On a domain error, the function returns an implementation-defined value;... §7.12.1 3
atan2(0,0)
可能会导致 0.0, 1.0, INF, NAN
等。除了返回某些内容外,未指定其他内容。0
当然是一个合理的选择,但没有明确的数学上正确的结果 - 它没有定义。为了与 IEC-60559 兼容(尽管许多实现努力遵守,但 C 并不要求),以下结果是强制性的。请注意由于 ±0 引起的差异。
atan2(±0, −0) returns ±π
atan2(±0, +0) returns ±0.
关于 π 的注释. π 是一个无理数且都是有限的 double
是有理数,机器 pi 的返回值(与 π 最接近的可表示 double
)预计为 atan2(+0, -0)
.较短的版本
"what will be atan2 output for both x and y as 0"
零。
"how its output can be 0, even wikipedia says that it should be undefined"
维基百科没有定义 C 或各种数学库规范——它是一个引用——而不是一个规范。
"why compiler gives 0"
这是来自浮点标准 IEC-60559/IEEE 754 的指定响应经常被各种 C 实现使用。
关于c - x 和 y 的 atan2 输出为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47909048/