logBase 10 erroneous

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.

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/

1 Like

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.

Basics.logBase is implemented 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, and a pull request proposing to use said javascript function.

2 Likes

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