Skip to content

fix(usePrecision): apply float-error correction to negative values - #5601

Open
contactjawad wants to merge 1 commit into
vueuse:mainfrom
contactjawad:fix-useprecision-negative
Open

contactjawad wants to merge 1 commit into
vueuse:mainfrom
contactjawad:fix-useprecision-negative

Conversation

@contactjawad

@contactjawad contactjawad commented Aug 20, 2026

Copy link
Copy Markdown

Description

usePrecision returns an incorrect value for some negative inputs. accurateMultiply only applies its floating-point-error correction when value > 0, so negative non-integers fall through to the naive value * power path and hit the same float errors the correction was written to avoid:

usePrecision(-1.1, 2, { math: 'floor' }) // -1.11  (should be -1.1)
usePrecision(1.1, 2, { math: 'floor' })  //  1.1   (correct, mirror case)

Since -1.1 already 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 > 0 to value !== 0 so the correction also runs for negatives. (For value === 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 on main and passes with this change; the existing tests are unaffected.

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.
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Aug 20, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

@vueuse/components

pnpm add https://pkg.pr.new/@vueuse/components@5601
npm i https://pkg.pr.new/@vueuse/components@5601
yarn add https://pkg.pr.new/@vueuse/components@5601.tgz

@vueuse/core

pnpm add https://pkg.pr.new/@vueuse/core@5601
npm i https://pkg.pr.new/@vueuse/core@5601
yarn add https://pkg.pr.new/@vueuse/core@5601.tgz

@vueuse/electron

pnpm add https://pkg.pr.new/@vueuse/electron@5601
npm i https://pkg.pr.new/@vueuse/electron@5601
yarn add https://pkg.pr.new/@vueuse/electron@5601.tgz

@vueuse/firebase

pnpm add https://pkg.pr.new/@vueuse/firebase@5601
npm i https://pkg.pr.new/@vueuse/firebase@5601
yarn add https://pkg.pr.new/@vueuse/firebase@5601.tgz

@vueuse/integrations

pnpm add https://pkg.pr.new/@vueuse/integrations@5601
npm i https://pkg.pr.new/@vueuse/integrations@5601
yarn add https://pkg.pr.new/@vueuse/integrations@5601.tgz

@vueuse/math

pnpm add https://pkg.pr.new/@vueuse/math@5601
npm i https://pkg.pr.new/@vueuse/math@5601
yarn add https://pkg.pr.new/@vueuse/math@5601.tgz

@vueuse/metadata

pnpm add https://pkg.pr.new/@vueuse/metadata@5601
npm i https://pkg.pr.new/@vueuse/metadata@5601
yarn add https://pkg.pr.new/@vueuse/metadata@5601.tgz

@vueuse/nuxt

pnpm add https://pkg.pr.new/@vueuse/nuxt@5601
npm i https://pkg.pr.new/@vueuse/nuxt@5601
yarn add https://pkg.pr.new/@vueuse/nuxt@5601.tgz

@vueuse/router

pnpm add https://pkg.pr.new/@vueuse/router@5601
npm i https://pkg.pr.new/@vueuse/router@5601
yarn add https://pkg.pr.new/@vueuse/router@5601.tgz

@vueuse/rxjs

pnpm add https://pkg.pr.new/@vueuse/rxjs@5601
npm i https://pkg.pr.new/@vueuse/rxjs@5601
yarn add https://pkg.pr.new/@vueuse/rxjs@5601.tgz

@vueuse/shared

pnpm add https://pkg.pr.new/@vueuse/shared@5601
npm i https://pkg.pr.new/@vueuse/shared@5601
yarn add https://pkg.pr.new/@vueuse/shared@5601.tgz

@vueuse/skills

pnpm add https://pkg.pr.new/@vueuse/skills@5601
npm i https://pkg.pr.new/@vueuse/skills@5601
yarn add https://pkg.pr.new/@vueuse/skills@5601.tgz

commit: cc1a61d

@codecov

codecov Bot commented Sep 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.64%. Comparing base (4ed28fb) to head (cc1a61d).
⚠️ Report is 20 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ilyaliao

Copy link
Copy Markdown
Member

Thanks for the PR. Some cases still look wrong though, for example:

usePrecision(-0.55, 1, { math: 'round' })

This should return -0.5, but here it returns -0.6.

We may need to change the calculation to fix the root cause.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants