Skip to content

fix(maths): prevent float rounding error in numberOfDigitsUsingLog for large safe integers - #2029

Open
SaifAli-S wants to merge 1 commit into
TheAlgorithms:masterfrom
SaifAli-S:fix-number-of-digits-precision
Open

SaifAli-S wants to merge 1 commit into
TheAlgorithms:masterfrom
SaifAli-S:fix-number-of-digits-precision

Conversation

@SaifAli-S

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Maths/NumberOfDigits.js previously implemented numberOfDigitsUsingLog as Math.floor(Math.log10(Math.abs(n))) + 1.

Because IEEE 754 double precision floats have a 53-bit mantissa, Math.log10(999999999999999) ($10^{15}-1$) evaluates to 15 instead of 14.9999.... This caused numberOfDigitsUsingLog(999999999999999) to return 16 instead of 15. Since $999999999999999 < \text{Number.MAX_SAFE_INTEGER}$ ($9007199254740991$), this was an unexpected calculation error for a safe integer.

This PR fixes the precision issue by checking power-of-ten bounds (Math.pow(10, digits - 1) and Math.pow(10, digits)), adds input validation (Number.isFinite), and adds unit test cases for $10^{15}-1$, $10^{15}$, and $10^{15}+1$ in Maths/test/NumberOfDigits.test.js.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
  • All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.

@SaifAli-S
SaifAli-S requested a review from appgurueu as a code owner November 13, 2026 18:34
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.47619% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.92%. Comparing base (5c39e87) to head (44286c5).

Files with missing lines Patch % Lines
Maths/NumberOfDigits.js 90.47% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2029   +/-   ##
=======================================
  Coverage   85.91%   85.92%           
=======================================
  Files         379      379           
  Lines       19778    19796   +18     
  Branches     3016     3023    +7     
=======================================
+ Hits        16993    17009   +16     
- Misses       2785     2787    +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants