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

**URL:** <https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838>\
**Category:** Questions about Thunkable X\
**Created:** [June 25, 2017, 7:30pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838 "2017-06-25T19:30:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![BillG](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@BillG](https://community.thunkable.com/u/BillG)\
**Post date:** [June 25, 2017, 7:30pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/1 "2017-06-25T19:30:25Z")

</div>

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.

![](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/2X/d/d3326f2261e8a1f297fe426f43256b90e3749171.png)

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

Thanks!  
BillG

---

<div class="post-metadata">

**Author:** ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)\
**Post date:** [June 25, 2017, 7:42pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/2 "2017-06-25T19:42:48Z")

</div>

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:

> **[Scratch - Imagine, Program, Share](https://scratch.mit.edu/discuss/topic/39873/?page=1#post-331300)**
>
> Make games, stories and interactive art with Scratch. (scratch.mit.edu)

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.

---

<div class="post-metadata">

**Author:** ![BillG](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@BillG](https://community.thunkable.com/u/BillG)\
**Post date:** [June 25, 2017, 7:53pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/3 "2017-06-25T19:53:52Z")

</div>

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.

![](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/2X/2/24c9c48c855bbb71de93543430655a159ff782b8.png)

Thanks!  
BillG

---

<div class="post-metadata">

**Author:** ![BillG](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@BillG](https://community.thunkable.com/u/BillG)\
**Post date:** [June 25, 2017, 8:57pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/4 "2017-06-25T20:57:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pavi2410](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/pavi2410/32/16588_2.png) [@pavi2410](https://community.thunkable.com/u/pavi2410)\
**Post date:** [June 25, 2017, 8:58pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/5 "2017-06-25T20:58:13Z")

</div>

what result you get after putting 1.57? @BillG

---

<div class="post-metadata">

**Author:** ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)\
**Post date:** [June 25, 2017, 9:01pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/6 "2017-06-25T21:01:02Z")

</div>

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](http://www.smbc-comics.com/?id=2999)  
😂

---

<div class="post-metadata">

**Author:** ![pavi2410](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/pavi2410/32/16588_2.png) [@pavi2410](https://community.thunkable.com/u/pavi2410)\
**Post date:** [June 25, 2017, 9:03pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/7 "2017-06-25T21:03:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)\
**Post date:** [June 25, 2017, 9:06pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/8 "2017-06-25T21:06:29Z")

</div>

It works in degrees, apparently:

[http://appinventor.mit.edu/explore/ai2/support/blocks/math.html#cos](http://appinventor.mit.edu/explore/ai2/support/blocks/math.html#cos)

But that was a nice idea for a test.

---

<div class="post-metadata">

**Author:** ![BillG](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@BillG](https://community.thunkable.com/u/BillG)\
**Post date:** [June 25, 2017, 9:07pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/9 "2017-06-25T21:07:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BillG](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@BillG](https://community.thunkable.com/u/BillG)\
**Post date:** [June 25, 2017, 9:11pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/10 "2017-06-25T21:11:13Z")

</div>

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

Thanks!  
BillG

---

<div class="post-metadata">

**Author:** ![pavi2410](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/pavi2410/32/16588_2.png) [@pavi2410](https://community.thunkable.com/u/pavi2410)\
**Post date:** [June 25, 2017, 9:12pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/11 "2017-06-25T21:12:15Z")

</div>

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

![https://thunkable.com/explore/ai2/support/blocks/math.html#convertdeg](https://sea1.discourse-cdn.com/flex015/uploads/thunkable1/original/2X/a/af773a05ab30bc8a9c587a023388ec15df36b6c9.png)

---

<div class="post-metadata">

**Author:** ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)\
**Post date:** [June 25, 2017, 9:12pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/12 "2017-06-25T21:12:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pavi2410](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/pavi2410/32/16588_2.png) [@pavi2410](https://community.thunkable.com/u/pavi2410)\
**Post date:** [June 25, 2017, 9:12pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/13 "2017-06-25T21:12:41Z")

</div>

cos 0 in radian is also 1

---

<div class="post-metadata">

**Author:** ![BillG](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@BillG](https://community.thunkable.com/u/BillG)\
**Post date:** [June 25, 2017, 9:13pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/14 "2017-06-25T21:13:22Z")

</div>

Thunkable uses degrees instead of radians.

Thanks!  
BillG

---

<div class="post-metadata">

**Author:** ![pavi2410](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/pavi2410/32/16588_2.png) [@pavi2410](https://community.thunkable.com/u/pavi2410)\
**Post date:** [June 25, 2017, 9:15pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/15 "2017-06-25T21:15:20Z")

</div>

Yes @BillG, I just checked that

[https://thunkable.com/explore/ai2/support/blocks/math.html#cos](https://thunkable.com/explore/ai2/support/blocks/math.html#cos)

---

<div class="post-metadata">

**Author:** ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)\
**Post date:** [June 25, 2017, 9:16pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/16 "2017-06-25T21:16:16Z")

</div>

> [@pavi2410](#):
>
> cos 0 in radian is also 1

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.

---

<div class="post-metadata">

**Author:** ![pavi2410](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/pavi2410/32/16588_2.png) [@pavi2410](https://community.thunkable.com/u/pavi2410)\
**Post date:** [June 25, 2017, 9:17pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/17 "2017-06-25T21:17:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)\
**Post date:** [June 25, 2017, 9:19pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/18 "2017-06-25T21:19:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BillG](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@BillG](https://community.thunkable.com/u/BillG)\
**Post date:** [June 25, 2017, 9:26pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/19 "2017-06-25T21:26:29Z")

</div>

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

Thanks!  
BillG

---

<div class="post-metadata">

**Author:** ![pavi2410](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/pavi2410/32/16588_2.png) [@pavi2410](https://community.thunkable.com/u/pavi2410)\
**Post date:** [June 25, 2017, 9:31pm UTC](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838/20 "2017-06-25T21:31:11Z")

</div>

The final solution is to round the values to some decimals

[Next page](https://community.thunkable.com/t/bug-cos-90-doesnt-return-exactly-zero/4838.md?page=2)
