diff options
-rw-r--r-- | src/parseTools.js | 4 | ||||
-rw-r--r-- | tests/test_core.py | 27 |
2 files changed, 29 insertions, 2 deletions
diff --git a/src/parseTools.js b/src/parseTools.js index c0c8608c..ddfb9d01 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -1520,8 +1520,8 @@ function getFastValue(a, op, b, type) { } return '(Math.imul(' + a + ',' + b + ')|0)'; } - } else { - if (a == '0') { + } else { // div + if (a == '0' && !(type in Runtime.FLOAT_TYPES)) { // careful on floats, since 0*NaN is not 0 return '0'; } else if (b == 1) { return a; diff --git a/tests/test_core.py b/tests/test_core.py index 991f43a9..ea2fe49e 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1372,6 +1372,33 @@ Succeeded! ''' self.do_run(src, '*1,10,10.5,1,1.2340,0.00*\n0.50, 3.30, 3.30, 3.30\nsmall: 0.0000010000\n') + def test_zerodiv(self): + self.do_run(r''' + #include <stdio.h> + int main(int argc, const char* argv[]) + { + float f1 = 1.0f; + float f2 = 0.0f; + float f_zero = 0.0f; + + float f3 = 0.0f / f2; + float f4 = f2 / 0.0f; + float f5 = f2 / f2; + float f6 = f2 / f_zero; + + printf("f3: %f\n", f3); + printf("f4: %f\n", f4); + printf("f5: %f\n", f5); + printf("f6: %f\n", f6); + + return 0; + } + ''', '''f3: nan +f4: nan +f5: nan +f6: nan +''') + def test_isnan(self): src = r''' #include <stdio.h> |