aboutsummaryrefslogtreecommitdiffhomepage
path: root/notes/numeric_operations_type.txt
diff options
context:
space:
mode:
Diffstat (limited to 'notes/numeric_operations_type.txt')
-rw-r--r--notes/numeric_operations_type.txt28
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