diff options
author | Samuel Lidén Borell <samuel@kodafritt.se> | 2024-06-02 21:20:48 +0200 |
---|---|---|
committer | Samuel Lidén Borell <samuel@kodafritt.se> | 2024-06-02 21:20:48 +0200 |
commit | 580bf6130632f6855fddeea7b07c8401c56108f2 (patch) | |
tree | 4bd5e7cdb68408c52ad8df030f7f887c7d97def0 /notes/numeric_operations_type.txt | |
parent | db73835b12f41be8766384a1cdcc34a0848354dc (diff) | |
download | slul-main.tar.gz slul-main.tar.bz2 slul-main.zip |
Diffstat (limited to 'notes/numeric_operations_type.txt')
-rw-r--r-- | notes/numeric_operations_type.txt | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/notes/numeric_operations_type.txt b/notes/numeric_operations_type.txt new file mode 100644 index 0000000..53a760d --- /dev/null +++ b/notes/numeric_operations_type.txt @@ -0,0 +1,28 @@ +Which type should numeric operations use? + +Currently, it works like this: + + byte a = 250 + byte b = 10 + ... + byte x = (a + b) - 20 # this overflows, because a+b is computed as a byte + +And this is actually inefficient, because the operation would most likely be +performed using 32 or even 64 bit registers. The exception is when the +temporary variable for (a+b) is spilled to stack. + +Comparison to other languages +----------------------------- + +C (from reading the ANSI C spec): + - performs operations as int or a larger type + - promotes to the largest operand (appears to not care about the result type) + - unsigned wins when there are operands with mixed signedness + +Rust (from reading random blogs): + - all integer literals need to have a suffix, e.g. u8 + (except for the default, which I think is int?) + - all casts, even narrowing ones, have to be explicit + +FreePascal (don't remember this exactly): + - Promotes to Int64 in mixed-sign comparisons |