+ − × ÷
Despite being rather long at around 6000 words, the Wikipedia article on ASCII doesn’t seem to give any clues about why its character set only includes two of the four basic arithmetic operations:
- are included, but not
÷. The standard was first published in 1963, so perhaps no-one can remember. Perhaps typewriters are to blame.
ASCII shouldn’t matter any more, now that Unicode encoded as UTF-8 has been around for twenty years. What’s more, as first reported by Google in 2008, UTF-8 is the most common encoding for web pages, which is how pages like this one can display multiplication and division symbols without using the HTML entities (
÷) in the page source.
For some reason, modern programming languages remain steadfastly faithful to ASCII characters in their language specifications, leaving us to perform multiplication and division with substitute characters:
/, years after the point that this seems necessary.
Scala is interesting, among programming languages, because of the way you can define operators and use them as if they were part of the language. You can add
× to numeric types, as an alternative multiplication operator, although perhaps with operator precedence issues (which parentheses deal with well enough). Here’s a simplified example, on the Scala console:
The only strange thing is that this isn’t built-in, for all of the ASCII operators that are really just approximations of the exact symbol, such as
≠ instead of
It would of course be inconvenient if you always had to type these in, with a keyboard that lacks these characters, but there are other options. In Scala, you can already use scalariform with the rewritearrowsymbols option, which replaces ASCII art arrows (
<-) with the Unicode equivalents that Scala does support (
←). Nice, but something of a work-around that could be better implemented using an IDE or OS-level auto-correct text input feature.
ASCII art API methods
Note that the conventional ASCII art arithmetic operators, such as
!= are not the same kind of thing as the ASCII art API methods that appear in some Scala APIs. My favourites are
<|*|>, the Scalaz ‘spaceship operator’, and
><>, the Play framework ‘fish operator’.
Arithmetic symbols meanwhile, such as
≥, are sufficiently fundamental for kids to learn at school. The same can’t be said for any Scala libraries, however FP-hip they may be. That’s not what this article is about.
I will concede that mathematical logic symbols are a grey area, because they are specialist notation (not taught to school children). The symbols for disjunction and conjunction (
∨), for example, are more than 100 years old), though, so it wouldn’t really be weird to want to use them instead of
|| in programming languages.
One more minus
+ − -
At the start of this article I glossed over one thing, suggesting that only the multiplication and division signs were missing from ASCII. It turns out that a hyphen is not the same character as a minus sign, typographically or in Unicode. The first two symbols above are arithmetic symbols, but the hyphen on the right (the third) just doesn’t fit.
I guess our newfangled number-crunching machines are stuck with the legacy of a nineteenth-century novelist’s tool after all. Here’s hoping that a programming language designer with an eye for typography takes up this particular challenge sooner, rather than later.