Cosine 90 doesn't return zero?

Title explains my problem.
I’m trying to do some dot calculations for my game, and from my understanding, to do degree comparisons, I need to use math.cos.
I input said degrees to the function in radians:
math.cos(math.rad(90))
but for some very particular reason, this doesn’t return 0, but a stupid number like 6.1232.
Inputting pi/2 instead of rad90 also gives the same result.

I’ve checked with other values as well,
math.cos(math.rad(60)) returns 0.5 as expected.
math.cos(0) works too, returns 1.

Is this a bug? Why does only cos90 return a wrong error, but other values don’t?
Thanks for your help and/or explanation in advance.

2 Likes

The cosine of 90 should return 0 if 90 is converted into degrees.

Possibly a bug with converting degrees to radians?
Can you try using the function to convert degrees to radians, and see if you get the same result?

Nevermind, I misread the post

https://love2d.org/forums/viewtopic.php?t=88409
basically its another floating point error by a very small margin

2 Likes

It’s just floating point precision error. The value you get (6.12 * 10^ -17) is very close to zero that it might as well be considered 0. This can be approximated to 0.0000000000000000612

image
Read more about floating point precision error here.

1 Like

Certainly not.
First of all, pi/2 is same as rad90. So, it would’ve worked in the first place if it was a conversion error.
image

cos90 gives -.4480, and cosrad90 gives the funny number.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.