-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: tools/toFixed precision #1950
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
Open
carlosjeurissen
wants to merge
7
commits into
svg:main
Choose a base branch
from
carlosjeurissen:fix/to-fixed-precision
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
840ef2d
Prevent toFixed from adding more decimals than was given
carlosjeurissen 5b48095
Use toFixed in more plugins
carlosjeurissen 90927e2
Simplify logic by just skipping toFixed with any precision higher tha…
carlosjeurissen 1d389d2
Add toFixedStr as string alternative for toFixed
carlosjeurissen 48e3d1c
Start using new toFixedStr method in cleanupListOfValues and cleanupN…
carlosjeurissen 89ae4bb
Correct return type for toFixedStr
carlosjeurissen 6d1a6b2
Start using toFixedStr in path.js
carlosjeurissen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toFixed(2.5845, 3)correctly gives me 2.585. Are there real examples where the check is needed? I iterated over every number few years ago and didn't found any.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toFixed(1.99999999999, 18)-> 1.9999999999899998There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GreLI Here is an example of before and after which adds additional digits as the result of floating point arithmetic.
Before:
after:
with the following svgo config:
As you can see it happens with the x1 attribute in the linear-gradient. And there is also the
2.0874900600000004in a d attribute further down.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KTibow Ah, I see. Without
if (precision > 17 || num % 1 == 0) return num;on line 237 it really have such issues.Though I must say, that are quite extreme precisions, far from practical ones. E.g. you lose precision by multiplication numbers with more than 9 digits in the fractional part due to how floating point calculations work. I wouldn't recommend to use
floatPrecision = 19to anyone. Editors usually don't write more than 5 fractional digits.Speaking of pixels there wouldn't be difference more than 1/256 of pixel (ok, 1/512 with high dpi display). So, given the usual display has no more than thousands of pixels, 7 digits in total is more than enough. Thus there is no sense in more than 5 fractional digits in percentages. Practically, one wouldn't see difference for something like SVGO default 3 digits. For some images it can be even 2 or 1 (without curves—I have some ideas about that).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The most egregious example I've seen in the wild is:
Without any kind of path normalization to bring the scale of the numbers closer to 0 (which may be worth looking at in future) reducing the precision can be problematic. But even then, a float precision like 19 does seem odd.
Generally this is true, but depend on the scale that transforms operate on, that can still cause problems. (Not denying, that SVGs like the example given seem bizarre, but it does happen.)
I haven't reviewed this PR yet as I've been focused on other things in SVGO, so can't say if I'm in favor of the change or not yet. Just noting that it still may be worth having better support for large precision values if it's otherwise causing problems.
@carlosjeurissen Sorry for putting you off! I've been busy with client work, and now that I have time for SVGO again, my priority is with issues related to the v4 release candidates. (Namely imports/interface related issues.)
Once those are sorted, we can take a deeper look at this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, transformations are often sick and overused. That's why it's better to apply transformations at first—that makes thinks easier.