4 ms·
This is wrong on so many levels it's hard to even start criticizing. TL/DR: the guy wants more or less the semantics of decimal floating point numbers (with NaN
by acqq 15y ago
This is wrong on so many levels it's hard to even start criticizing. TL/DR: the guy wants more or less the semantics of decimal floating point numbers (with NaNs and all) in integers, and believes that such "integers" would be "better" in his words"to make it difficult for those specialized integers to flow into allocation functions, array indices, etc. That sort of thing is the root of a lot of serious bugs in C/C++ applications."
In fact, integers that we have in current CPUs are product of let's say at least 50 years of engineering and they are optimum, even if somebody declares them "the root of a lot of serious bugs in C/C++ applications." I've got the news for him: a lot of other and older languages have the same behavior because that's the local optimum for a hardware implementation and it will remain so even once we have decimal fp numbers of the same speed as integers (even if we ignore that actually we won't). Before two's complement there were other integer variants implemented in hardware and they turned out to be less functional as two's complement representation.
Then gives the example that if this code
result = result * 10 + cursor - '0';
were evaluated as
result * 10 + (cursor - '0')
there won't be overflow, totally ignoring that the languages often have exactly defined order of evaluation and that if the language has the defined order of evaluation from left to right for + and - then the only proper evaluation is only
(result * 10 + cursor) - '0'
It's as simple as that.
Then he ends with the loop that wouldn't work with floating point NaNs (and therefore wouldn't fit in NaNs for integers too):
for (x = INT_MIN; x != iNaN; x++)
because every number is not equal to NaN, so the above loop would never end.
I'd say everything he wrote is not even wrong.
- angersock 15y agoThe biggest problem is that the author seems to be completely ignorant (willfully or otherwise) of the issues involved at the hardware level. They go so far as to point out that high-level languages shouldn't have these limitations (sure, agreed), but then is baffled that something like C/C++ does (and should). C maps more-or-less directly onto assembly, which in turn maps more-or-less directly onto command bit vectors for an ALU. There is no magic decision in the industry to use twos-complement, wrapping arithmetic, or fixed-size integers (which map onto machine words). This person needs to spend some quality time with low-level architectural stuff and gain a better understanding of why things are the way they are. (not that the points are bad, necessarily, for a DSL or something--but stay out of our systems languages!) EDIT: Okay, author is clearly somewhat knowledgeable about the field, but still... these are interesting ideas that don't really make sense for low-level stuff. It's not a matter of allocating enough memory (for bignums or whatever)--it's how the mapping occurs to the actual hardware instructions and datatypes.
- shasta 15y agoYou and others seem to be missing that he is talking about the default 'int' type. If you want a specific overflow behavior, he implies that would be under a different name. I agree -- 'int' doesn't need to be the type that maps most efficiently to the hardware, even in a "systems language". It can and should be a fairly efficient type that has reasonable semantics.
- rayiner 15y ago> The biggest problem is that the author seems to be completely ignorant (willfully or otherwise) of the issues involved at the hardware level. There's not any issues at the hardware level. Integer arithmetic is simplistic mostly because languages that run on current CPU's have simplistic integer semantics. It's not because more complicated semantics would be impossible to implement at the hardware level. Take a look at some of the complex integer instructions that SSE supports with single-cycle latency.
- caf 15y agoModifying a 2s complement ALU so that if the overflow bit is set, then every bit of the result is set to 1 would be trivial. It's just another n OR gates, and those are pretty cheap (OK, you'd probably want an AND against a control bit, too). The author doesn't ignore that this would require a change at the hardware level - he says "Processor architectures will have to add a family of integer math instructions that properly propagate NaN values..." - and he is right that the change would not be a costly one.
- repsilat 15y ago> that's the local optimum for a hardware implementation and it will remain so even once we have decimal fp numbers of the same speed as integers This doesn't preclude the use of twos complement, and twos complement isn't enforced (or mentioned) in C's standard. It doesn't need to be expensive, either - as said in the article, "These should be simple ALU hacks." I don't see any reason why these operations on processors would be slower than the ones we have already. I wouldn't be surprised if they used a few more transistors, but on the scale of today's processors it won't cost anything at all compared to all of the silicon required for instruction pipelining, branch prediction and out-of-order execution. > a lot of other and older languages have the same behavior This isn't true either. If you'd take the time to read the other posts on the site (and probably the LLVM guide to undefined behaviour[1]) you'll find that the C and C++ standards place quite different restrictions on the behaviour of integers than, say, Java (which is by comparison rather strict). The loop that you say "would never end"? It would end. When `x` overflows it'll be set to `iNaN`, and the loop will exit. Your "order of operations" complaint? He's talking about simple, unobvious errors with a real-life example. He then proposes semantics for a new language that would solve this problem. What's your complaint here? On the whole I thought John came up with a bunch of ideas, most of them good, almost all of them better than the status quo. I thought your post was the exactly the sort of knee-jerk response I've come to expect from Reddit. [1]: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
- acqq 15y ago"The loop that you say "would never end"? It would end. When `x` overflows it'll be set to `iNaN`, and the loop will exit." I see you don't have experience with NaN semantics: http://en.wikipedia.org/wiki/NaN http://en.wikipedia.org/wiki/NaN "A comparison with a NaN always returns an unordered result even when comparing with itself." If you say "but iNaNs would be different" you already missed the point.
- repsilat 15y agoThe author is proposing semantics for instruction sets and languages that don't exist. He could have said "`!(x is iNaN)`", or "`!isINAN(x)`" instead of "`x != iNaN`", but he didn't. Maybe he should have, I'm sure he didn't give it much thought. Nowhere does he say that the semantics of his `iNaN` are exactly analogous to the those of existing floating-point NaNs, though. Implying that he did so that you have one more nit to pick damages the tone of conversation.
- rcfox 15y ago1) regehr isn't saying that he doesn't like two's complement, he's saying that he doesn't like two's complement wraparound. He's saying that it would probably make sense for integers to operate linearly or fail if they get to a point where they will become non-linear. 2) Arguing to keep a local optimum seems silly to me. Why not reach for the global optimum? 3) He's not arguing for a change in the order of evaluation, he's identifying a situation that would work with integers that operate linearly and showing that it doesn't work when integers behave non-linearly (ie: around INT_MAX and INT_MIN).