-
Notifications
You must be signed in to change notification settings - Fork 471
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
add tests for Math.sumPrecise #4049
Conversation
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.
Matches the tests in my polyfill.
I looked through the tests at a high level and they seem correct. I'm working on documentation for how to write a testing plan and this proposal seemed the right size to try it out on, so I've written that up in #4054. I'd be interested in your opinion; too detailed, not detailed enough? |
I'd also like to see a test case for whatever input values trigger this (non-standard?) |
That (Strictly speaking I'm checking for a slightly different thing - the polyfill can potentially work past the point where the spec errors - but like the note says, it is not expected to be reached in practice.) |
I see, thanks! |
I removed the "awaiting author" label because there's no outstanding feedback for me afaik. |
It definitely doesn't, though I think it covers a majority. You can check the numeric results against Python with the following script if you'd like: import sys
import re
import math
if len(sys.argv) != 2:
print('Usage: python check-sum-tests.py /path/to/sumPrecise/sum.js')
sys.exit(1)
with open(sys.argv[1]) as f:
file = f.read()
for line in file.splitlines():
if 'Math.sumPrecise(' not in line:
continue
match = re.search("Math\.sumPrecise\(\[([^]]*)\]\), ([^)]*)\)", line)
vals = [float(x.strip()) for x in match.group(1).split(',')]
assertedResult = float(match.group(2))
try:
actualResult = math.fsum(vals)
except OverflowError:
# python's fsum can't handle intermediate overflow
# we can work around it by summing the large parts seperately,
# with those parts shrunk by a factor of 64 so the result doesn't overflow
# as long as there is a reasonable number of items this works fine
# order doesn't matter because this is fsum
# and shrinking then growing by a factor of 64 doesn't lose precision because
# the values are large
large = [x / 64 for x in vals if abs(x) > 1]
small = [x for x in vals if abs(x) <= 1]
actualResult = 64 * math.fsum(large) + math.fsum(small)
print(actualResult == assertedResult) Of course, my tests were against an implementation derived from the same logic as |
The proposal for Math.sumPrecise is now stage 2.7. I've written tests which exercise
-0
,NaN
, and ±Infinity
I don't have a principled approach for what cases to include for the last bullet point. If implementations submit more, I'm inclined to include anything that actually tripped up any implementation in a way which was not covered by an existing test.