Numbers
Units & Casting
There are 3 types of numbers in Stof: integer, float, and units. In the above section, we saw a brief example of the integer and floating-point number types. However, there is a variant of "float" that helps immensely in Stof - unit types.
Number Operations
#[main]
fn main() {
let res = 10 + 10; // addition
res = 10 - 5; // subtraction
res = 10 * 10; // multiplication
res = 10 / 2; // division
res = 10 % 2; // modulo/remainder
res = 0b0101 & 0b0100; // bit and
res = 0b0101 | 0b0110; // bit or
res = 0b0101 ^ 0b0100; // bit xor
res = 0b0011 << 2; // bit shift left
res = 0b1100 >> 2; // bit shift right
res += 10; // res = res + 10
res -= 10; // res = res - 10;
res *= 10; // res = res * 10;
res /= 10; // res = res / 10;
res %= 10; // res = res % 10;
res &= 0b0100; // res = res & 0b0100;
res |= 0b0110; // res = res | 0b0110;
res ^= 0b0100; // res = res ^ 0b0100;
res >>= 2; // res = res >> 2;
res <<= 2; // res = res << 2;
}Casting
Casting (and conversions) are done with the "as" keyword.
Casting also automatically occurs with function calls (parameters and return type).
Units
In Stof, units are additional number types that can be used in place of "float". The "float" type will match all unit types (so you can have a float parameter that accepts any units); however, the unit types will not match each other and will perform conversions when cast to other unit types.
All logical operators (greater than, less than, equals, etc.) and number operations consider units, performing conversions automatically when necessary (and able). Units of the same category will always have a common unit that is the larger of the two (Ex. m + mm -> m). For angles, it will always be radians if the types are mixed.
Examples
Memory
Computer memory/storage units are helpful for configuration files, among many other use cases. Like all units, conversions between any memory unit can be done via a simple cast (either "as" or by declaring units as a type).
Time
One of the most useful applications of Stof units is with time, which is all over the place in configurations and intersystem communications.
As an example, the Time.now() function returns "ms" instead of an integer. This allows users to subtract days, for example, and have Stof handle the complexity.
Angles
There are 4 angle types in Stof: radians (rad), degrees (deg), positive radians (prad), and positive degrees (pdeg).
Positive variations ensure the angle is (and remains) positive (-90deg as pdeg == 270deg) and are used for comparisons.
Radians (rad) and degrees (deg) clamp the angle between [0, +-360deg]. The positive variations clamp the angle between [0, 360deg) (transforming +-360deg into 0 for comparisons). If you don't like the casting and clamping, use a normal float and do it yourself.
Length
Temperature
Mass
Incompatible Units
If two units do not have compatible types (Ex. mass & time), then the units are removed.
Last updated