4 ms·
Weak arguments. Integer division is misleading. It uses the mathematical division operator for something that doesn't behave in that way. Having a separate 'in
by IshKebab 10y ago
Weak arguments.
Integer division is misleading. It uses the mathematical division operator for something that doesn't behave in that way. Having a separate 'integer division' operator (e.g. //) is much clearer.
Nobody could say that the pre/post increment aren't confusing. They feature heavily in 'trick question' C++ tests (along with undefined behaviour, type promotion and so on).
- dagw 10y agoInteger 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...
- zamalek 10y ago> It uses the mathematical division operator for something that doesn't behave in that way. Floating point does not behave in a mathematical way, either.[1][2] Unless your language uses fractional representation using big integers for real numbers. > that the pre/post increment aren't confusing The operator and retrieval of the variable are two separate operations that occur in the order that you read them. None less trivial than "I" before "E" except after "C". [1]: https://en.wikipedia.org/wiki/Single-precision_floating-point_format https://en.wikipedia.org/wiki/Single-precision_floating-poin... [2]: https://en.wikipedia.org/wiki/Arithmetic_underflow https://en.wikipedia.org/wiki/Arithmetic_underflow
- IshKebab 10y ago> Floating point does not behave in a mathematical way It approximately does. The only common way that newbies would get tripped up is something like (3 * 1/3) != 3. Maybe that is a case for not allowing == operator to operate on floats (use a function/keyword instead), but I think that may be a step too far.
- vonmoltke 10y agoI don't think that is a step too far. When I was writing embedded code, == and != were prohibited for floating-point values. I support such a rule in general.
- krylon 10y agoYears ago, I worked on software that dealt with computational geometry a lot, and I quickly found out the hard way that the equality operator is pretty much useless for floating point values. On the other hand, that is a property of floating point numbers, regardless of what language one uses.
- dragonwriter 10y ago> On the other hand, that is a property of floating point numbers, regardless of what language one uses. That's true, but, OTOH, having floating point rather than an exact representation be the default representation for values represented as decimal literals, and the only non-integer type supported by convenient operators is a language-specific "feature" of C that many other languages don't share. (Though, to be fair, there are also many newer and popular languages that do share both, and more just the first, of those features.)
- petee 10y ago> Nobody could say that the pre/post increment aren't confusing. I will - its one of the simplest concepts and far from any of C's real problems.
- tym0 10y agoGotta love that the answer to the comment "pre/post increment might be confusing" is "Git gud".