Skip to content

Numbers, Bool and Char

OperatorOnGives
+ - *Int, Float, BigIntthe sum, difference, product
/Int, BigIntthe quotient, cut towards zero (7 / 2 is 3, -7 / 2 is -3)
/Floatthe exact quotient (7.0 / 2.0 is 3.5)
%Int, BigIntthe remainder, with the sign of the number divided
&, |, ^Intand, or, exclusive or, bit by bit: flags & 4.
<<, >>Intmoves the bits left or right, the sign kept by >>: 1 << 3. The shift goes from 0 to 63, otherwise the program stops.
-xInt, Float, BigIntthe opposite
= != < <= > >=numbers, Char, Bool (=, !=)a Bool
&& || !Booland, 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.

/ 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
-4
1

a.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).

MethodGives
n.abs -> Intthe value without its sign.
n.min(other: Int) -> Intthe smaller of the two.
n.max(other: Int) -> Intthe larger of the two.
n.pow(e: Int) -> Intn to the power e; e must not be negative.
n.div_euclid(other: Int) -> Int, n.rem_euclid(other: Int) -> Intthe 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) -> Intthe greatest number that divides both (12.gcd(18) is 6).
n.factorial -> Int1 * 2 * … * n (5.factorial is 120); past 20 it overflows: use a BigInt.
n.to_float -> Floatthe same number as a Float.
n.to_bigint -> BigIntthe 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 -> Stringits digits.
fn main {
print((-5).abs)
print(3.min(8))
print(2.pow(10))
print(7.to_float / 2.0)
print(65.to_char)
}
5
3
1024
3.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.

MethodGives
x.abs -> Floatthe value without its sign.
x.min(other: Float) -> Float, x.max(other: Float) -> Floatthe smaller, the larger.
x.floor -> Intthe largest whole number below or equal ((-2.5).floor is -3).
x.ceil -> Intthe smallest whole number above or equal.
x.round -> Intthe nearest whole number, halves away from zero (2.5.round is 3).
x.round(digits: Int) -> Floatthe value kept to digits decimals (3.14159.round(2) is 3.14).
x.fixed(digits: Int) -> Stringa text with exactly digits decimals (2.5.fixed(2) is 2.50): the way to show prices and measures.
x.to_int -> Intthe whole part, cut towards zero ((-2.7).to_int is -2).
x.sqrt -> Floatthe square root (NaN for a negative number).
x.pow(e: Int) -> Float, x.pow(e: Float) -> Floatx to the power e.
x.exp -> Float, x.ln -> Float, x.log10 -> Floate to the power x, the natural logarithm, the base 10 logarithm.
x.sin, x.cos, x.tan -> Floattrigonometry, x in radians.
x.asin, x.acos, x.atan -> Floatthe inverse functions, in radians.
y.atan2(x: Float) -> Floatthe angle of the point (x, y), in radians, from -pi to pi.
x.sinh, x.cosh, x.tanh -> Floatthe hyperbolic functions.
x.log2 -> Floatthe base 2 logarithm.
x.hypot(y: Float) -> Floatthe square root of x² + y².
Float.e -> FloatEuler’s number, next to Float.pi.
Float.pi -> Floatπ, 3.141592653589793.
x.to_string -> Stringits 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.566370614359172
1.4142135623730951
1.0
2.718281828459045
4

Every 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.

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.

MethodGives
b.pow(e: Int) -> BigIntb to the power e (2.to_bigint.pow(100) is 1267650600228229401496703205376).
b.abs -> BigIntthe value without its sign.
b.min(other: BigInt), b.max(other: BigInt) -> BigIntthe smaller, the larger.
b.div_euclid(other), b.rem_euclid(other), b.divmod(other)the Euclidean division, as for Int.
b.gcd(other: BigInt) -> BigIntthe greatest number that divides both.
b.factorial -> BigInt1 * 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 -> Stringits 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)
}
265252859812191058636308480000000
none

true 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).

MethodGives
c.to_int -> Intits Unicode code ('A'.to_int is 65).
c.is_letter -> Boola letter of any alphabet ('é', 'я').
c.is_digit -> Boolone of 0 to 9.
c.is_space -> Boola space, a tab, a line break…
c.is_upper -> Bool, c.is_lower -> Boolan uppercase or lowercase letter.
c.to_upper -> Char, c.to_lower -> Charthe same letter in the other case.
c.to_string -> Stringa text of this one character.

Characters compare by their codes: 'a' < 'b'.

  • x.clone: a copy (for numbers, the value itself);
  • x.to_string: the text print writes.