Notice: Stat is currently in private beta. This documentation is incomplete and subject to change.
Stat Docs
Numbers in Stat
Numbers in Stat work the way you'd expect them to work in the real world. Unlike other languages that use floating point
numbers and floating point math calculations, Stat takes a different approach and uses fractional numbers and fractional
math.
The common issues that developers usually run into with other programming languages is that the number system is limited
to 64-bits. This also means that there is only so much precision that is available for storing numbers and for performing
math calculations. Stat has no such limitation. Numbers are not limited to 64 bits and floating point math is scrapped
all together in favor of fractional math calculations. This also means that we can represent a wider range of numbers
including those that are impossible to represent exactly in a floating point number system... like one third
1/3 for example. Other languages will store this value as 0.3333333333333333 but that's not exactly correct. Sure it's close,
but it's not one third exactly. Subsequently, when performing simple math calculations like 0.1 + 0.2, you
can get a result like 0.30000000000000004 instead of the expected result of 0.3. It's this discrepancy
that causes the dreaded 1 cent rounding error when coding financial applications. In Stat, those rounding errors are gone
entirely. Number Types
There are 2 number types in Stat.
intCan hold integer valuesfractionCan hold fraction and floating point values
Integer Literals
You can code an integer literal in Stat by simply typing the integer value. Integers can be positive
or negative and have no limit on how large or small (meaning negative) they can be. There are also
two special integer literals:
infinityThis is the largest integer allowed-infinityThis is the smallest integer allowed
MAIN // This is how you create n integer literal // This statement doesn't do anything // other than create the integer literal, but this // is a valid statement, albeit a bit useless 52
The code above doesn't do anything other than create an integer literal, then forgets about it.
You'd be better served by storing that integer literal into a variable so that it can be referred to later
or so that you can pass it to a function or a stream. Here are some more useful examples:
MAIN let luckyNumber = 52 let negativeNumber = -107 let maxNumber = infinity let minNumber = -infinity
Using Different Integer Bases
Stat has the ability to specify different integer bases when coding integer literals.
You could use base 16 instead of base 10 for example. To use a different base, just
put the correct prefix in front of the number. Here is a list of the supported bases:
- base10 - (Decimal) No prefix necessary. This is the default. You could optionally prefix your integers with
0d - base16 - (Hexadecimal) Prefix your integers with
0x - base8 - (Octal) Prefix your integers with
0o - base2 - (Binary) Prefix your integers with
0b
Here are some examples of integer literals using different bases
MAIN // 2571 Using base 10, the default let intLiteral = 2571 // Same thing but with a prefix intLiteral = 0d2571 // The same number using base 16, hexadecimal intLiteral = 0xa0b // You can use capital letters as well... your choice intLiteral = 0xA0B // The same number using base 8, octal intLiteral = 0o5013 // The same number using base 2, binary intLiteral = 0b101000001011 // Negative numbers in different bases let negInt = -251 negInt = -0d251 negInt = -0xfb negInt = -0o373 negInt = -0b11111011
Floating Point Literals
Just like other languages, you can code your numbers as floating point literals by placing a decimal point
after the integer followed by 1 or more numbers after the decimal like this:
12.7. In Stat,
floating point literals resolve to the fraction data type. 12.7 means 12 and
seven tenths. Here are some examples of floating point literals. MAIN let floatLiteral = 12.907 floatLiteral = 0.001 floatLiteral = -7.52 // You can use different bases too let base16Float = 0x1a.5c2 base16Float = -0x3b4.9f
Fraction Literals
Fraction literals are probably a new concept for most programmers, but it's pretty simple.
Just write the fraction literal like you did in elementary school.
Fraction literals start with an integer literal, followed by a fraction like so:
5 1/3 Here are some examples of fraction literals. MAIN // Five and one third let fraction = 5 1/3 // Three seventy thirds fraction = 0 3/73 // Negative six and two ninths fraction = -6 2/9 // You can also use different bases // This equals 26 1474/1835 let base16Fraction = 0x1a 5c2/72b // This equals 948 157/266 base16Fraction = -0x3b4 9d/10a
Numeric Operators
Stat has all the expected number operators and can perform all the normal math
operations with numbers just like other programming languages. However, Stat is
missing something that other languages have.... limitations. Since there is no
upper limit to how large a number can be, you can execute math operations without
having to worry about going past the maximum number limit. Just know that the larger
your numbers get, the longer it'll take to perform math calculations on those numbers.
Here is a list of all the numeric operators that Stat supports:
+: Addition operator, adds two numbers together.-: Subtraction operator, subtracts the right number from the left number.*: Multiplication operator, multiplies two numbers together./: Division operator, divides the left number by the right number.%: Remainder operator, calculates remainder after dividing the left number by the right number.**: Exponent operator, rases the left number to the power of the right number./#: Integer division, divides the left number by the right and discards the remainder.
MAIN // These both equal 52 let integerResult = 50 + 2 integerResult = 56 + -4 // These all equal 25 integerResult = 31 - 6 integerResult = 22 - -3 integerResult = 51 - 0x1a integerResult = 5 * 5 integerResult = 101 /# 4 // This equals 3 integerResult = 107 % 4 // These both equal 25.0 // The data type for these operators is: fraction let fractionResult = 5 ** 2 fractionResult = 100 / 4 // This equals one third... exactly fractionResult = 1 / 3 // This equals one twenty fifth (1/25) fractionResult = 5 ** -2 // This equals 8 11/15 fractionResult = 1 1/3 + 7 2/5
Bitwise Operators
In Stat, you can perform bitwise operations on integers (not fractions). This allows you to
manipulate the binary representation of the integer. There are several bitwise operators available.
Here is a full list:
&Bitwise AND - Compares 2 integers bit by bit and sets the result bit to 1 if both corresponding bits are 1, otherwise sets the result bit to 0|Bitwise OR - Compares 2 integers bit by bit and sets the result bit to 1 if either corresponding bits are 1, otherwise sets the result bit to 0^Bitwise XOR - Compares 2 integers bit by bit and sets the result bit to 1 if corresponding bits are different, otherwise sets the result bit to 0>>Bitwise shift right - Shifts bits to the right discarding any that fall off the right side of the integer<<Bitwise shift left - Shifts bits to the left, filling the right side of the integer with zeros
MAIN // This equals 32 let bwAnd = 50 & 672 // This equals 690 let bwOr = 50 | 672 // This equals 658 let bwXor = 50 ^ 672 // This equals 168 let shitRight = 672 >> 2 // This equals 2688 let shiftLeft = 672 << 2
Previous
Next