开发者

The precision of the natural logarithm is not correct in CLISP. What might be wrong?

开发者 https://www.devze.com 2023-02-24 06:03 出处:网络
What wen开发者_JAVA技巧t wrong? [1]> (log (exp 1)) 0.99999994 This is due to the finite precision of floating-point representations of fractional numbers.

What wen开发者_JAVA技巧t wrong?

 [1]> (log (exp 1))
 0.99999994


This is due to the finite precision of floating-point representations of fractional numbers.

Please see: http://en.wikipedia.org/wiki/Floating_point

(exp 1) is going to be an approximation of e (which requires infinite precision to represent perfectly). The natural logarithm of that approximation will be approximately (but not exactly) 1. Understanding floating-point representation will allow you to understand why this happens.

CLISP is using your machine architecture's native representation of floats. Most commonly by far, this representation is in one the formats specified by IEEE 754 (typically 32- or 64-bit; in your case it looks like 32-bit). In a nutshell, fractional parts are represented by a sum of inverse powers of 2 (i.e., some combination of 1/2, 1/4, 1/8, ... 1/2^32, etc.)


Try with double precision floating point:

(log (exp 1.0d0))

=> 1.0D0 ; at least in Clozure CL

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号