KL literal numbers

## Integer Numbers

Integers are whole numbers, meaning numbers with no fractional portion. For example 12 is an integer but 12.5 (with a decimal point) is not.

To write an integer number in KL source code, simply write it as 12345 for example. The number cannot have spaces or commas (as thousands separators) in it.

Numbers in KL are by default in decimal (radix/base 10). Because computers are based on binary (radix 2), sometimes it is useful to be able to write a number in binary. To write a binary number, you must prefix it with "0b" (zero and "b") so the compiler knows it is a binary number (as opposed to a decimal number that just happens to contain only "0" and "1" digits).

Hexadecimal (radix 16) is also useful sometimes because 16 is a power of 2 (it is 2\*\*4) and is thus directly compatible with binary (1 hexadecimal digit consists of exactly 4 binary digits). To write a hexadecimal number, prefix it with "0x". Being radix 16, it has more possible digit values than decimal. This means that "0" to "9" is insufficient for hexadecimal. We need 6 more symbols. We use the first 6 letters of the alphabet. Thus the 16 possible digit values in radix 16 are represented using these characters: 0123456789ABCDEF (lowercase a-f is also accepted.)

Here is the same number written 4 different ways:

```
1234   {decimal}
0x4D2  {hexadecimal}
0x4d2  {hexadecimal}
0b10011010010  {binary}

```

Internally all 3 forms are converted to binary by the compiler (computers are based on binary), so it does not matter which you use -- the result is exactly the same regardless of which you use. Being able to write the number in different radixes is only for your convenience, it does not affect the operation of the program.

Leading zero digits are insignificant, thus the following numbers are equivalent and have the same interpretation. In the case of hexadecimal and binary, the leading zero digits must appear after the prefix ("0x" or "0b").

```
1234      {decimal}
001234    {decimal}
0x4D2     {hexadecimal}
0x0004D2  {hexadecimal}
0b00010011010010  {binary}
00x4D2    {INVALID}

```

You might ask, if I cannot use space or comma as a thousands separator, is there any character that I can use as a thousands separator or to group digits in a number? Yes, you can use the underscore ("\_") character. For example:

```
1_315_000    {decimal}
0xA1B2_4444  {hexadecimal}
0b11111111_00000000  {binary}

```

1\_315\_000 is interpreted the same as 1315000 (the underscores are simply skipped/ignored). In the case of decimal, the first digit cannot be an underscore. In the case of hexadecimal and binary, any underscores must be after the prefix.

Names in KL are not permitted to begin with a digit "0" to "9", but can contain "0" to "9" elsewhere within the name. This allows the compiler to easily and quickly distinguish between names and numbers. If it begins with "0" to "9", then it is interpreted as a number.

If a number begins with "-", then it is interpreted as a negative number. It is acceptable to prefix a number with "+" to explicitly specify a positive number, but the "+" can be omitted and positive is assumed by default. In the case of hexadecimal and binary, the "+" or "-" must appear before the "0x" or "0b" prefix. Here are some examples of valid positive and negative numbers:

```
-123     {decimal}
+123     {decimal}
-0xA1D2  {hexadecimal}
+0b111   {binary}
0b-111   {INVALID}

```

Some confusion may arise because the "+" and "-" characters are also used for addition and subtraction operators. These situations are distinguished by the presence of whitespace.

For example, "-3" is interpreted as the number negative 3, whereas "- 3" (with a space) is interpreted as subtract the positive number 3. Note that "9-1" (without space) is interpreted as 2 numbers, the number 9 followed by the number negative 1. If you wanted it to be interpreted as subtraction, then write it with spaces as "9 - 1". Likewise, "someVarName-46" is interpreted as a name followed by the number negative 46. If you wanted it to be interpreted as subtraction, then write it with spaces as "someVarName - 46".

## Fractional Numbers

In situations where you are using floating-point maths (or perhaps also fixed-point maths), you can write a decimal number with a fractional portion as "123.456" (without quotes) for example. The "." character is used as the decimal point. "123" is the integer (whole number) portion, and "456" is the fractional portion.

When using a decimal point, there must be at least 1 digit ("0" to "9") on both sides of the decimal point. Extra leading and trailing zero digits can be included and are insigificant (do not change the interpretation).

The number cannot contain spaces or commas. Underscores can be included as a thousands separator (underscores are simply skipped/ignored). The first digit cannot be an underscore, and the characters immediately left and right of the decimal point cannot be an underscore.

The number can be prefixed with "+" or "-" to indicate positive or negative, with the same rules as for integers.

"Scientific notation" can be used. For example "1.2e3" or "1.2e+3" means 1200 (calculated as 1.2×103). "4.56e-5" means 0.0000456 (calculated as 4.56×10-5). The decimal point must be included, thus "1e8" is invalid (use "1.0e8" instead). The "e" must be lowercase. The exponent (the number after the "e") must be a positive or negative integer.

Here are some valid examples:

```
123.456
+123.456
-50.55
115_700.65
0023.300
0.0
0.5
42.0
1.2e3
4.56e-5

```

Here are some invalid examples:

```
42._
.5
-.5
_.5
_102.73
1e3
2.0e
5.9e1.2
4.e-3

```

## Text Character Numbers

So now you know how to write numbers in decimal, hexadecimal, and binary, but there is another representation. This one is slightly confusing to beginners. Firstly, realize that internally in a computer, everything is (and can only be) represented as a number. So if everything must be a number, how is the letter "A" represented inside a computer? As the number 65. And "B" is 66, and "C" is 67, and so forth. The uppercase and lowercase versions of the letter/character are represented using different numbers, so "A" is 65 while "a" is 97.

However it is difficult/inconvenient to remember which numbers are used for which letters of the alphabet (not to mention other characters such as "$"), so you can have the compiler automatically supply the number for a character by writing the character enclosed in single quotation marks (also known as apostrophes). Following you can see the same number written 4 different ways:

```
'A'   {character}
65    {decimal}
0x41  {hexadecimal}
0b01000001  {binary}

```

Note that there is a difference between the text characters '0' to '9' and the numbers 0 to 9. For example, there is a difference between the number 1 and the number for the text character '1'. There is a text character '1' and it is represented by the number 49. Perhaps it would have made more sense for the character '1' to be represented by the number 1 (or perhaps that would have been even more confusing).

Regardless of what makes the most sense, it has been established by convention that the text character '1' is represented as the number 49, and it is not worth the hassle of changing the standard even if it could be successfully argued that representing it using the number 1 makes more sense. For this reason, the following is the same number written 3 different ways:

```
'1'   {character}
49    {decimal}
0x31  {hexadecimal}

```

A quoted character sequence can also contain characters specified by name rather than literally. For a listing of character names, see the Literal/Quoted Text section of this documentation. Following you can see the same number written 4 different ways:

```
'<amp>'  {character by name}
'&'      {character literally}
0x26     {hexadecimal}
38       {decimal}

```

Do not confuse the effect of SINGLE quotation marks with the effect of DOUBLE quotation marks. Single quotation marks make a quoted CHARACTER sequence, whereas double quotation marks make a quoted TEXT sequence. Single quotation marks get the integer number of a character, whereas double quotation marks make a read-only text variable.

The following characters cannot appear literally/directly in a quoted character sequence, because they would make the source code confusing, ambiguous, unreliable, or disruptive to the compiler. If you want to get the number of one of these characters, you must specify it by name not literally.

Following you can see some invalid quoted character sequences, and the correct sequence that was probably intended.

 '"'
 '<'
 '>'
 '{'
 '}'

It is also possible to write a quoted character sequence containing multiple characters, for example as a 3 character code. In this case, the numbers of the characters are packed together as 8-bit values. There is a maximum of 8 characters (64 bits in total). Only basic characters can be used in a multiple-character sequence (because other characters require more than 8 bits each). The characters can be specified literally or by name. Following you can see the same number written 4 different ways:

```
'ABC'       {character}
0x414243    {hexadecimal}
0x41_42_43  {hexadecimal}
4276803     {decimal}

```

Another example, this time with 2 characters specified by name:

```
'<amp>GO<amp>'  {character}
0x26474F26      {hexadecimal}
642207526       {decimal}

```

Following you can see how a number is written in decimal (radix 10), hexadecimal (radix 16), and binary (radix 2).