[Bug] cos (90) doesn't return exactly zero

Hi @thunkable,

The Math block cos (90 degrees) returns a small number instead of exactly zero. Same for -90 degrees. Also fails in Android version.

(Note: The intermediate variable is needed since the “from Label set Text to” block won’t coerce non-text values.)

Thanks!
BillG

I theory, it should be 0, as the adjacent side of the triangle becomes infinitesimally small, eventually reaching 0. The only problem with that assumption is that numbers in computing are finite and binary so it gets really really close to zero, but never quite makes it.

There’s a discussion here:

You could always control for this by checking if the input angle is +/-90 degrees and if so have it output 0, otherwise pass the angle to the cos function.

Hi @thunkable,

The Math block cos (90 degrees) returns a small number instead of exactly zero. Same for -90 degrees. Note: Also fails in iOS version.

Thanks!
BillG

Thanks @domhnallohanlon, good reading. It’s interesting to note that hand calculators “always” return exactly zero for cos (90), so they’re probably doing the special check that you mention above.

Thanks!
BillG

1 Like

what result you get after putting 1.57? @BillG

He’s not inputting 1.57, the answer is 0.00000000157 instead of 0.

For more on floating point arithmetic see here:
http://www.smbc-comics.com/?id=2999
:joy:

I think it is radian, but I checked that in radian cos 90 ≈ 0.44

Just to confirm I ask him to put π/2 ≈ 1.57

If it calculates in radians, then result will tend to 0

1 Like

It works in degrees, apparently:

http://appinventor.mit.edu/explore/ai2/support/blocks/math.html#cos

But that was a nice idea for a test.

Android returns 0.99962 for cos(1.57 degrees).

What’s the significance of 1.57? I don’t see that in my iOS/Android answers.

Thanks!
BillG

Oops, missed the updates, you were testing for degrees vs radians.

Thanks!
BillG

1 Like

In Android we have this block, maybe ios too have that

https://thunkable.com/explore/ai2/support/blocks/math.html#convertdeg

π/2 radians is 90 degrees so 1.57 was an approximation for 3.1415…/2

I think it’s safe to say that we’re working in degrees here since cos(0) is 1.

cos 0 in radian is also 1

Thunkable uses degrees instead of radians.

Thanks!
BillG

1 Like

Yes @BillG, I just checked that

https://thunkable.com/explore/ai2/support/blocks/math.html#cos

yes, but if it were in radians then cos(1.57) would be close to 0, Since Bill’s answers for cos(1.57) was very close to 1 it was fair to deduce that in the expression cos(x) the argument is in degrees rather than radians.

Yes @domhnallohanlon I understood that but this is only the case of Android. what about ios?

It’s the exact same thing but with larger floats on iOS than Android - hence the higher precision.

Just for completeness, in iOS, cos(1.57 degrees) returns 0.9996245977511435.

Thanks!
BillG

1 Like

The final solution is to round the values to some decimals