Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bug related to IEEE 754 #16

Merged
merged 7 commits into from
Mar 26, 2022
Merged

Fix Bug related to IEEE 754 #16

merged 7 commits into from
Mar 26, 2022

Conversation

leomotors
Copy link
Sponsor Contributor

The Bug

Screenshot 2022-03-25 172856

This bug happens for every number that ends with .29, .57, .58

Because when these numbers are multiplied by 100, they become

Screenshot 2022-03-25 174701

The issue is at rounding down the Satang to 2 digits which cause the inaccuracy

This pull request

  • ✅ Fix for String
  • ✅ Fix for Small Number (<1000)
  • ✅ Fix for Big Number (>100000)
  • ➕ Added Test Cases to ensure those 3 are fixed, and also added test cases to other parts to ensure this change doesn't break the library

String

Before:

satang = satangStr ? Math.floor(Number('0.' + satangStr) * 100) : 0;

After:

satang = satangStr
  ? Number(satangStr.slice(0, 2)) *
      (satangStr.length >= 2 ? 1 : [100, 10][satangStr.length])
  : 0;

This will fix for all strings, and it also appeared to be a bit faster than before

Number

Before:

satang = Number.isInteger(input)
  ? 0 
  : Math.floor((input * 100) % 100);

After:

satang = Number.isInteger(input)
  ? 0
  : Math.floor(((input + Number.EPSILON * (baht || 1)) * 100) % 100);

This will now fix for most of the numbers, theoretically, it should work up to around 10^12-14
For performance, based on the benchmark, it does not have much difference.

@vercel
Copy link

vercel bot commented Mar 25, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/notnarze/baht-js/2RK2gr642feC4GXZUteUUtbWS1JH
✅ Preview: https://baht-js-git-fork-leomotors-main-notnarze.vercel.app

Copy link
Owner

@narze narze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏🏻

This will be released as v0.7.0

@sonarcloud
Copy link

sonarcloud bot commented Mar 26, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

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