-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: round to correct precision when step uses exponential notation #8290
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
Changes from 4 commits
b2d7658
872c527
96317b5
afa153f
c447134
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
import {clamp, snapValueToStep} from '../src/number'; | ||||||
import {clamp, roundToStepPrecision, snapValueToStep} from '../src/number'; | ||||||
|
||||||
describe('number utils', () => { | ||||||
describe('clamp', () => { | ||||||
|
@@ -8,6 +8,34 @@ describe('number utils', () => { | |||||
}); | ||||||
}); | ||||||
|
||||||
describe('roundToStepPrecision', () => { | ||||||
it('should return the input unchanged for integer steps', () => { | ||||||
expect(roundToStepPrecision(7.123, 1)).toBe(7.123); | ||||||
expect(roundToStepPrecision(5, 10)).toBe(5); | ||||||
}); | ||||||
it('should round to the correct decimal places for steps with decimals', () => { | ||||||
expect(roundToStepPrecision(1.24, 0.1)).toBe(1.24); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe I'm over thinking this, but shouldn't this be
Suggested change
I realize it's the existing behaviour, I'll have to look back to remember why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for looking into this! I was wondering the same thing while looking at the original implementation 🤔 let precision = pointIndex >= 0 ? stepString.length - pointIndex : 0; I also thought precision should maybe be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi @scrgl thanks for pointing out that incorrect implementation! 872c527 fixed the issue mentioned in #8290 (comment) and added more tests. I opted not to use |
||||||
expect(roundToStepPrecision(1.456, 0.01)).toBe(1.456); | ||||||
expect(roundToStepPrecision(1.2345, 0.015)).toBe(1.2345); | ||||||
expect(roundToStepPrecision(1.2345, 0.25)).toBe(1.235); | ||||||
// Should not overcount precision | ||||||
expect(roundToStepPrecision(2.349, 0.100)).toBe(2.35); | ||||||
expect(roundToStepPrecision(2.35, 0.100)).toBe(2.35); | ||||||
// Should handle negative values | ||||||
expect(roundToStepPrecision(-1.456, 0.01)).toBe(-1.456); | ||||||
// Should handle zero value | ||||||
expect(roundToStepPrecision(0, 0.01)).toBe(0); | ||||||
}); | ||||||
it('should handle rounding for exponential step values', () => { | ||||||
expect(roundToStepPrecision(0.123456789, 1e-3)).toBe(0.1235); | ||||||
expect(roundToStepPrecision(0.123456789, 1e-7)).toBe(0.12345679); | ||||||
expect(roundToStepPrecision(0.123456789, 1.5e-7)).toBe(0.123456789); | ||||||
expect(roundToStepPrecision(0.123456789, 2.5e-6)).toBe(0.12345679); | ||||||
// Should handle exponential notation steps regardless of e/E case | ||||||
expect(roundToStepPrecision(0.123456789, 1E-8)).toBe(0.123456789); | ||||||
}); | ||||||
}); | ||||||
|
||||||
describe('snapValueToStep', () => { | ||||||
it('should snap value to nearest step based on min and max', () => { | ||||||
expect(snapValueToStep(2, -0.5, 100, 3)).toBe(2.5); | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.