From 45ef41a78b8dd0564b9d704ce2c4ad3c1b983ae5 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 18 Nov 2026 00:59:34 +0100 Subject: more consistent integer and floating point arithmetics without undefined behavior * `-1^*0` is 1, just like `1^*0` * Floating point arithmetics use inf and NaN instead of errors. In particular `1./0` and `1.^/0` no longer throw errors. We better stay with IEEE 754 semantics than striving for consistency with integer arithmetics. Lua does the same with its hybrid integer/float type system. The check for 0 was flawed anyway. * C has lots of undefined behavior with regard to shifts, so we normalize them just like Lua does: Negative displacements shift in the other direction. A displacement >= 64 is always 0. * Integers are allowed to overflow silently. The overflow behavior is undefined in C, but since we assume two's complement representation, they will actually be defined in practice. This is also what Lua does. Alternatively we could use checked additions, subtractions and multiplications to catch all overflows and throw errors. * There should be no undefined behavior in arithmetics in SciTECO. --- tests/testsuite.at | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/testsuite.at b/tests/testsuite.at index 8a81bab..3f55e15 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -94,20 +94,43 @@ TE_CHECK([[2^R 23=]], 1, ignore, ignore) TE_CHECK([[23 (2^R)\^D .-5"N(0/0)']], 0, ignore, ignore) AT_CLEANUP +AT_SETUP([Multiplication]) +AT_SKIP_IF([test $TECO_NUMBER -lt 64]) +# Overflows should be modulo 2^*64. +# All real architectures should use two's complement. +TE_CHECK([[(@0ffffffffffffff ^< 8):=]], 0, [-256], ignore) +AT_CLEANUP + +AT_SETUP([Division and modulo]) +TE_CHECK([[5/2:=]], 0, [2], ignore) +TE_CHECK([[1/0:=]], 1, ignore, ignore) +TE_CHECK([[1^/0:=]], 1, ignore, ignore) +# floating point numbers +TE_CHECK([[5./2:=]], 0, [2.5], ignore) +TE_CHECK([[1./0:=]], 0, [inf], ignore) +TE_CHECK([[1.^/0:=]], 0, [nan], ignore) +TE_CHECK([[0./0:=]], 0, [nan], ignore) +AT_CLEANUP + AT_SETUP([Exponentiation]) -TE_CHECK([[-1^*0 - (-1)"N(0/0)']], 0, ignore, ignore) TE_CHECK([[-1^*-5 - (-1)"N(0/0)']], 0, ignore, ignore) TE_CHECK([[0^*-5=]], 1, ignore, ignore) +TE_CHECK([[-1^*0 - 1"N(0/0)']], 0, ignore, ignore) TE_CHECK([[0^*0 - 1"N(0/0)']], 0, ignore, ignore) TE_CHECK([[1^*-5 - 1"N(0/0)']], 0, ignore, ignore) TE_CHECK([[2^*-5 - 0"N(0/0)']], 0, ignore, ignore) # floating point numbers TE_CHECK([[2.^*-5:=]], 0, [0.03125], ignore) +TE_CHECK([[0.^*-5:=]], 0, [inf], ignore) AT_CLEANUP AT_SETUP([Bit shifting]) -TE_CHECK([[(@0ff^<8) - @0ff00"N(0/0)]], 0, ignore, ignore) -TE_CHECK([[(@0ffaa^>8) - @0ff"N(0/0)]], 0, ignore, ignore) +TE_CHECK([[(@0ff ^< 8) - @0ff00"N(0/0)]], 0, ignore, ignore) +TE_CHECK([[(@0ff ^< 128) - 0"N(0/0)]], 0, ignore, ignore) +TE_CHECK([[(@0ff ^> -128) - 0"N(0/0)]], 0, ignore, ignore) +TE_CHECK([[(@0ff ^> -8) - @0ff00"N(0/0)]], 0, ignore, ignore) +TE_CHECK([[(@0ffaa ^> 8) - @0ff"N(0/0)]], 0, ignore, ignore) +TE_CHECK([[(@0ffaa ^< -8) - @0ff"N(0/0)]], 0, ignore, ignore) AT_CLEANUP AT_SETUP([Missing left operand]) -- cgit v1.2.3