Bitwise » Forums » RFC Number grouping (0x1200_0070, 123_456, 10_000.00) proposal
uucidl
Nicolas Léveillé
22 posts

Developping desktop music applications.

#14985 RFC Number grouping (0x1200_0070, 123_456, 10_000.00) proposal
6 months ago Edited by Nicolas Léveillé on April 18, 2018, 6:24 a.m.

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

pervognsen
Per Vognsen
49 posts / 1 project
#14986 RFC Number grouping (0x1200_0070, 123_456, 10_000.00) proposal
6 months ago Edited by Per Vognsen on April 18, 2018, 7:42 a.m.

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.