Language Reference: Numbers
Numbers and numerical operators
Pointless uses a single number type for integers and decimal numbers, which it stores using floating-point representation.
Important numerical functions include:
| Function | Use |
|---|---|
| Math.abs | Get the absolute value of a number |
| Math.ceil | Round a number up |
| Math.floor | Round a number down |
| Math.max | Get the maximum of two numbers |
| Math.min | Get the minimum of two numbers |
| Math.round | Round a number |
| Math.roundTo | Round a number to a given precision |
| Math.sqrt | Get the square root of a number |
See the standard library Math module for more built-in functions for working with numbers.
Syntax
Numbers use familiar mathematical syntax.
120
3.14
-0.5
120
3.14
-0.5
Numbers can also use engineering notation.
1e-4
6.022e23
0.0001
6.022e+23
Operators
Mathematical operations use the following operators.
-8 -- Negation
5 + 5 -- Addition
20 - 4 -- Subtraction
6 * 7 -- Multiplication
3 / 12 -- Division
2 ** 4 -- Exponentiation
10 % 7 -- Modulus
-8
10
16
42
0.25
16
3
These operators obey the standard order of operations.
2 + 3 * (4 + 1)
17
Modulus Behavior
The modulus operator % uses the
floor mod
variant when operating on negative values, meaning that the result of
a modulus operation will have the same sign as the divisor (the second
operand).
10 % 7
-10 % 7
10 % -7
-10 % -7
3
4
-4
-3
Comparison
Numbers can be compared for equality using the equals
== and not-equals != operators.
1 + 1 == 2
2 + 2 != 4
true
false
Numbers can also be compared using the inequality operators:
<less-than>greater-than<=less-than-or-equal-to>=greater-than-or-equal-to
1 < 2
1 > 2
5 > 5
5 >= 5
true
false
false
true
Floating-point Error
Like any system based on floating-point arithmetic, the results of some numerical operations in Pointless involving non-integer values will be inexact. Watch out for this behavior when comparing non-integer values.
n = (Math.sqrt(5) * 2) ** 2
n
n == 20
20.000000000000004
false
Strictness
Pointless is stricter than many other languges when it comes to numerical operations. Calculations that would produce a NaN or infinity value in other languages will cause errors in Pointless.
-- Cannot take square root of a negative number
(-1) ** 0.5
Error: invalid arguments
-- Numerical overflow
10 ** 1000
Error: result out of range
Note that NaN and infinity values can be accessed via
Math.nan and Math.inf.