4

Ever since I learned that clang was able to compile c++ source files written in Unicode, I began to use it heavily when writing math-related code. Compare

uₙ₊₁ᵖ = A*uₙ + B*uₙ₋₁;
uₙ₊₁ᶜ = π * Aₜₒₜ;
uₙ₊₁ = uₙ₊₁ᵖ + uₙ₊₁ᶜ;

and

u_n1_p = A*u_n + B*u_n_1;
u_n1_c = pi * A_tot;
u_n1 = u_n1_p + u_n1_c;

For me it's just like night and day: I understand the first piece of code just by reading it, whereas I simply don't want to read the other one

I know that Python3 and Ruby allow Unicode source files so it seems that this feature is spreading.

Objections can be made against this kind of practice: e.g. not all fonts support these characters, your source file depends of the encoding your are using, and you have to actually copy/paste (for instance) the Unicode character from somewhere into in your text editor. However I think the gain in readability is really great.

Now as you can see on this page not all (not even latin) letters are available in subscripts and superscripts. Worse, these were absolutely not intended for this usage of writing math in a source file (see here)

Hence my questions:

  1. Do you use Unicode for math-related code ? What do you think of this usage ?

  2. Is there any way to turn a character in subscript or superscript ? (similar to combining characters used for diacritics)

4
  • 1
    As for 2. No. "Superscript", as you perceive it, is an attribute. Unicode does not "do" attributes. It is not similar to diacritics (it's probably your keyboard driver that allows typing base character, then accent, to get an accented character).
    – Jongware
    May 5, 2014 at 6:38
  • @Jongware : The Unicode block U0300 contains "Comibining Diacritical Marks". They are codepoints which add a diacritical mark to the preceding base character. This is independent from keyboard drivers.
    – MSalters
    May 5, 2014 at 8:47
  • @MSalters. True (But irrelevant to the question because this does not really "combine" two characters. These characters are designed in the font with a negative width, so they appear in the correct position. Additionally, an OpenType aware application may substitute the two characters by a single designed glyph-with-accent.)
    – Jongware
    May 5, 2014 at 10:39
  • I think it's a good practice. I do it all the time. However, some people are concerned about security aspects for potential of abuse. Also it depends on the language. Python (3) is more open about use of unicode characters, while gcc C++ allows much less.
    – Sohail Si
    Mar 16, 2023 at 10:50

2 Answers 2

2

I would say NO unless

  • internal only code, and without polluting public APIs
  • whole team agrees it is of significant benefit
  • math intensive functions only (not for fairly trivial math tasks)
  • separate out from business logic/interface code
  • limited to some subset of unicode (perhaps just the subscripts and greek symbols)

And even if all of these requirements were met I would weight the hassle of use against the increased readability and tend towards sticking with ASCII.

Make sure you give your team strict guidelines on when it's acceptable, so that you don't get into a situation where every for loop uses iₙ.

My computer doesn't seem to like the 'LATIN SUBSCRIPT SMALL LETTER N' (U+2099) character you've used and just renders it as a box which greatly reduces readability. Make sure that your tools/fonts support this style of editing.

PEP8 states that unicode characters shouldn't be used for identifiers within the standard libraries - they probably have a good reason why.

In summary - NO unless you have a really good reason, and then only in separate math intensive modules. I suppose I could be convinced it was valuable under certain scenarios.

0

My question to OP is: how long is ever since ?

Good questions. Unicode is with us for a long time now, so why should programming be forced to use american style ASCII without any accents? While working, and studying, C# and Javascript, I am discovering that those languages are Unicode-aware. C# defines two interesting constants in System.Math :

    //     Represents the natural logarithmic base, specified by the constant, e.
    public const double E = 2.7182818284590451;

    //     Represents the ..., specified by the constant, π.
    public const double PI = 3.1415926535897931;

Here we see a unicode comment for π, but not for ℯ. Wouldn't it be nice to also have both constants with unicode identifiers, to be able to write, eg.:

 double circumference = 2 * Math.π * r;

The case for e is complicated, as it is often used with exponents, that are always hard to express on a single line. Also, the unicode representations for ℯ (U+212F), the log base, and ℮ (U+212E), the charge of an electron, are dubious. I could not really find confirmation of the exact correct unicode for the elementary charge.

I guess that there aren't really Unicodes for such constants, other than the usual greek characters, which should be looked up in the Unicode greek alphabet.

My conclusion for System.Math is to keep the ascii identifiers E and PI, and add the unicode identifier π.

As for OP question 1, I would also recommend to use variables for math using the greek alphabet instead of coercing e.g. φ to phi, δ to delta or d, like:

var x = 2 * π * sin(φ);

Such code is definitely NOT harder to maintain than the ascii version.

However I like the technological progress from ascii to unicode, I would still recommend programming in plain old us-english. Variable names and comments in spanish, hungarian, no thanks. Perhaps nice for the original programmer, but it makes it more difficult to collaborate. (disclosure: I am not a native-english speaker) And, at least in C# and Javascript, the reserved words are english only: for, if, else, ...

So: keep it simple: unicode for the greek alphabet: yes, for math symbols. Unicode for multilanguage(accents): no, please use english.

Super/subscripts: actually I find this a great idea. The problem I see is in the complexity: the n+1 in the subscript is intended as part of the variable name, but looks like a C#/C++ operation. Just do not use operator-like glyphs in a name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.