4 ms·
Most of this discussion revolves around integer overflow. Part of the problem is that most of the computer hardware is now twos-complement arithmetic. Programm
by Animats 6y ago
Most of this discussion revolves around integer overflow.
Part of the problem is that most of the computer hardware is now twos-complement arithmetic. Programmers think of that as part of the language. It's not, for C. C has run, in the past, on
- 36 bit ones complement machines (DEC and UNIVAC)
- Machines with 7-bit "char" (DEC)
- Machines with 9-bit "char" (UNIVAC, DEC)
- Machines where integer overflow yields a promotion to float (Burroughs)
- Many GPUs provide saturating arithmetic, where INT_MAX + 1 == INT_MAX.
Go, Java and Rust have explicit byte-oriented models with defined overflow semantics, but C does not. C has undefined overflow semantics.
Many years ago, around the time Ada was being defined, I wrote a note titled "Type Integer Considered Harmful". I was pushing the idea that
integer variables should all have explicit range bounds, not type names.
As in Ada, overflow would be checked. Intermediate variables in expressions would have bounds such that the intermediate value could not overflow without the final result overflowing and being caught. Intermediate values often have to be bigger than the operands for this to work.
This never caught on, partly because long arithmetic hardware was rare back then, but it illustrates the problem. Numeric typing in programming languages addresses a numeric problem with a linguistic solution. Hence, trouble. Bounds are the right answer numerically, but force people to think too hard about the limits of their values.
- PeterisP 6y agoExplicit bounds everywhere mean that those bounds need to be automatically checked every time instead of only where explicitly specified by the programmer. This leads to safer code at a nontrivial performance tradeoff, one which would not be acceptable for C.
- Too 6y agoYou could imagine bounds being baked into the types and checked at compile time. int{0..10} foo = 4 int{0..10} bar = 5 int{0..10} buzz = foo+bar // ERR: 10+10 potentially > 10 int{0..10} boom = wrapping_add(foo, bar) // OK int{0..100} sum = foo+bar // OK There are tools which can do this, for example Code Contracts in C#. It becomes rather tedious and verbose so is something usually only left for very safety critical code.
- scoutt 6y agoThis could be useful in C for some situations. That said, the above is completely useless if just only one of those values (ranges or assignments) is dynamic: you'll be back to carefully (and manually) checking you don't overflow.* *edit: unless your compiler automatically emits instructions to do the check at runtime; a thing that won't happen (and I don't want) in C.
- pjmlp 6y agoSomething that Ada, Modula and Pascal compilers are able to optimize away in most cases. C keeps targeting an hardware model that even most Arduinos are super computers by comparisasion.
- ndesaulniers 6y agoI've heard of wrap around and saturate, but promote to float? What?! Do you have more info on how that worked?
- irishsultan 6y agoSame way it does in Javascript?
- Animats 6y ago48-bit numbers. "The internal format of a single-precision floating-point number consisted of one unused bit, followed by the sign of the number, then the sign of the exponent, then a six-bit exponent, then 39-bit mantissa. The bias of the exponent was such that it could be considered to be in excess-32 notation as long as the mantissa was considered to be a binary integer instead of a binary fraction. This allowed integers to also be interpreted as unnormalized floating-point numbers."[1] So there was only one numeric format at the hardware level. Convenient. [1] http://employees.oneonta.edu/zhangs/csci201/general%20Floating%20Point%20Format.htm http://employees.oneonta.edu/zhangs/csci201/general%20Floati...