Numbers and math
Ant has ten number types: eight integer types and two float types. int and float64 are the defaults, and the others are for when the size matters. Numeric types never mix: +, <, and the other arithmetic and comparison operators need both operands of one type, so you convert explicitly by calling the target type's name. The library splits the rest of the work across five places: the conversion functions of the prelude, String for parsing and printing numbers, Math for arithmetic on numbers, Bits for the bits of integers, and Random for random numbers you can repeat.
let prices = [19.99, 5.25, 0.76]
let total = prices.sum() ?? 0.0
let mean = total / float64(prices.length())
println("total ${String.fromFloat(total, 2)}")
println("mean ${String.fromFloat(mean, 2)}")total 26.00
mean 8.67Converting between number types
Call a type's name to convert a number to it. A conversion to an integer type must be exact: a fraction, a value out of range, NaN, or an infinity panics. A conversion to a float type rounds to the nearest value it can hold and never panics.
float64(7) / 2.0 // 3.5
int8(100) // 100
int(Math.trunc(-2.7)) // -2
int(Math.round(2.5)) // 3
Math.toInt(2.5) // null
int(2.5) // panic: 2.5 has a fraction, so it does not convert to int- int, int8, int16, int32, int64: a number as a signed integer, exactly
- uint8, byte, uint16, uint32, uint64: a number as an unsigned integer, exactly
- float64, float32: a number as a float, rounded to the nearest value
- Math.toInt: a whole
float64as anint, ornullwhereintwould panic - Math.trunc, Math.round, Math.floor, Math.ceil: a
float64made whole first, sointdoes not panic on a fraction
Parsing and printing numbers
Parsing returns null on text that is not a number, so the caller decides what a bad value means. It accepts no white space around the number, so trim text read from a line, a file, or stdin first. Printing chooses the base or the number of decimals. String.fromFloat rounds a half to even, while Math.round rounds it away from zero.
"42".toInt() // 42
"ff".toInt(base: 16) // 255
"2.5".toFloat() // 2.5
"4 apples".toInt() // null
" 42\n".trim().toInt() // 42
String.fromInt(255, base: 2) // "11111111"
String.fromFloat(Math.PI, 3) // "3.142"
String.fromFloat(2.5, 0) // "2"
String.from(0.1 + 0.2) // "0.30000000000000004"- String.toInt: text in a base from 2 to 36 as an
int, ornull - String.toUint: text with no sign as a
uint64, ornull - String.toFloat: a decimal number,
NaN, or an infinity as afloat64, ornull - String.fromInt: any integer as digits in a base from 2 to 36
- String.fromFloat: a
float64in fixed point with a set number of decimals, rounded half to even - String.from: any value as text, as string interpolation renders it
Arithmetic and overflow
Integer / truncates toward zero, and % keeps the dividend's sign. Integer overflow panics, and so does integer division by zero; float division follows IEEE 754. When you want wrapping arithmetic, as a hash function does, ask for it with Bits.
7 / 2 // 3
-7 % 2 // -1
1.0 / 0.0 // Infinity
Bits.wrapAdd(Math.maxInt, 1) // -9223372036854775808
Math.maxInt + 1 // panic: integer overflow- Arithmetic operators:
+,-,*,/, and% - Bits.wrapAdd, Bits.wrapSub, Bits.wrapMul:
a + b,a - b, anda * b, wrapping around in two's complement instead of panicking - Array.sum: the sum of an array of numbers, or
nullwhen it is empty - Array.range: the
ints fromfromup to but excludingto
Sign, size, and bounds
Math.abs, Math.min, Math.max, and Math.clamp take any one number type and keep it, so they work on an int without a conversion. Math.abs of the most negative integer, such as Math.minInt, panics, because its absolute value does not fit the type.
Math.abs(-3) // 3
Math.max(2.5, 1.0) // 2.5
Math.clamp(130, 0, 100) // 100
[4, 9, 1].max() // 9
Math.abs(Math.minInt) // panic: Math.abs: integer overflow- Math.abs: the absolute value of any number
- Math.min, Math.max: the smaller or larger of two numbers or strings
- Math.clamp: a value held within
lo ..= hi - Array.min, Array.max: the least or greatest element, or
nullwhen the array is empty
Rounding
The four rounding functions take and return a float64. They differ on negative numbers and on halves.
Math.floor(-2.5) // -3.0
Math.ceil(-2.5) // -2.0
Math.round(-2.5) // -3.0
Math.trunc(-2.5) // -2.0- Math.floor: the greatest whole number not above
x - Math.ceil: the least whole number not below
x - Math.round: the nearest whole number, halves away from zero
- Math.trunc:
xwith its fraction dropped, toward zero
Powers, roots, and logarithms
These functions take a float64. Convert an int with float64 first.
let side = 3
Math.sqrt(float64(side * side)) // 3.0
Math.pow(2.0, 10.0) // 1024.0
Math.hypot(3.0, 4.0) // 5.0
Math.log2(1024.0) // 10.0- Math.pow:
xraised to the powery - Math.sqrt, Math.cbrt: the square and cube root
- Math.hypot: the square root of
x² + y², without overflow in between - Math.exp:
eraised to the powerx - Math.log, Math.log2, Math.log10: the natural, base-2, and base-10 logarithm
Trigonometry
Angles are in radians.
let degrees = 90.0
Math.sin(degrees * Math.PI / 180.0) // 1.0
Math.atan2(1.0, 0.0) // 1.5707963267948966- Math.sin, Math.cos, Math.tan: the sine, cosine, and tangent of an angle
- Math.asin, Math.acos, Math.atan: the inverse functions
- Math.atan2: the angle of the point
(x, y), from-PItoPI; it takesyfirst
Special float values
The float64 functions never panic. Outside its domain a function returns NaN or an infinity. NaN is not equal to anything, itself included, so test for it with Math.isNaN.
let x = Math.sqrt(-1.0)
x == Math.NaN // false
Math.isNaN(x) // true
Math.isFinite(Math.log(0.0)) // false- Math.isNaN: whether
xisNaN - Math.isFinite: whether
xis neitherNaNnor an infinity - Math: the constants
Math.PI,Math.E,Math.NaN,Math.Infinity,Math.maxFloat64,Math.minInt,Math.maxInt, andMath.maxUint64
Bitwise operations
Ant has no bitwise operators. The Bits functions take any one integer type and work on its two's complement bits at its width.
let canRead = 0b100
let canWrite = 0b010
let mode = Bits.or(canRead, canWrite)
Bits.and(mode, canWrite) != 0 // true
Bits.shiftLeft(1, 4) // 16
Bits.popCount(byte(255)) // 8
Bits.leadingZeros(byte(1)) // 7- Bits.and, Bits.or, Bits.xor, Bits.not: the logical operations on each bit
- Bits.shiftLeft, Bits.shiftRight: the bits moved by
n, arithmetic right for a signed type and logical for an unsigned one - Bits.rotateLeft, Bits.rotateRight: the bits turned by
n, the bits shifted out coming back in at the other end - Bits.popCount: the number of one bits
- Bits.leadingZeros, Bits.trailingZeros: the zero bits above the highest one bit, or below the lowest
Random numbers
A Random generator made from a seed gives the same values in the same order on every run, which makes a simulation or a test repeatable. Math.random and Math.randomInt cannot be seeded. None of them is fit for keys or tokens: use Crypto.randomBytes for those.
let rng = Random.new(42)
let rolls = Array.range(0, 5).map({ rng.int(1, 7) })
println(rolls)
let deck = ["A", "K", "Q", "J", "10"]
rng.shuffle(deck)
println(deck)[2, 2, 5, 1, 3]
["J", "A", "Q", "10", "K"]- Random.new: a generator from a seed, or from the host's secure source without one
- Random.int: a uniform
intfromfrom, inclusive, toto, exclusive - Random.float: a uniform
float64in[0, 1) - Random.bool:
trueorfalse, each half the time - Random.pick: a uniform element of an array, or
nullwhen it is empty - Random.shuffle: an array reordered in place
- Random.fork: a second generator, so each goroutine draws from its own
- Math.random, Math.randomInt: an unseeded
float64in[0, 1)orintfromfrom, inclusive, toto, exclusive - Crypto.randomBytes: bytes from the host's secure source, for keys and tokens
See also
- int and the sized integers, float32 and float64: the number types
- Number literals: how to write numbers in code
- Number, Integer, Ordered: the bounds for a function generic over numbers
