# Math cos wth

Dajka Ferenc

Greenhorn

Posts: 6

Tim Moores

Bartender

Posts: 2488

4

posted 3 years ago

No, it definitely is. toRadians() is giving you an approximation of pi / 2, but with some error in it for the reasons stated in the FAQ, and then Math.cos() is adding more error, for the same reasons.

That's why when doing floating point calculations, we need to take those errors into account. For example when comparing for equality, we don't test if

- 0

Dajka Ferenc wrote:no its not

No, it definitely is. toRadians() is giving you an approximation of pi / 2, but with some error in it for the reasons stated in the FAQ, and then Math.cos() is adding more error, for the same reasons.

That's why when doing floating point calculations, we need to take those errors into account. For example when comparing for equality, we don't test if

`x == y`but rather if

`Math.abs(x - y)< epsilon`.

posted 3 years ago

- 0

"and other arithmetic inaccuracies"...yes it is. In particular, 90 degrees = PI/2 radians, and Java cannot hold the value exactly in a floating point value. So when you take the cosine you end up with something very close to zero, instead of exactly zero.

posted 3 years ago

- 0

And this line here makes no sense:

You're taking degrees, converting to radians, which is fine, getting the cosine, which is fine, but then doing a radian ⇒ degree conversion on the cosine. That last step is trying to convert a unitless scalar into an angular measure, and that's not valid.

You're going

You're taking degrees, converting to radians, which is fine, getting the cosine, which is fine, but then doing a radian ⇒ degree conversion on the cosine. That last step is trying to convert a unitless scalar into an angular measure, and that's not valid.

You're going

`deg ⇒ rad ⇒ scalar ⇒ deg`. You'd need to add a call to Math.acos() in there for it to make sense.Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |