Can standard math functions handle infinity correctly?
int main() {
double inf = INFINITY;
double pi = acos(-1.0);
printf("[1]: %f %f\n", atan(inf) / pi, atan(-inf) / pi);
printf("[2]: %f %f\n", tan(inf) / pi, tan(-inf) / pi);
return 0;
}
outputs
[1]: 0.500000 -0.500000
[2]: -nan -nan
Is this behavior a specific standard? [2]
Is the behavior undefined? undefined?
I want to be sure what is at least [1]
a guaranteed result.
+3
source to share
1 answer
Both are well-defined behaviors. Quote from http://en.cppreference.com
-
a tan
If the argument is ± 0, it is returned unmodified.
If the argument is ± ∞, NaN is returned and FE_INVALID is increased.
If the argument is NaN, NaN is returned. -
Athan
If the argument is ± 0, it is returned unmodified.
If the argument is + ∞, + π / 2 is returned. If the argument is -∞, -π / 2 is returned.
If the argument is NaN, NaN is returned.
+4
source to share