4 ms·
The mathematical definition of "integer" includes the set of negative integers. That's the whole point, otherwise we could just use natural numbers. So making i
by purpleblue 4y ago
The mathematical definition of "integer" includes the set of negative integers. That's the whole point, otherwise we could just use natural numbers. So making ints signed makes the most sense otherwise there's an impedance mismatch that you need to explain away.
- benj111 4y agoBut neither signed or unsigned ints can represent all ints. It's a question of what subset of ints you want to be able to represent.
- pornel 4y agoIntegers can also be infinitely large. C doesn't use mathematical definitions, but its own.
- mcguire 4y agoThe mathematical definition of "integer" is unbounded above and below. The "numbers" used in computing systems are not mathematical numbers in any way.
- Asooka 4y agoThe numbers used in computing systems are normal mathematical numbers in the Z_2^32 field. The signed ones are shifted by half the range in the negative direction. There is nothing un-mathematical about them, people who have studied any math in university, i.e. all CS graduates, should have gone through an introductory algebra course that covers fields, rings, etc. So I'm not sure why people keep saying ints are not mathematical.
- tsimionescu 4y agoThat depends on the computing system. You're right if we're talking about the processor level (on any modern processor), but the "signed ints" in C or C++ are not a field at all, since INT_MAX + 1 is undefined, as are -INT_MIN and INT_MIN - 1 (and many many many multiplications). Also, as someone else was pointing out, unsigned short arithmetic is not well defined either.
- deleted 4y ago[deleted]
- mcguire 4y agoTo be honest, I adapted a phrase I usually use for floating point numbers. But anyway, "So making ints signed makes the most sense otherwise there's an impedance mismatch that you need to explain away" is a pretty poor argument when you're dealing with Z_2^32 (or 2^64) minus 2^31, with some operations becoming partial. (And that's assuming you're on a twos-complement machine; those goofy ones-complement boxes had positive and negative zero.)
- qsort 4y agoI know what you mean and I'm not saying this with any animus, but this is pretty funny: > people who have studied any math in university, i.e. all CS graduates, should have gone through an introductory algebra course that covers fields, rings, etc. > Z_2^32 field
- jcelerier 4y ago> The mathematical definition of "integer" is unbounded above and below. which is exactly why signed overflow is UB: if you use int, you say "I want something that behaves exactly like a mathematical integer in exchange of the promise of not working with quantities too large"
- andreareina 4y agoThat's the first time I've heard that reason for int overflow being UB, do you have a source for that?
- jcelerier 4y agohow do you call something that verifies those as axioms? https://gcc.godbolt.org/z/53ben65hf https://gcc.godbolt.org/z/53ben65hf
- qsort 4y agoI mean, it's not the only word in programming that's overloading a word with a mathematical definition. Just off the top of my head: - function - set - map - monad - vector - field - class Some of them are obviously referring to something else entirely. Some of them are kind of like the math thing but subtly different.
- jpfr 4y agoTensor. The term is used to mean “multi-dimensional array” in the machine learning community. That is however not a tensor.