问题
Are cmath exp()
and log()
functions always symmetrical?
Ergo if I do
double x;
double y = exp(log(x));
assert(x == y);
will the assert ever fail, and in that case: under what circumstances? We can assume that x
is a rational number > 0.
回答1:
A floating-point log
cannot be one-to-one. It needs to be monotone increasing and satisfy log(64) > 4.15
and log(128) < 4.86
. There are 252double
s between 64 and 128, but there are fewer than 250double
s between 4.15 and 4.86. There are multiple doubles
in that range with the same double-precision logarithm, so exp(log(x)) == x
must fail for at least one of them.
回答2:
They are no more symmetrical than *
and /
. When dealing with floating point numbers there are rounding errors, so x
and y
may differ in the 15th (or so) digit.
来源:https://stackoverflow.com/questions/16913277/are-cmath-exp-and-log-functions-always-symmetrical