在golang中,有没有解决固定cos数学函数的方法?

huangapple go评论67阅读模式
英文:

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

余弦(pi / 2)的值是零

你所看到的只是一个轻微的浮点数不准确性。它正确地返回了“接近零”。

参考:计算机科学家应该了解的浮点运算知识

英文:

The cosine of pi / 2 is zero.

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.

huangapple
  • 本文由 发表于 2016年2月20日 07:01:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/35516647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定