# logBase 10 erroneous

**URL:** <https://discourse.elm-lang.org/t/logbase-10-erroneous/6312>\
**Category:** Learn\
**Created:** [September 27, 2020, 10:55pm UTC](https://discourse.elm-lang.org/t/logbase-10-erroneous/6312 "2020-09-27T22:55:48Z")\
**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:** [September 27, 2020, 10:55pm UTC](https://discourse.elm-lang.org/t/logbase-10-erroneous/6312/1 "2020-09-27T22:55:48Z")

</div>

I’m using `Basics.logBase` and am getting a surprising result.  
`logBase 10 1000` returns `2.9999999999999996`. This is problematic for my overall algorithm as I am expecting to be able to use `floor` to accurately find how many digit “places” I need. Any way I can get `logBase 10 1000` to return `3` as expected?

On further investigation, this issue appears to affect every value of `10^x` where `x%3 == 0`.

---

<div class="post-metadata">

**Author:** ![mattpiz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattpiz/32/860_2.png) [@mattpiz](https://discourse.elm-lang.org/u/mattpiz)\
**Post date:** [September 28, 2020, 6:24am UTC](https://discourse.elm-lang.org/t/logbase-10-erroneous/6312/2 "2020-09-28T06:24:03Z")

</div>

Float arithmetic is subject to this kind of issues. If I had to count the number of digits of a number, I’d use recursive integer division by 10 such as [https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods/](https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods/)

---

<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:** [September 28, 2020, 7:43pm UTC](https://discourse.elm-lang.org/t/logbase-10-erroneous/6312/3 "2020-09-28T19:43:16Z")

</div>

Yes, there are other ways to approach my particular problem but the link you provided includes the method I’m using. My specific use case is orthogonal to the problem of a potentially fundamental bug in a basic arithmetic function. The appearance of a pattern suggests to me that the error is not simply “floating point math is quirky” but rather something that can be fixed. I would create an issue on github to address this if others can confirm the bug.

---

<div class="post-metadata">

**Author:** ![jreusch](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@jreusch](https://discourse.elm-lang.org/u/jreusch)\
**Post date:** [September 28, 2020, 9:05pm UTC](https://discourse.elm-lang.org/t/logbase-10-erroneous/6312/4 "2020-09-28T21:05:54Z")

</div>

`Basics.logBase` is [implemented](https://github.com/elm/core/blob/1.0.5/src/Basics.elm#L619) as `log number / log base`, where `log` is just `Math.log` from Javascript. The rounding errors you are seeing comes from the floating-point division happening here.

There also exists `Math.log10` in Javascript, which yields better results, but does not exist in Internet Explorer. As always, there already exists an issue [matching your problem](https://github.com/elm/core/issues/1048), and a [pull request](https://github.com/elm/core/pull/1054/files%5D) proposing to use said javascript function.

---

<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:** [October 8, 2020, 9:06pm UTC](https://discourse.elm-lang.org/t/logbase-10-erroneous/6312/5 "2020-10-08T21:06:10Z")

</div>

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