The precision of the natural logarithm is not correct in CLISP. What might be wrong?
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
精彩评论