math: Give a proper error for division by zero

This errored out *later* because the result was infinite or NaN, but
it didn't actually stop evaluation.

I'm not sure if there is a way to get floating point math to turn an
infinity back into something that doesn't depend on a literal
infinity, but division by zero conceptually isn't a thing we can
support.

There's entire branches of maths dedicated to figuring out what
dividing by "basically zero" means and we don't have to get into it.
This commit is contained in:
Fabian Boehm
2022-09-08 16:50:18 +02:00
parent 7f1e9bf57f
commit 5edba044a3
4 changed files with 29 additions and 7 deletions

View File

@@ -151,9 +151,14 @@ not math -s 12
not math 2^999999
# CHECKERR: math: Error: Result is infinite
# CHECKERR: '2^999999'
not math 1 / 0
# CHECKERR: math: Error: Result is infinite
# CHECKERR: '1 / 0'
printf '<%s>\n' (not math 1 / 0 2>&1)
# CHECK: <math: Error: Division by zero>
# CHECK: <'1 / 0'>
# CHECK: < ^>
printf '<%s>\n' (math 1 % 0 - 5 2>&1)
# CHECK: <math: Error: Division by zero>
# CHECK: <'1 % 0 - 5'>
# CHECK: < ^>
# Validate "x" as multiplier
math 0x2 # Hex
@@ -259,8 +264,9 @@ math pow 2 x cos'(-pi)', 2
# This used to take ages, see #8170.
# If this test hangs, that's reintroduced!
math 'ncr(0/0, 1)'
# CHECKERR: math: Error: Result is infinite
# CHECKERR: math: Error: Division by zero
# CHECKERR: 'ncr(0/0, 1)'
# CHECKERR: ^
# Variadic functions require at least one argument
math min