On Monday, 24 November 2014 at 16:45:35 UTC, Ola Fosheim Grøstad
wrote:
On Monday, 24 November 2014 at 16:00:53 UTC, ketmar via
Digitalmars-d wrote:
this *is* overflow. D just has overflow result defined.
So it basically is and isn't modular arithmetic at the same
time?
Overflow is part of modular arithmetic. However, there is no
signed and unsigned modular arithmetic, or, more precisely, they
are the same.
Computer words just aren't a good representation of integers.
You can either use modular arithmetic, which follows the common
arithmetic laws for addition and multiplication (commutativity,
associativity, etc., even most non-zero numbers have a
multiplicative inverse), but break the common ordering laws (a >=
0 && b >= 0 implies a+b >= 0).
Or you can use some other order preserving arithmetic (e.g.
saturating to min/max values), but that breaks the arithmetic
laws.
I think Ada got this right by providing the ability to specify
the modulo value, so you can define:
type Weekday is mod 7;
type Byte is mod 256;
A solid solution solution is to provide «As if Infinitely
Ranged Integer Model» where the compiler figures out how large
integers are needed for computation and then does overflow
detection when you truncate for storage:
http://resources.sei.cmu.edu/library/asset-view.cfm?assetid=9019
You could just as well use a library like GMP.