ISO/IEC JTC1 SC22 WG21 N2281 = 07-0141 - 2007-05-02
Lawrence Crowl
Numeric literals of more than a few digits are hard to read. For example, compare 237498123 with 237499123 for equality and 237499123 with 20249472 for relative magnitude.
We propose to add the underscore character as a digit separator in numeric literals. The pervious examples become clearer as 237_498_123 with 237_499_123 and 237_499_123 with 20_249_472.
Bjarne Stroustrup has suggested using a space as a separator. While this approach is consistent with some presentation styles, it would likely make editing tools that grab "words" less reliable. Furthermore, the preprocessor syntax would need to change (section 2.9 below) with potential unforseen risk.
This proposal does not address binary literals or hexadecimal floating-point literals. We believe those should be addressed in a separate paper.
This approach has been implemented in the Ada programming language.
To the grammar, no changes are necessary. The preprocessing number tokens already admit an underscore via the non-terminal nondigit.
.
digite
signE
sign.
To the grammar, no changes are necessary. This section defines non-terminals used elsewhere.
a b c d e f g h i j k l m
n o p q r s t u v w x y z
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z _
0 1 2 3 4 5 6 7 8 9
To the grammar, edit as follows, permitting underscores between digits. (Note some renderings of HTML will overstrike the underscore with the markup for inserted text. Look for "more than usual" spacing in that case.)
_
digit0
_
octal-digit0x
hexadecimal-digit0X
hexadecimal-digit_
hexadecimal-digit1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
To the grammar, edit as follows, permitting underscores between digits. (Note some renderings of HTML will overstrike the underscore with the markup for inserted text. Look for "more than usual" spacing in that case.)
.
digit-sequence.
e
signopt digit-sequenceE
signopt digit-sequence+ -
_
digit