diff options
| author | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2026-11-18 00:59:34 +0100 |
|---|---|---|
| committer | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2026-11-18 00:59:34 +0100 |
| commit | 45ef41a78b8dd0564b9d704ce2c4ad3c1b983ae5 (patch) | |
| tree | 903774e7beeb359c8afc848f97e9d57ae3515ca1 /src/expressions.c | |
| parent | 40b7968a3d7435a80678a169a1e811431ff7a7ad (diff) | |
more consistent integer and floating point arithmetics without undefined behaviormaster-fmsbw-ci
* `-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.
Diffstat (limited to 'src/expressions.c')
| -rw-r--r-- | src/expressions.c | 70 |
1 files changed, 50 insertions, 20 deletions
diff --git a/src/expressions.c b/src/expressions.c index 84d76cb..68049eb 100644 --- a/src/expressions.c +++ b/src/expressions.c @@ -32,6 +32,16 @@ #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. * This is OK since we're currently singleton. @@ -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: |
