Bitwise»Forums
Nicolas Léveillé
22 posts
Developping desktop music applications.
RFC Number grouping (0x1200_0070, 123_456, 10_000.00) proposal
Edited by Nicolas Léveillé on
Here is a proposal to allow number groupings in numeric literals.

It allows the optional introduction of a single character (here suggested as _) at any position separating consecutive digits shown in the following examples:

0x1200_0070 == 0x12000070
123_456 == 123456
10_000.40 == 10000.42

Problem being addressed:
- Reducing frequency of mistakes in numbers with repeated strings of similar digits
- Readability of numbers

Context:

Numeric constants with lots of repeated numbers happen especially with addresses and 64bit numbers.
Embedded platforms for instance have memory mapped addresses that I've caught myself making mistakes at entry.
Human languages have introduced typographic markings for number groupings: thin spaces, commas and dots.
Number groupings vary also between cultures, some mark the thousandth, some the ten-thousandth.
Programming languages of the C family: C++14 has introduced with the ' has an optional number grouping.

See http://en.cppreference.com/w/cpp/language/integer_literal

The character _ has been chosen in some documentations (I've seen it used in hardware manuals)

Value:

Increased readability, optional

Costs:

- Implementation cost: small (lexer change only)
- Maintenance cost: small (does not interact with other language features)
- User cost: using the feature is optional, so users who don't like it can ignore it. One character is easy to learn and probably isn't too taxing even if it differs from other languages.
- Opportunity cost: the chosen character is can't be used anymore as a digit

Per Vognsen
49 posts / 1 project
RFC Number grouping (0x1200_0070, 123_456, 10_000.00) proposal
Edited by Per Vognsen on
Thanks for the detailed proposal. This is simple and worth doing.

By the way, you don't have to make highly formal proposals. Feel free to just raise ideas casually and we can discuss them back and forth. In this case I don't think there's much to discuss; you already covered everything.