Integers and Floats
East supports two distinct types of numbers – integers and floating-point numbers. Some programming environments support a single "number" type representing both. However:
- Finding a single representation to handle all cases is challenging. For example, integer data coming from external sources (like databases) often exceeds JavaScript's
Number.MAX_SAFE_INTEGER. - Working with such numbers can actually be more difficult to reason about as a programmer. For example, only integers can index an array, and it is useful to know that a value must a whole number.
While JavaScript (and TypeScript) began with a single number they later had to add bigint for the reasons above, leaving the language in a messy state.
East is intended to be easy to work with from the beginning.
Features such as automatic "promotion" of integers to floats during mathematical operations (like division) makes it easy to work with the two types while retaining the safety benefits.
Integer values and type
Integers are whole numbers.
Elara uses a 64-bit signed integer representation, allowing for values between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.
The East type for integers is IntergerType.
The JavaScript values used for IntegerType in the EDK are bigints, written with a suffix n like 1n.
| East type | Example value (JavaScript) | East object notation |
|---|---|---|
IntegerType | 0n | 0 |
IntegerType | 42n | 42 |
IntegerType | -1000n | -1000 |
Note that arithmetic for integers in East is wrapping (unchecked).
Float values and type
Floating-point numbers are a representation of "real" numbers, which may be fractional (like 0.5).
Elara uses the standard IEEE-754 64-bit floating-point representation.
The East type for floats is FloatType.
The JavaScript values used for FloatType in the EDK are numbers, the standard numeric type in JavaScript.
| East type | Example value (JavaScript) | East object notation |
|---|---|---|
FloatType | 0 | 0.0 |
FloatType | 3.14 | 3.14 |
FloatType | -1e10 | -1e10 |
FloatType | Infinity | Infinity |
FloatType | -Infinity | -Infinity |
FloatType | NaN | NaN |
Floating-point arithmetic has the possibility of generating infinite or unknown values, for example when dividing by zero. These values are handled automatically according to the IEEE-754 standard.
Mathematical operations
East provides a range of mathematical operations for manipulating numeric values.
| East function | Description | Example usage | Result |
|---|---|---|---|
Add | Addition, a + b | Add(6, 2) | 8 |
Subtract | Subtraction, a − b | Subtract(6, 2) | 4 |
Multiply | Multiplication, a × b | Multiply(6, 2) | 12 |
Divide | Division, a ÷ b | Divide(6, 2) | 3 |
Modulo | Modulus (remainder), mod(a, b) | Modulo(6, 2) | 0 |
Pow | Power of, aᵇ | Pow(6, 2) | 36 |
Sqrt | Square root, √a | Sqrt(36) | 6 |
Exp | Exponential (base e), eᵃ | Exp(1) | 2.718281828459045 |
Log | Logarithm (base e), ln(a) | Log(1) | 0 |
Conversion and rounding
It is possible to convert integer to floats, and vice-versa.
The Convert operation performs conversion between IntegerType and FloatType.
When converting from a float to an integer there is the possibility the float is not a whole number.
In this case the Convert will round the float, "truncating" it toward zero.
It is also possible to Round a float, either returning another float or an integer.
The Round function accepts a "rounding mode", where you can choose to round to the nearest intger, the next greater integer (ceiling), next lesser integer (floor), or towards zero (truncate).
(Note that truncation differs from flooring when the input is negative.)
| East function | Description | Example usage | Result |
|---|---|---|---|
Convert | Convert integer to float | Convert(Const(3n), FloatType) | 3.0 |
Convert | Convert float to integer | Convert(Const(3.14), IntegerType) | 3 |
Round | Round to nearest | Round(Const(3.14)) | 3.0 |
Round | Round down | Round(Const(3.14), "floor") | 3.0 |
Round | Round up | Round(Const(3.14), "ceiling") | 4.0 |
Round | Round to nearest as integer | Round(Const(3.14). "nearest", "integer") | 3 |
Round | Round down as integer | Round(Const(3.14), "floor", "integer") | 3 |
Round | Round up as integer | Round(Const(3.14), "ceiling", "integer") | 4 |
Note that Floor and Ceiling rounding come up frequently in business problems.
For this reason the EDK exports the Floor and Ceiling functions as shorthand syntax for Round with these rounding modes.
| East function | Description | Example usage | Result |
|---|---|---|---|
Floor | Round down | Floor(Const(3.14)) | 3.0 |
Ceiling | Round up | Ceiling(Const(3.14)) | 4.0 |
Floor | Round down as integer | Floor(Const(3.14), "integer") | 3 |
Ceiling | Round up as integer | Ceiling(Const(3.14), "integer") | 4 |
Later you will see that Round, Floor and Ceiling are also used to round date-time values (e.g. to the nearest hour or day).
Automatic conversion and promotion
While most mathematical operations make sense for integers, some are specific to floating-point numbers.
The result of Divide, Sqrt, Exp and Log will always be a float.
To perform these operations, the any integer inputs will always be first converted to a float.
Similarly, mixing together integers and floats in the same mathematical operation will result in a float.
Adding a whole number and a fractional number will result in a fractional number.
When you mix integers and floats in Add, Subtract, Multiply, Modulo or Pow the integer will first be converted (or "promoted") to a float.
The output will be a float.
Of course, integer operations involving two integers (e.g. adding two integers) will result in integer output and not trigger any conversion.
With automatic conversion and promotion, East's two numerical types are quick and easy to work with while retaining the benefits of a strong type system.
Comparison and ordering
It is worth noting the comparison and ordering properties of floating-point numbers in East differs from the IEEE 754 comparisons.
Instead East provides a "total ordering" with Equal and Less (total ordering is also a part of the IEEE 754 standard).
The total order guarantees that:
-0.0is less than (and not equal to)0.0.NaNis equal to itself.NaNis greater than every other float value.
Representation
Integer values are represented as strings in Elara's canonical JSON representation, to safely store and transmit values larger than JavaScript's Number.MAX_SAFE_INTEGER.
They are printed without any quotes or adornmenents (like the n suffix for bigint) by Print.
Float values are usually represented as numbers in Elara's canonical JSON representation.
The special values NaN, Infinity and -Infinty are encoded in JSON by the strings "NaN", "Infinity" and "-Infinity" respectively.
Floats are always printed by Print with a decimal component (like 1.0 not 1).
In this way they are not ambiguous with integer values.
Special values NaN, Infinity and -Infinity are printed directly as such.
East does not support NaN signalling bits; such values will treated as a non-signalling NaN.
Next steps
Continue to the next tutorial to understand how to use and manipulate text and strings with East and Elara.