Why in Common Lisp, when I run the function “acos” with an argument of 1, is the answer is wrong?

陌路散爱 提交于 2019-12-12 21:53:07

问题


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.



来源:https://stackoverflow.com/questions/17424545/why-in-common-lisp-when-i-run-the-function-acos-with-an-argument-of-1-is-the

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