This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
fix(numberFilter): format large numbers correctly #8705
Closed
gauravaror
wants to merge
1
commit into
angular:master
from
gauravaror:numberFilterLargeNumberFormatBug
Closed
Changes from all commits
Commits
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 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 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.
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.
again, test some additional inputs --- try to find corner cases and make sure they work as you expect
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.
sure will add additional test cases,
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.
I was trying to write some additional test cases is when i found out that .. When we apply the number filter to 879832749374983274928 we get result 879,832,749,374,983,200,000.00
Which is not a correct result since all the digits after 16 digits is being converted to zero, which is wrong.
This is happening because
They are 64-bit floating point values, the largest exact integral value is 253, or 9007199254740992
any number greater than this number will get the zeros after 16 digits and there by loose precision at the least.
this loose of precision is outside of the scope of number filter
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.
There is one more test case i found, but this doesn't look like a valid case also:
8798327493749832749 | number:3342
If this is the filter.
Output returned will be NAN.000000000000000000000000000000000000000
Problem comes when we, try to shift the number by the facction size . fractionSize here is very high, so shifting yields a infinity. which goes and converts into NAN on further processing.
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.
Seems like behaviour is also browser of javascript engine specific when it goes out of precision.
Travis build failed at:
IE 9.0.0 (Windows 7) filters formatNumber should format large number FAILED
Expected '879,832,749,374,983,100,000.00' to be '879,832,749,374,983,200,000.00'.
Chrome gets the result 879,832,749,374,983,200,000.00 and IE to 879,832,749,374,983,100,000.00
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.
shall i just remove the testcase .. and this will be an acceptable and useful fix ?
for the case going to infinity, i just switched the order i was shifting forward and shifting backwards.
and moreover there was an unnecessary math.round function i removed it. now it seems like those cases are handled well..
Seems like some of the tests are failing now . i will look why are they failing , and are they legitimate failure. mostly they are failing because it expected 1.12 but returning 1.13
" Expected '1,234.5' to equal '1,234.6'.
Error: Expected '1,234.5' to equal '1,234.6'.
at null. (/home/gaurav/Work/Angular/myFork/angular.js/test/ng/filter/filtersSpec.js:167:35)
Expected '1,234.56' to equal '1,234.57'.
Error: Expected '1,234.56' to equal '1,234.57'.
at null. (/home/gaurav/Work/Angular/myFork/angular.js/test/ng/filter/filtersSpec.js:168:35)
Expected '1.2' to equal '1.3'.
Error: Expected '1.2' to equal '1.3'.
at null. (/home/gaurav/Work/Angular/myFork/angular.js/test/ng/filter/filtersSpec.js:170:35)
Expected '1.25' to equal '1.26'.
Error: Expected '1.25' to equal '1.26'.
"
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.
no.
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.
How are there kind of cases handle in general.. is there a way to write test-cases for such scenario or handle these scenario more gracefully ? i have not worked on how to handle these corner cases . it will be great if you can guide me a little with this(maybe by providing pointers to places i can learn more or similar cases which are handled in angular) ?
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.
Hi,
This is used in other tests
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.
Thank @lgalfaso i will try that out in my test case.
For the number going to infinity, when exponent is very high. i tried reversing the shift back and sift forward order so that number never reach infinity.
But there is issue in that because rounding of the number is not done correctly.
Example being: 1.11119 is converted to 1.1111 instead of its correct output 1.1112
So what i am planning to do is keep the logic as it is . but when the number goes to infinity and we get number as NAN i will again do shift back and shift forward in different order.
Will push the code in a while