# Bug in trig functions

**URL:** <https://discourse.elm-lang.org/t/bug-in-trig-functions/2724>\
**Category:** Learn\
**Created:** [December 8, 2018, 5:18am UTC](https://discourse.elm-lang.org/t/bug-in-trig-functions/2724 "2018-12-08T05:18:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [December 8, 2018, 5:18am UTC](https://discourse.elm-lang.org/t/bug-in-trig-functions/2724/1 "2018-12-08T05:18:54Z")

</div>

I might be missing something but I think there’s a bug in the Basics module `sin` and `cos` functions.

```auto
> pi
3.141592653589793 : Float
> sin pi
1.2246467991473532e-16 : Float -- should be zero
> cos pi
-1 : Float
> sin 0
0 : Float
> cos 0
1 : Float
> sin (pi/2)
1 : Float
> cos (pi/2)
6.123233995736766e-17 : Float -- should be 0
> sin (pi*2)
-2.4492935982947064e-16 : Float -- should be 0
> cos (pi*2)
1 : Float

```

any ideas what is going on?

---

<div class="post-metadata">

**Author:** ![mgold](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mgold/32/284_2.png) [@mgold](https://discourse.elm-lang.org/u/mgold)\
**Post date:** [December 8, 2018, 5:32am UTC](https://discourse.elm-lang.org/t/bug-in-trig-functions/2724/2 "2018-12-08T05:32:12Z")

</div>

> [@stevensonmt](#):
>
> 1.2246467991473532e-16

Notice the `e-16` at the end, meaning that you’re not getting 1.22, you’re getting 1.22 divided by ten to the sixteenth power. Which is very small, but not zero.

This is how floating point numbers work, for better or worse. I think it has to do with the fact the `pi` is not exactly π. This is the same answer returned by the JavaScript expression `Math.sin(Math.PI)`.

If you’re working with floating point numbers in elm-test, there are dedicated functions for expecting a value to be within a certain tolerance.

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [December 8, 2018, 5:37am UTC](https://discourse.elm-lang.org/t/bug-in-trig-functions/2724/3 "2018-12-08T05:37:00Z")

</div>

I guess I just have to remember to run `floor` on those values to get 0.

---

<div class="post-metadata">

**Author:** ![ianmackenzie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ianmackenzie/32/86_2.png) [@ianmackenzie](https://discourse.elm-lang.org/u/ianmackenzie)\
**Post date:** [December 8, 2018, 5:49am UTC](https://discourse.elm-lang.org/t/bug-in-trig-functions/2724/4 "2018-12-08T05:49:13Z")

</div>

Using `floor` won’t work either, since in your

```elm
> sin (pi*2)
-2.4492935982947064e-16 : Float 

```

example, using `floor` will give you -1, not 0! You could use `round`, but depending on exactly what you’re doing there will almost certainly be a better solution. If you’re testing something, test within a tolerance as @mgold mentions, or if you just want to display the result in ‘nice’ way then try a dedicated number formatting package; [`myrho/elm-round`](https://package.elm-lang.org/packages/myrho/elm-round/latest/) looks pretty nice.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [December 18, 2018, 5:49am UTC](https://discourse.elm-lang.org/t/bug-in-trig-functions/2724/5 "2018-12-18T05:49:14Z")

</div>

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