Produce more math errors in division

Besides dividing by zero, any result producing NaN including inf/inf, NaN/anything, anything/NaN causes a math error as well. We only contemplated NaN/anything and neglected the rest, so we generalize it.
This commit is contained in:
Sei Lisa 2016-05-15 18:21:24 +02:00
parent 28a8f0757c
commit 23b27bd3af
2 changed files with 7 additions and 3 deletions

View file

@ -669,8 +669,6 @@ def div(a, b, f32=True):
if b == 0: if b == 0:
raise ELSLMathError raise ELSLMathError
if ta in (int, float): if ta in (int, float):
if math.isnan(a): # NaN/anything gives math error
raise ELSLMathError
if ta == int and tb == int: if ta == int and tb == int:
# special case # special case
if a == -2147483648 and b == -1: if a == -2147483648 and b == -1:
@ -679,7 +677,10 @@ def div(a, b, f32=True):
# signs differ - Python rounds towards -inf, we need rounding towards 0 # signs differ - Python rounds towards -inf, we need rounding towards 0
return - a//-b # that's -(a//-b) not (-a)//-b return - a//-b # that's -(a//-b) not (-a)//-b
return a//b return a//b
return F32(ff(a)/ff(b), f32) ret = F32(ff(a)/ff(b), f32)
if math.isnan(res): # A NaN result gives a math error.
raise ELSLMathError
return ret
if ta == Vector: if ta == Vector:
a = v2f(a) a = v2f(a)
b = ff(b) b = ff(b)

View file

@ -493,6 +493,9 @@ def do_tests():
shouldexcept('div(1.0, 0.0)', ELSLMathError) shouldexcept('div(1.0, 0.0)', ELSLMathError)
shouldexcept('div(1, 0)', ELSLMathError) shouldexcept('div(1, 0)', ELSLMathError)
shouldexcept('div(NaN, 1)', ELSLMathError)
shouldexcept('div(1, NaN)', ELSLMathError)
shouldexcept('div(F32(1e40), F32(1e40))', ELSLMathError)
shouldexcept('zstr("blah")', ELSLInvalidType) shouldexcept('zstr("blah")', ELSLInvalidType)
test(r'zstr(Key(u"xy\0zzy"))', Key(u'xy')) test(r'zstr(Key(u"xy\0zzy"))', Key(u'xy'))
test('typecast(Infinity, unicode)', u'Infinity') test('typecast(Infinity, unicode)', u'Infinity')