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. --- doc/sciteco.7.template | 23 +++++++++++++++-- src/expressions.c | 70 +++++++++++++++++++++++++++++++++++--------------- tests/testsuite.at | 29 ++++++++++++++++++--- 3 files changed, 97 insertions(+), 25 deletions(-) diff --git a/doc/sciteco.7.template b/doc/sciteco.7.template index 7a10f09..3971b71 100644 --- a/doc/sciteco.7.template +++ b/doc/sciteco.7.template @@ -1016,8 +1016,8 @@ however. In this specific build, numbers are @TECO_NUMBER@-bit. .LP .SCITECO_TOPIC integer float -A \*(ST number can be either a signed integer or floating point -number \(em it is dynamically typed. +A \*(ST number can be either a signed integer (two's complement) +or floating point number (according to IEEE 754) \(em it is dynamically typed. Integer constants create integers and floating point constants create floats. Operators create integers if all operands are integers, @@ -1076,6 +1076,10 @@ Floating point constants cannot currently contain exponents. Floating point constants beginning with \(lq0.\(rq are not octal. \# But ^O0.333 would be. You can also write e.g. @0ff.ff for hexadecimal \# floating point constants. But this is pretty useless to my knowledge. +.SCITECO_TOPIC infinity NaN +There is currently no special command or construct to represent +infinity or NaN (Not a Number), but you can use \(lq(0./0)\(rq +to get a NaN value and \(lq(1./0)\(rq to get a floating point infinity value. .LP A complete constant is pushed on the number stack, either as an integer or float. @@ -1091,6 +1095,7 @@ you can push a sequence of integers by space-separating them The following binary operators are supported with descending precedence, whereas borders separate operators of the \fIsame\fP precedence: +. .SCITECO_TT 90n .TS box,tab(;); @@ -1156,6 +1161,20 @@ Binary OR. T};\^ .TE .SCITECO_TT_END SP +. +.LP +.SCITECO_TOPIC overflow +When signed integers overflow the result is as if the operation was +performed modulo 2^*@TECO_NUMBER@. +Integer divisions by 0 produce an error. +Floating point divisions by 0 however result in infinity according to +IEEE 754. +Other values like \(lq0.0/0\(rq are undefined and result in special +NaN (not a number) floats. +A shift operation with a negative displacement shifts in the opposite +direction, i.e. \(lq1 ^> -1\(rq is equivalent to \(lq1 ^< 1\(rq. +Left shifts with a displacement of larger than or equal to @TECO_NUMBER@ +are always guaranteed to be 0. .LP .SCITECO_TOPIC associativity All binary operators are \fIleft-associative\fP, i.e. associate diff --git a/src/expressions.c b/src/expressions.c index 84d76cb..68049eb 100644 --- a/src/expressions.c +++ b/src/expressions.c @@ -31,6 +31,16 @@ #include "qreg.h" #include "expressions.h" +/* + * All real-life architectures have negative integers in two's complement. + * C23 will demand that as well. + * If this would not be given, the result of multiplication and addition + * overflows would be undefined. + */ +#if (-1 & 3) != 3 +#warning "Negative integers are not in two's complement" +#endif + /* * Number and operator stacks are static, so * they can be passed to the undo token constructors. @@ -224,15 +234,14 @@ teco_expressions_calc(GError **error) switch (op) { case TECO_OP_POW: { if (vleft_is_float || vright_is_float) { + /* + * NOTE: 0.0^*-1 will be Inf, instead of throwing an error. + */ teco_expressions_push(pow(vleft_is_float ? vleft.f : vleft.i, vright_is_float ? vright.f : vright.i)); break; } - if (!vright.i) { - teco_expressions_push(vleft.i < 0 ? -1 : 1); - break; - } if (vright.i < 0) { if (!vleft.i) { g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED, @@ -266,34 +275,40 @@ teco_expressions_calc(GError **error) break; case TECO_OP_DIV: - if (!(vright_is_float ? vright.f : vright.i)) { - g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED, - "Division by zero"); - return FALSE; - } - if (vleft_is_float || vright_is_float) { + /* + * NOTE: 1.0/0 will be Inf instead of throwing an error. + */ teco_expressions_push((vleft_is_float ? vleft.f : vleft.i) / (vright_is_float ? vright.f : vright.i)); break; } - teco_expressions_push(vleft.i / vright.i); - break; - - case TECO_OP_MOD: - if (!(vright_is_float ? vright.f : vright.i)) { + if (!vright.i) { g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED, - "Remainder of division by zero"); + "Division by zero is undefined"); return FALSE; } + teco_expressions_push(vleft.i / vright.i); + break; + + case TECO_OP_MOD: if (vleft_is_float || vright_is_float) { + /* + * NOTE: 1.0^/0 will be NaN instead of throwing an error. + */ teco_expressions_push(fmod(vleft_is_float ? vleft.f : vleft.i, vright_is_float ? vright.f : vright.i)); break; } + if (!vright.i) { + g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED, + "Remainder of division by zero is undefined"); + return FALSE; + } + teco_expressions_push(vleft.i % vright.i); break; @@ -318,13 +333,28 @@ teco_expressions_calc(GError **error) break; case TECO_OP_SHL: - teco_expressions_push((vleft_is_float ? (teco_int_t)vleft.f : vleft.i) << - (vright_is_float ? (teco_int_t)vright.f : vright.i)); + if (vleft_is_float) + vleft.i = vleft.f; + if (vright_is_float) + vright.i = vright.f; + if (vright.i < 0) + /* undefined behavior in C, so we shift the other direction like in Lua */ + teco_expressions_push(vleft.i >> -vright.i); + else + /* left shift larger than n bits is undefined - always return 0 */ + teco_expressions_push(vright.i < sizeof(vleft.i)*8 ? (vleft.i << vright.i) : 0); break; case TECO_OP_SHR: - teco_expressions_push((vleft_is_float ? (teco_int_t)vleft.f : vleft.i) >> - (vright_is_float ? (teco_int_t)vright.f : vright.i)); + if (vleft_is_float) + vleft.i = vleft.f; + if (vright_is_float) + vright.i = vright.f; + if (vright.i < 0) + /* undefined behavior in C, so we shift the other direction like in Lua */ + teco_expressions_push(-vright.i < sizeof(vleft.i)*8 ? (vleft.i << -vright.i) : 0); + else + teco_expressions_push(vleft.i >> vright.i); break; case TECO_OP_AND: 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