Are cmath exp() and log() functions always symmetrical?

爱⌒轻易说出口 提交于 2019-12-11 20:35:39

问题


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 252doubles between 64 and 128, but there are fewer than 250doubles 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!