我正在尝试在 golang 中使用数学 API 中的 cos 函数,但由于某种原因,该函数返回了一个不正确的值。这个问题有已知的解决方案吗?
代码:
fmt.Println("cos(pi/2):", math.Cos(math.Pi / 2))
fmt.Println("sen(pi/2):", math.Sin(math.Pi / 2))
输出:
余弦(圆周率/2):6.123233995736757e-17
森(pi/2): 1
最佳答案
您看到的只是轻微的浮点误差。它正确地返回“几乎为零”。
参见:What Every Computer Scientist Should Know About Floating-Point Arithmetic .
关于function - golang中固定cos数学函数有没有解决办法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35516647/