- Perl 6 Deep Dive
- Andrew Shitov
- 572字
- 2021-07-03 00:05:49
Integer data type
The value of the Int type is an integer value in Perl 6. The value can hold both positive and negative numbers, as well as zero, and the language does not limit the size of the number. It can be as small as one byte, for example, take a look at the following example:
say 42;
It can also be of arbitrary precision, as shown here:
say 239874637819093248768900298372340;
In the preceding examples, common decimal notation was used. Perl 6 allows using other bases; for example, 16 for hexadecimal values. To create an integer with a base other than 10, use the so-called adverbial notation, as shown in the following example:
say :16<D0CF11E>;
This will print 218951966, which is the decimal representation of the :16<D0CF11E> integer.
In the same manner, you will create values with other bases. Consider the following example:
say :8<755>;
say :2<10101>;
The preceding two lines of code will print 493 and 21, correspondingly.
The base value should not necessarily be a power of two. Other integer values between 2 and 36 are allowed, for instance, consider the following lines of code:
say :5<342>;
say :30<102spqr>;
In the first example, the base is 5, thus, the digits 0 to 4 are available in the representation of a number. The value :5<342> corresponds to 97 in the decimal form.
In the second example, we are free to use more 'digits', namely, 30 of them. Those digits are the regular Arabic digits 0 to 9, followed by 20 letters of the Latin alphabet, which are a to t. The decimal value of :30<102spqr> is 731399307.
You may have noticed that, in the preceding examples, some letter digits were in uppercase and some were in lowercase. For Perl 6, there is no difference; the integer digits, when they include alphabetical characters, are case-insensitive. So, the numbers :16<D0CF11E> and :16<d0cf11e>, as well as :30<102spqr> and :30<102SPQR> are equivalent.
Case-insensitivity indirectly defines the range of the allowed bases for integers; as we have 10 Arabic digits and 26 Latin letters, their combination gives 36 different characters.
In Perl 6, long integer values (that is, integers with many digits) may be spelled down with visual separation to groups of digits using the underscore character. The most straightforward goal of that feature is to provide the way to split the number into the groups of three digits. For example, consider the following line of code:
say 75_926_028;
Here, the notation of 75_926_028 is nothing more than giving the number of 75926028, but it lets us clearly see that the number is 75 million, 926 thousand, and 28. For the compiler, there is no difference, and both numbers are equally easy to read. For humans, it is much more easy to read a number that is split into groups.
Technically, you are not restricted to the way you split the number. This means that the following format is formally correct:
say 2_12_85_06;
This format may be suitable for phone numbers, but not for regular integers.
You cannot, however, have two underscores in a row. Neither can a number start or end with it. The following three attempts will not compile:
say 20__17;
This code will generate the compiler error:
Only isolated underscores are allowed inside numbers
say _2017;
say 2017_;
The error message in these two cases is a bit shorter, the compiler just says that it is:
Confused