Numbers, Bool and Char
Operators
Section titled “Operators”| Operator | On | Gives |
|---|---|---|
+ - * | Int, Float, BigInt | the sum, difference, product |
/ | Int, BigInt | the quotient, cut towards zero (7 / 2 is 3, -7 / 2 is -3) |
/ | Float | the exact quotient (7.0 / 2.0 is 3.5) |
% | Int, BigInt | the remainder, with the sign of the number divided |
&, |, ^ | Int | and, or, exclusive or, bit by bit: flags & 4. |
<<, >> | Int | moves the bits left or right, the sign kept by >>: 1 << 3. The shift goes from 0 to 63, otherwise the program stops. |
-x | Int, Float, BigInt | the opposite |
= != < <= > >= | numbers, Char, Bool (=, !=) | a Bool |
&& || ! | Bool | and, or, not; the right side is computed only when needed |
Both sides of an operator have the same type: n.to_float * 1.5, never n * 1.5.
A result too large for an Int stops the program (integer overflow), and so does a division by
zero, for Int, BigInt and Float alike. The program never goes on with a wrong value.
Quotient and remainder
Section titled “Quotient and remainder”/ gives the quotient and % the remainder of a division between whole numbers: they go together,
a = (a / b) * b + (a % b).
fn main { let a: Int := 17 let b: Int := 5 print("{a} = {a / b} × {b} + {a % b}") print(-7 / 2) print(-7 % 2)}17 = 3 × 5 + 2-3-1% is not a percentage: it is only the remainder. Its usual uses are testing parity (n % 2 = 0),
divisibility (n % 5 = 0) and going back to the start (index % size).
For a negative number, / cuts towards zero, so the remainder is negative too (-7 % 2 is -1).
The Euclidean division always leaves a remainder from 0 to b.abs - 1; it is what to use to go
round a List or a clock with a number that may be negative:
fn main { let (q, r): (Int, Int) := 17.divmod(5) print("{q} {r}") print((-7).div_euclid(2)) print((-7).rem_euclid(2))}3 2-41a.div_euclid(b), a.rem_euclid(b) and a.divmod(b) (both in a tuple) exist for Int and for
BigInt. % does not apply to a Float.
A whole number of 64 bits, from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Written
42, -7, 1_000_000 (the _ are ignored), 0xff (hexadecimal).
| Method | Gives |
|---|---|
n.abs -> Int | the value without its sign. |
n.min(other: Int) -> Int | the smaller of the two. |
n.max(other: Int) -> Int | the larger of the two. |
n.pow(e: Int) -> Int | n to the power e; e must not be negative. |
n.div_euclid(other: Int) -> Int, n.rem_euclid(other: Int) -> Int | the quotient and the remainder of the Euclidean division: the remainder is never negative. |
n.divmod(other: Int) -> (Int, Int) | both at once: 17.divmod(5) is (3, 2). |
n.gcd(other: Int) -> Int | the greatest number that divides both (12.gcd(18) is 6). |
n.factorial -> Int | 1 * 2 * … * n (5.factorial is 120); past 20 it overflows: use a BigInt. |
n.to_float -> Float | the same number as a Float. |
n.to_bigint -> BigInt | the same number as a BigInt. |
n.to_char -> Char? | the character whose Unicode code is n (65.to_char is 'A'), none if there is none. |
n.to_string -> String | its digits. |
fn main { print((-5).abs) print(3.min(8)) print(2.pow(10)) print(7.to_float / 2.0) print(65.to_char)}5310243.5'A'A number with a fraction, 64 bits (IEEE 754 double), about 15 to 17 significant digits. Written
3.14, 2.0, 1e-3. A whole number written in the code becomes a Float where one is expected:
let x: Float := 2.
| Method | Gives |
|---|---|
x.abs -> Float | the value without its sign. |
x.min(other: Float) -> Float, x.max(other: Float) -> Float | the smaller, the larger. |
x.floor -> Int | the largest whole number below or equal ((-2.5).floor is -3). |
x.ceil -> Int | the smallest whole number above or equal. |
x.round -> Int | the nearest whole number, halves away from zero (2.5.round is 3). |
x.round(digits: Int) -> Float | the value kept to digits decimals (3.14159.round(2) is 3.14). |
x.fixed(digits: Int) -> String | a text with exactly digits decimals (2.5.fixed(2) is 2.50): the way to show prices and measures. |
x.to_int -> Int | the whole part, cut towards zero ((-2.7).to_int is -2). |
x.sqrt -> Float | the square root (NaN for a negative number). |
x.pow(e: Int) -> Float, x.pow(e: Float) -> Float | x to the power e. |
x.exp -> Float, x.ln -> Float, x.log10 -> Float | e to the power x, the natural logarithm, the base 10 logarithm. |
x.sin, x.cos, x.tan -> Float | trigonometry, x in radians. |
x.asin, x.acos, x.atan -> Float | the inverse functions, in radians. |
y.atan2(x: Float) -> Float | the angle of the point (x, y), in radians, from -pi to pi. |
x.sinh, x.cosh, x.tanh -> Float | the hyperbolic functions. |
x.log2 -> Float | the base 2 logarithm. |
x.hypot(y: Float) -> Float | the square root of x² + y². |
Float.e -> Float | Euler’s number, next to Float.pi. |
Float.pi -> Float | π, 3.141592653589793. |
x.to_string -> String | its shortest exact text: 0.1 stays 0.1. |
x.floor, x.ceil, x.round and x.to_int stop the program when the result does not fit in an
Int.
fn main { let r: Float := 2.0 print(Float.pi * r.pow(2)) print(2.0.sqrt) print((Float.pi / 2.0).sin) print(1.0.exp) print(3.7.round)}12.5663706143591721.41421356237309511.02.7182818284590454Every one of these gives the same digits on every system, built or run: Kova computes them
itself, correctly rounded where it matters (exp, ln, log10, pow), instead of asking the
system, whose libraries differ in the last digit.
BigInt
Section titled “BigInt”A whole number without a limit, for what does not fit in 64 bits: factorials, cryptography,
exact counts. A number too large for an Int, written in the code, is a BigInt; a smaller one
becomes a BigInt where one is expected.
| Method | Gives |
|---|---|
b.pow(e: Int) -> BigInt | b to the power e (2.to_bigint.pow(100) is 1267650600228229401496703205376). |
b.abs -> BigInt | the value without its sign. |
b.min(other: BigInt), b.max(other: BigInt) -> BigInt | the smaller, the larger. |
b.div_euclid(other), b.rem_euclid(other), b.divmod(other) | the Euclidean division, as for Int. |
b.gcd(other: BigInt) -> BigInt | the greatest number that divides both. |
b.factorial -> BigInt | 1 * 2 * … * b (25.to_bigint.factorial is 15511210043330985984000000). |
b.to_int -> Int? | the same number as an Int, none if it does not fit. |
b.to_string -> String | its digits. |
The operators + - * / %, the comparisons and -b work on BigInts, and n.to_bigint turns an Int
into one.
fn main { var f: BigInt := 1 for i in 1..=30 { f := f * i.to_bigint } print(f) print(f.to_int)}265252859812191058636308480000000nonetrue or false. &&, || and ! combine them; if and while take them. b.to_string is
"true" or "false".
One Unicode character, written between single quotes: 'a', 'é', '\n'. A String is made of
them (s.chars).
| Method | Gives |
|---|---|
c.to_int -> Int | its Unicode code ('A'.to_int is 65). |
c.is_letter -> Bool | a letter of any alphabet ('é', 'я'). |
c.is_digit -> Bool | one of 0 to 9. |
c.is_space -> Bool | a space, a tab, a line break… |
c.is_upper -> Bool, c.is_lower -> Bool | an uppercase or lowercase letter. |
c.to_upper -> Char, c.to_lower -> Char | the same letter in the other case. |
c.to_string -> String | a text of this one character. |
Characters compare by their codes: 'a' < 'b'.
Every number also has
Section titled “Every number also has”x.clone: a copy (for numbers, the value itself);x.to_string: the textprintwrites.