Skip to content

Numbers

Novis has four number types:

Type What it is
int a whole number, from about minus 9.2 quintillion to plus 9.2 quintillion
uint a whole number that is never negative
float a number with a fraction, such as 3.5
decimal an exact number with up to 28 digits after the point

/ between two whole numbers gives a float. 7 / 2 is 3.5, and 6 / 3 is 2.

The number types

Novis has one true-or-false type and four number types.

  • bool is true or false. You cannot use it in arithmetic.
  • int is a whole number that can be negative.
  • uint is a whole number that cannot be negative. It can count twice as high as an int.
  • float is a fast number with a fraction. It is not always exact, so use it for a weight or a ratio.
  • decimal is an exact number with up to 28 decimal places. Use it for money.

A number written in the code gets its type from where you put it. float $weight = 3; makes 3 a float, and decimal $price = 1.10; makes 1.10 a decimal.

If a whole number gets too big or too small for its type, the program throws an ArithmeticError. int and uint cannot be used in one sum. decimal and float cannot be used in one sum either. Convert one side with as.

The examples below show whole numbers that are too big, money and a measurement, and a parcel priced at a shop counter. The last one adds a decimal and a float, so it does not compile.

Whole numbers that never wrap

<?nvs
// Whole numbers have two types. An `int` can be negative. A `uint` cannot be negative,
// and it can count twice as high. If a result is too big for its type,
// the program throws an `ArithmeticError`. The number does not wrap around to a small value.
int $balance = -250;
uint $stock = 18446744073709551615;
echo $balance + 100, "\n";
echo $stock, "\n";
try {
echo $stock + 1, "\n";
} catch (ArithmeticError $e) {
echo 'out of room: ', $e->message, "\n";
}
bool $owing = $balance < 0;
echo $owing ? 'owing' : 'clear', "\n";
Output
-150
18446744073709551615
out of room: Integer addition overflowed
owing

Money in decimal and measurements in float

<?nvs
// `decimal` and `float` are both numbers with a fraction. A `decimal` is exact,
// so use it for money. A `float` is fast but not always exact, so use it for measurements.
// `0.1 + 0.2` prints `0.3` for both types. Only the `decimal` sum is equal to `0.3`.
float $quick = 0.1;
float $more = 0.2;
float $floatSum = $quick + $more;
echo $floatSum, ' ', $floatSum == 0.3 ? 'exactly 0.3' : 'not quite 0.3', "\n";
decimal $tenth = 0.1;
decimal $fifth = 0.2;
decimal $exactSum = $tenth + $fifth;
echo $exactSum, ' ', $exactSum == 0.3 ? 'exactly 0.3' : 'not quite 0.3', "\n";
decimal $price = 1.10;
echo $price * 3, "\n";
float $grams = 2.5;
echo $grams * 3, "\n";
Output
0.3 not quite 0.3
0.3 exactly 0.3
3.30
7.5

Price a parcel at the counter

<?nvs
// Prices a parcel at a shop counter. The number of items is a `uint`.
// The money is a `decimal`, so it stays exact to the cent.
// The weight is a `float`, because a scale is not exact either.
uint $items = 3;
decimal $each = 12.75;
float $kilos = 0.84;
decimal $goods = $each * $items;
decimal $postage = 2.50;
if ($kilos > 0.5) {
$postage = 4.90;
}
decimal $due = $goods + $postage;
echo $items, ' items at ', $each, ' each = ', $goods, "\n";
echo 'postage ', $postage, ' for ', $kilos * 1000, " g\n";
echo 'due ', $due, "\n";
Output
3 items at 12.75 each = 38.25
postage 4.90 for 840 g
due 43.15

Decimal and float do not mix

<?nvs
// Shows that a `decimal` and a `float` cannot be added. Convert one side
// with `as` first. This program does not compile.
// proof: compile-error
decimal $price = 1.10;
float $discount = 0.5;
echo $price + $discount, "\n";
Output
error[E0455]: `decimal` and `float` have no representable common type in arithmetic
--> 04-decimal-and-float-do-not-mix.nvs:8:6
|
8 | echo $price + $discount, "\n";
| ^^^^^^^^^^^^^^^^^^ mixed `decimal`/`float` operand
= help: convert one side explicitly with `as decimal`/`as float`
error: aborting due to 1 error
To read what a code means and how to fix it, run `nvs agent show <code>`.

Details

An int is a 64-bit signed whole number. A uint is a 64-bit unsigned whole number, from 0 to 18446744073709551615. A result outside that range throws an ArithmeticError, such as Integer addition overflowed. Below zero, a uint throws Integer subtraction overflowed.

-1 is never a uint, because the - is an operator. uint $u = -1; is the error E0401. Beside a uint variable, a plain number is a uint too, so $u + 1 compiles.

$i + $u with an int and a uint is the error E0407. An int variable does not assign to a uint variable either: that is the error E0401. Convert one side with as int or as uint.

When one side of a sum is a float, the other side is converted to float. A decimal and a float in one sum is the error E0455. A decimal and an int or a uint give a decimal.

A float prints without a trailing .0, so 2.0 prints 2. 0.1 + 0.2 prints 0.3, but it is not equal to 0.3.

A decimal keeps its decimal places: 1.10 * 3 is 3.30. Division rounds at the 28th place, so 1 as decimal / 3 is 0.3333333333333333333333333333.

bool has no arithmetic: $b + 1 is the error E0716. A bool converts only to string and to bool. true as int is the error E0708, so write $b ? 1 : 0. true as string is "1" and false as string is "".

true and false are also types of their own, with one value each.

The page Strings and interpolation shows the ways to write a number, such as 1_000, 0x1A and 1h30m.

  • Do use int for counts and IDs.
  • Do use decimal for money. 0.1 plus 0.2 is exactly 0.3 in a decimal.
  • Don’t use float for money. A float cannot store most decimal fractions exactly.
  • Don’t use uint only to say “not negative” inside your own code. Mixing it with int needs as on every line.