fix(usePrecision): apply float-error correction to negative values - #5601
Open
contactjawad wants to merge 1 commit into
Open
contactjawad wants to merge 1 commit into
contactjawad wants to merge 1 commit into
Conversation
accurateMultiply only applied its float-error correction when value > 0, so
negative non-integers fell back to the naive `value * power` path and hit
floating-point errors. For example usePrecision(-1.1, 2, { math: 'floor' })
returned -1.11, while the positive mirror usePrecision(1.1, 2, { math: 'floor' })
correctly returns 1.1. Apply the correction to negative values too.
@vueuse/components
@vueuse/core
@vueuse/electron
@vueuse/firebase
@vueuse/integrations
@vueuse/math
@vueuse/metadata
@vueuse/nuxt
@vueuse/router
@vueuse/rxjs
@vueuse/shared
@vueuse/skills
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5601 +/- ##
==========================================
+ Coverage 68.38% 68.64% +0.26%
==========================================
Files 350 351 +1
Lines 8324 8353 +29
Branches 2548 2543 -5
==========================================
+ Hits 5692 5734 +42
+ Misses 2149 2139 -10
+ Partials 483 480 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Member
|
Thanks for the PR. Some cases still look wrong though, for example: usePrecision(-0.55, 1, { math: 'round' })This should return We may need to change the calculation to fix the root cause. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
usePrecisionreturns an incorrect value for some negative inputs.accurateMultiplyonly applies its floating-point-error correction whenvalue > 0, so negative non-integers fall through to the naivevalue * powerpath and hit the same float errors the correction was written to avoid:Since
-1.1already has fewer than 2 decimal places, its value at precision 2 should be unchanged — exactly like the positive mirror.Fix
Change the guard from
value > 0tovalue !== 0so the correction also runs for negatives. (Forvalue === 0,"0"has no., so that branch was never taken anyway — behaviour is unchanged there.)Test
Added a case asserting
-1.1/-2.2(floor) and-2.3(ceil) stay unchanged at precision 2. It fails onmainand passes with this change; the existing tests are unaffected.