aboutsummaryrefslogtreecommitdiffhomepage
path: root/notes/numeric_operations_type.txt
blob: 53a760d9e74f4627a7eebb74207b2499d14cd4b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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