From 23b27bd3af42872de7d007e6749f1b0fb814cec9 Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Sun, 15 May 2016 18:21:24 +0200 Subject: [PATCH] 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. --- lslopt/lslbasefuncs.py | 7 ++++--- testfuncs.py | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lslopt/lslbasefuncs.py b/lslopt/lslbasefuncs.py index f9bdccf..2a987e2 100644 --- a/lslopt/lslbasefuncs.py +++ b/lslopt/lslbasefuncs.py @@ -669,8 +669,6 @@ def div(a, b, f32=True): if b == 0: raise ELSLMathError if ta in (int, float): - if math.isnan(a): # NaN/anything gives math error - raise ELSLMathError if ta == int and tb == int: # special case 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 return - a//-b # that's -(a//-b) not (-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: a = v2f(a) b = ff(b) diff --git a/testfuncs.py b/testfuncs.py index 2a3920e..8eb17fe 100644 --- a/testfuncs.py +++ b/testfuncs.py @@ -493,6 +493,9 @@ def do_tests(): shouldexcept('div(1.0, 0.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) test(r'zstr(Key(u"xy\0zzy"))', Key(u'xy')) test('typecast(Infinity, unicode)', u'Infinity')