[cppx] How to avoid disastrous integer wrap-around

The time is 65535:65535 PM, and the temperature is a nice comfy 4294967295 degrees… Oops. Clearly some signed integer values have been promoted to unsigned somewhere, or perhaps the program has just used unsigned integer arithmetic incorrectly.

There are two main cures for that:

  • make sure that you’re only using unsigned arithmetic in expressions that involve unsigned type numerical values, or …
  • make sure that you’re only using signed arithmetic in expressions that involve unsigned type numerical values.

As of 2010 most programmers still choose the first cure, attempting to use unsigned arithmetic wherever there are unsigned numerical values involved, e.g. being careful to use size_t instead of int for some loop counter. I think the main reason is that it’s an old convention (which is propagated by e.g. the C++ standard library), a case of “frozen history”, but it may also have something to do with each intrusion of unsigned numerical values being viewed as a purely local problem, suggesting that a purely local fix, each time, is appropriate. This then leads to a bug-attracting mix of signed and unsigned type expression, which moreover are difficult to recognize as respectively signed and unsigned type, and are easy to get wrong.

Here I’m advocating the second cure, a single global convention: do not mix signed and unsigned numerical types. The unsigned types are for bit-level operations (only), while the signed types are for numerical values (only). But since the standard library was designed before the advantages of this clear usage separation were fully realized, and indeed at a time when some computer architectures still conferred some advantages to e.g. an unsigned size(), adopting this more rational and work-saving convention requires some support.

[… More] Read all of this posting →

[wordpress] WordPress replacing “C++” tag with “C#”!

It turned out that WordPress replaces all “C++” tags with “C#” tags in their presentation of postings about any particular theme, like the list of postings about programming.

Technically a “C++” tag is first transformed to the tag id “c” by removing all punctuation, and then the presentation of tag id “c” is as “C#” (not even with proper superscript like C#).

I understand that to people at large “C++” or “C#” or just “C” is much the same thing, really, so it shouldn’t matter what WordPress presented it as?

But this is akin to me posting about how I’m the proud owner of a Bentley, and WordPress informing the world that my blog is about how aroused I am as owner of a mare (female horse).

How does one get from “proud owner of Bentley”, to “aroused owner of mare”? First one, i.e. WordPress, generalizes (like “C++” → “c”). Proud, that must mean happy, surely, and Bentley, that must mean a means of transportation, surely. So, he means that he’s the happy owner of a means of transportation.

Then one, i.e. WordPress, specializes, incorrectly. Happy, oh that must mean aroused, and means of transportation, that must mean a mare, and even if that perhaps is not 100% precisely what he meant it does give the rough idea, surely?, and if we chose something else we’d offend someone else!

It’s all so needless.

Just don’t do it, WordPress.