-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cpp) improve number literals in C++ (#3847)
- Loading branch information
1 parent
5bb7114
commit fdd24e9
Showing
4 changed files
with
148 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,55 @@ | ||
<span class="hljs-comment">/* digit separators */</span> | ||
<span class="hljs-type">int</span> number = <span class="hljs-number">2'555'555'555</span>; <span class="hljs-comment">// digit separators</span> | ||
<span class="hljs-type">float</span> exponentFloat = <span class="hljs-number">.123'456e3'000</span>; <span class="hljs-comment">// digit separators in floats</span> | ||
<span class="hljs-type">float</span> suffixed = <span class="hljs-number">3.000'001'234f</span> <span class="hljs-comment">// digit separators in suffixed numbers</span> | ||
<span class="hljs-type">char</span> word[] = { <span class="hljs-string">'3'</span>, <span class="hljs-string">'\0'</span> }; <span class="hljs-comment">// make sure digit separators don't mess up chars</span> | ||
<span class="hljs-type">float</span> negative = <span class="hljs-number">-123.0f</span>; <span class="hljs-comment">// negative floating point numbers</span> | ||
<span class="hljs-comment">/* Floating-point literals. */</span> | ||
<span class="hljs-comment">// Decimal.</span> | ||
<span class="hljs-number">1.</span> | ||
<span class="hljs-number">+12.</span> | ||
<span class="hljs-number">-1'234.</span> | ||
<span class="hljs-number">1.2</span> | ||
<span class="hljs-number">1.234'5</span> | ||
<span class="hljs-number">1'234.567'8</span> | ||
<span class="hljs-number">1'234.567'8e-9</span> | ||
<span class="hljs-number">1'234.567'8E9'0</span> | ||
<span class="hljs-number">1234e56</span> | ||
<span class="hljs-number">1'234e5'6</span> | ||
<span class="hljs-comment">// Hexadecimal.</span> | ||
<span class="hljs-number">0x1p2</span> | ||
<span class="hljs-number">+0x1.p2</span> | ||
<span class="hljs-number">-0X1A.P2</span> | ||
<span class="hljs-number">0x1A'2B3C.p4</span> | ||
<span class="hljs-number">0x1.Ap2</span> | ||
<span class="hljs-number">0x1A.2B3C'4Dp+5</span> | ||
<span class="hljs-number">0x1A'2b3C.4D5e'6Fp7'8</span> | ||
<span class="hljs-comment">// Literal suffixes.</span> | ||
<span class="hljs-number">1.F</span> <span class="hljs-number">1.F16</span> <span class="hljs-number">1.F32</span> <span class="hljs-number">1.f64</span> <span class="hljs-number">1.f128</span> | ||
<span class="hljs-number">2.BF16</span> <span class="hljs-number">2.bf16</span> | ||
<span class="hljs-number">0X3'4.P5L</span> <span class="hljs-number">0x3'4.p5l</span> | ||
|
||
<span class="hljs-comment">/* literal suffixes: these are to be highlighted as numbers */</span> | ||
<span class="hljs-number">90ll</span> <span class="hljs-number">90LL</span> <span class="hljs-number">90u</span> <span class="hljs-number">90U</span> <span class="hljs-number">90l</span> <span class="hljs-number">90L</span> | ||
<span class="hljs-number">90ul</span> <span class="hljs-number">90ul</span> <span class="hljs-number">90Ul</span> <span class="hljs-number">90UL</span> <span class="hljs-number">90lu</span> <span class="hljs-number">90lU</span> <span class="hljs-number">90Lu</span> <span class="hljs-number">90LU</span> | ||
<span class="hljs-number">90ull</span> <span class="hljs-number">90uLL</span> <span class="hljs-number">90Ull</span> <span class="hljs-number">90ULL</span> <span class="hljs-number">90llu</span> <span class="hljs-number">90llU</span> <span class="hljs-number">90LLu</span> <span class="hljs-number">90LLU</span> | ||
<span class="hljs-comment">/* Integer literals. */</span> | ||
<span class="hljs-comment">// Binary.</span> | ||
<span class="hljs-number">+0b1</span> | ||
<span class="hljs-number">0B01</span> | ||
<span class="hljs-number">-0b10'1011</span> | ||
<span class="hljs-comment">// Hexadecimal.</span> | ||
<span class="hljs-number">+0x1</span> | ||
<span class="hljs-number">0X1A</span> | ||
<span class="hljs-number">-0x1A'2b3C</span> | ||
<span class="hljs-comment">// Octal.</span> | ||
<span class="hljs-number">+01</span> | ||
<span class="hljs-number">012</span> | ||
<span class="hljs-number">-0123'4567</span> | ||
<span class="hljs-comment">// Decimal.</span> | ||
<span class="hljs-number">0</span> | ||
<span class="hljs-number">+1</span> | ||
<span class="hljs-number">12</span> | ||
<span class="hljs-number">-123'456'789</span> | ||
<span class="hljs-comment">// Literal suffixes.</span> | ||
<span class="hljs-number">0B1U</span> <span class="hljs-number">0b1u</span> | ||
<span class="hljs-number">0X2L</span> <span class="hljs-number">0x2l</span> | ||
<span class="hljs-number">03LL</span> <span class="hljs-number">03ll</span> | ||
<span class="hljs-number">4UL</span> <span class="hljs-number">4Ul</span> <span class="hljs-number">4uL</span> <span class="hljs-number">4ul</span> | ||
<span class="hljs-number">5LU</span> <span class="hljs-number">5Lu</span> <span class="hljs-number">5lU</span> <span class="hljs-number">5lu</span> | ||
<span class="hljs-number">6ULL</span> <span class="hljs-number">6Ull</span> <span class="hljs-number">6uLL</span> <span class="hljs-number">6ull</span> | ||
<span class="hljs-number">7LLU</span> <span class="hljs-number">7LLu</span> <span class="hljs-number">7llU</span> <span class="hljs-number">7llu</span> | ||
<span class="hljs-number">8ZU</span> <span class="hljs-number">8Zu</span> <span class="hljs-number">8zU</span> <span class="hljs-number">8zu</span> | ||
<span class="hljs-number">9'0UZ</span> <span class="hljs-number">9'0Uz</span> <span class="hljs-number">9'0uZ</span> <span class="hljs-number">9'0uz</span> | ||
|
||
<span class="hljs-type">char</span> word[] = { <span class="hljs-string">'3'</span>, <span class="hljs-string">'\0'</span> }; <span class="hljs-comment">// Make sure digit separators don't mess up chars.</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,55 @@ | ||
/* digit separators */ | ||
int number = 2'555'555'555; // digit separators | ||
float exponentFloat = .123'456e3'000; // digit separators in floats | ||
float suffixed = 3.000'001'234f // digit separators in suffixed numbers | ||
char word[] = { '3', '\0' }; // make sure digit separators don't mess up chars | ||
float negative = -123.0f; // negative floating point numbers | ||
/* Floating-point literals. */ | ||
// Decimal. | ||
1. | ||
+12. | ||
-1'234. | ||
1.2 | ||
1.234'5 | ||
1'234.567'8 | ||
1'234.567'8e-9 | ||
1'234.567'8E9'0 | ||
1234e56 | ||
1'234e5'6 | ||
// Hexadecimal. | ||
0x1p2 | ||
+0x1.p2 | ||
-0X1A.P2 | ||
0x1A'2B3C.p4 | ||
0x1.Ap2 | ||
0x1A.2B3C'4Dp+5 | ||
0x1A'2b3C.4D5e'6Fp7'8 | ||
// Literal suffixes. | ||
1.F 1.F16 1.F32 1.f64 1.f128 | ||
2.BF16 2.bf16 | ||
0X3'4.P5L 0x3'4.p5l | ||
|
||
/* literal suffixes: these are to be highlighted as numbers */ | ||
90ll 90LL 90u 90U 90l 90L | ||
90ul 90ul 90Ul 90UL 90lu 90lU 90Lu 90LU | ||
90ull 90uLL 90Ull 90ULL 90llu 90llU 90LLu 90LLU | ||
/* Integer literals. */ | ||
// Binary. | ||
+0b1 | ||
0B01 | ||
-0b10'1011 | ||
// Hexadecimal. | ||
+0x1 | ||
0X1A | ||
-0x1A'2b3C | ||
// Octal. | ||
+01 | ||
012 | ||
-0123'4567 | ||
// Decimal. | ||
0 | ||
+1 | ||
12 | ||
-123'456'789 | ||
// Literal suffixes. | ||
0B1U 0b1u | ||
0X2L 0x2l | ||
03LL 03ll | ||
4UL 4Ul 4uL 4ul | ||
5LU 5Lu 5lU 5lu | ||
6ULL 6Ull 6uLL 6ull | ||
7LLU 7LLu 7llU 7llu | ||
8ZU 8Zu 8zU 8zu | ||
9'0UZ 9'0Uz 9'0uZ 9'0uz | ||
|
||
char word[] = { '3', '\0' }; // Make sure digit separators don't mess up chars. |