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 floating-point arithmetic issues when props are decimals. #619

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jcfrancisco
Copy link

Consider the following case:

      const value = 5.3;
      const props = {
        marks: {},
        step: 0.05,
        min: 0,
        max: 5.3
      };

I would expect the output of getClosestPoint to be 5.3. However, it returns 5.25.

This is because of floating point arithmetic issues with the function.

I've added a test case and taken a stab at figuring it out, making sure I continue to respect the Math.floor logic on maxSteps.

Comment on lines +24 to +31
export function getPrecision(step) {
const stepString = step.toString();
let precision = 0;
if (stepString.indexOf('.') >= 0) {
precision = stepString.length - stepString.indexOf('.') - 1;
}
return precision;
}
Copy link
Author

Choose a reason for hiding this comment

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

This function is untouched, moved it up because I'm using it elsewhere.

Comment on lines -73 to +98
parseFloat(closestPoint.toFixed(getPrecision(step)));
withPrecision(closestPoint, getPrecision(step));
Copy link
Author

Choose a reason for hiding this comment

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

I can back out this refactor if it's unwanted

@codecov
Copy link

codecov bot commented Jan 21, 2020

Codecov Report

Merging #619 into master will increase coverage by 0.43%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #619      +/-   ##
==========================================
+ Coverage    95.4%   95.83%   +0.43%     
==========================================
  Files           2        2              
  Lines          87       96       +9     
  Branches       29       30       +1     
==========================================
+ Hits           83       92       +9     
  Misses          4        4
Impacted Files Coverage Δ
src/utils.js 95.5% <100%> (+0.5%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5ba4181...e4768ef. Read the comment docs.

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.

None yet

1 participant