3 ms·
Integer division is misleading. For dynamic languages I kind of agree, but with static languages I think integer division is the right thing. int+int, int*int,
by dagw 10y ago
Integer division is misleading.
For dynamic languages I kind of agree, but with static languages I think integer division is the right thing. int+int, int*int, int%int and int-int all return an int, so having int/int returning a not_int (what type should it return? should 4/2 return a different type than 2/4?) would be equally confusing.
- IshKebab 10y agoSounds like a lack of imaginationg! I would suggest that it does not compile, and a different operator is required that makes it clear that it isn't 'normal' division. `//` is a good choice, except that is a comment in many languages. Maybe `_/` to indicate that it is a sort of 'floor division'.
- dagw 10y agoMaking int/int an illegal operation would solve some problems, but I doubt it would make things less confusing.
- IshKebab 10y agoI think it would simplify things. All you need to remember is: / = normal division _/ = division then round down Then the compiler enforces all the hard-to-remember rules, i.e. that you can't to normal division on integers. Floats would support both.
- 1wd 10y agoI like the Pascal way of using named operators like `div` (and `mod`, `xor`, `shr`, `shl`, `sar` etc.). Much easier to remember, read and type. Why does everything have to be cryptic symbols? This also frees up those symbols for use in more frequently used constructs. How often do you actually use those bit fiddling operators nowadays?
- AnimalMuppet 10y agoI want / for division for the same reason that I want + for addition. (Pascal doesn't make you say 'plus'...)
- 1wd 10y agoPascal has / for real division, but div for (truncated) integer division.
- AnimalMuppet 10y agoSo it had + for "integer addition, but can overflow", and what for "real" addition? My point is that separating "real" division from integer division is... a bit artificial? Not totally mistaken - there are differences - and yet calling one "real" is going a bit too far. All of these models of arithmetic suffer from the limitations of their representation. In all cases, you'd better be aware of what those limitations are...