Suddenly you didn't know, but in the language you write in, you can use _
in numbers. For example, the following PHP code:
<?php
print(1_00);
print(100);
Will output 100100
( check online ). This syntactic sugar appeared in Ada in 1980 and has had mixed success over the past 40 years. But over the past year, it has been added to javascript, PHP, Go, Scala, and even conservative Erlang. I cannot explain what caused the surge in popularity, so in this article I will simply describe the history of separators in numbers.
19 68 Algol
At the dawn of programming, we didn't really bother with spaces, and therefore in Algol it was possible to insert a space in variable names and numbers. For example, a 1 st var
is a valid variable name, and a synonym a1stvar
. The code:
BEGIN
INT a 1 st var = 1 234 567;
REAL a 2 nd var = 3 . 1 4159 26 5 359;
print((a1stvar, newline, a2ndvar))
END
Will output ( check ):
+1234567
+3.14159265359000e +0
UPD: ( fixed-form source files). fixed-form source files 2018 .
19_80 Ada
, 1980 Ada. , Ada 2 16. 11
(6
) 5#11#
. , , I: Float := 3#0.1#
— 1/3 ( ):
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
procedure hello is
I: Float := 3#0.1#;
J: Float := 1_00.0;
begin
Put(I);
Put(J);
end hello;
19_85 Caml
19_87 Perl
, , Caml Perl. Perl , ( ) :
$x = 1_0_0;
$y = 2__5________0;
$z = $x+$y;
print "Sum of $x + $y = $z";
19_93 Ruby
19_96 OCaml
, , . OCaml () Caml, Ruby Perl, (, ).
20_03 D (v0.69)
D , , ( ).
20_10 Rust
20_11 Java (SE 7)
20_12 Julia
Rust OCaml, Julia Ruby. Java Perl .
20_14 Swift
Rust.
20'14 C++ (v14)
. , C++ . , _1
. :
#include <iostream>
int main()
{
for (int _1 = 0; _1 < 5; ++_1) {
std::cout << _1 << " ";
}
}
UPD: , (User-defined literals). 1
:
#include <iostream>
constexpr unsigned long long operator"" _0(unsigned long long i)
{
return 1;
}
int main()
{
int x = 0_0;
std::cout << x;
return 0;
}
20_16 Python (3.6)
20_17 C# (7.0), Kotlin (v1.1)
20_18 Haskell (GHC 8.6.1)
20_19 JS (V8 v7.5, Chrome 75), PHP (v7.4), Go (v1.13), Scala (v2.13)
- , . , , . , PHP :
// PHP 7.3
decimal : [1–9][0–9]* | 0
hexadecimal : 0[xX][0–9a–fA–F]+
octal : 0[0–7]+
binary : 0[bB][01]+
// PHP 7.4
decimal : [1–9][0–9]*(_[0–9]+)* | 0
hexadecimal : 0[xX][0–9a–fA–F]+(_[0–9a–fA–F]+)*
octal : 0[0–7]+(_[0–7]+)*
binary : 0[bB][01]+(_[01]+)*
, PHP :
20_20 Erlang (v23)
Erlang. , , Erlang , Ada ( ), ( 36). , () 3z
36 (=143
).
-module(main).
-export([start/0]).
start() ->
io:fwrite("~w", [36#3z]).
33 .