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
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.