英文:
Is there a solution to fixed cos math function in golang
问题
我正在尝试在Golang中使用math API中的cos函数,但由于某种原因,该函数返回了一个错误的值。对于这个问题,是否有已知的解决方案?
代码:
fmt.Println("cos(pi/2):", math.Cos(math.Pi / 2))
fmt.Println("sen(pi/2):", math.Sin(math.Pi / 2))
输出:
cos(pi/2): 6.123233995736757e-17
sen(pi/2): 1
英文:
I am trying to use cos function from math API in golang, but for some reason the function is returning an incorrect value. Is there a known solution for this problem?
Code:
fmt.Println("cos(pi/2):", math.Cos(math.Pi / 2))
fmt.Println("sen(pi/2):", math.Sin(math.Pi / 2))
Output:
cos(pi/2): 6.123233995736757e-17
sen(pi/2): 1
答案1
得分: 7
你所看到的只是一个轻微的浮点数不准确性。它正确地返回了“接近零”。
英文:
What you're seeing is just a slight floating point inaccuracy. It's correctly returning "almost zero".
See: What Every Computer Scientist Should Know About Floating-Point Arithmetic.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论