Why in Common Lisp, when I run the function “acos”

2019-07-21 04:51发布

问题:

instead of the correct answer of 0 when I run (acos 1) in, to find the ArcCosine of 1 emacs, I get the answer

-6.125742f-17

why is that and how do I get the correct answer of 0 like Windows Calculator says it should be

I'm using SBCL as my Lisp Interpreter on Windows 8 and SBCL is 32-BIT

when I run:

(acos (rational 1))

i get the same answer and when I run

(acos (float 1))

i get this error

The value -6.1257422745431e-17
is not of type
  (DOUBLE-FLOAT 0.0 3.141592653589793).
   [Condition of type TYPE-ERROR]

回答1:

The accepted answer to sin(M_PI) is not 0 is applicable here as well:

You need to read What Every Computer Scientist Should Know About Floating-Point Arithmetic and realise that all floating point computations are approximate and the approximation you got is "good enough".

As for the SBCL error, you should report it to the SBCL maintainers.