Repository navigation
fix(number): keep decimal multipleOf floats exact and within bounds - #4162
Conversation
✅ Deploy Preview for fakerjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## next #4162 +/- ##
==========================================
- Coverage 99.19% 99.14% -0.05%
==========================================
Files 1184 1184
Lines 3953 3969 +16
Branches 714 718 +4
==========================================
+ Hits 3921 3935 +14
- Misses 28 29 +1
- Partials 4 5 +1
🚀 New features to boost your workflow:
|
|
Although this handles “nice” literals like 0.3 or 1.5 with matching bounds, it isn’t a full fix, and it can regress other bounds. For example:
For a more general fix I’d:
|
|
I also considered "tricking" the engine to collapse these values by itself. E.g. by using +1-1 or /10*10 But I havent looked any deeper into this yet. |
|
@matthewmayer you're right on both points. Reworked in f31bab0 along the lines you suggested:
@ST-DDT re the "+1-1" / "/10*10" idea: I think that would only round away the representation error after the fact. It wouldn't bring back a bound that |
ST-DDT
left a comment
There was a problem hiding this comment.
Please address the comments above.
…Of bound Signed-off-by: kevin9327 <[email protected]>
|
Addressed in the latest push: shortened the scaling comment, scoped the re-throw comment to the |
# Conflicts: # test/modules/number.spec.ts
|
Fixed the red CI: the Lint failure was oxfmt formatting in |
|
@matthewmayer I would like to have an explicit approval from you as well (human math checking), then I will approve 🙂 |
|
aside from the docs issue, this is fine. |
faker.number.float()with a decimalmultipleOfthat is not a power of ten (0.3, 0.25, 1.5, ...) divides by1 / multipleOf. Floating-point error in that division gives values that are not the intended multiples, and some of them fall belowmin:For a decimal step, this change scales by the step's own number of decimal places instead. It picks an integer multiple of the scaled step, then divides once, so 3 × 0.3 becomes
9 / 10 = 0.9. Steps of the form 10^-n and non-decimal steps keep the existing code path. The new test fails onnextand passes with the fix.AI-assisted: I directed the investigation and reviewed and verified the change myself.