Captain's numbers with a fractional part are exact decimals. A price of
19.99 is 19.99, not the nearest binary fraction to it, and three of them
cost 59.97. This chapter explains what a Decimal is, how its literals and
arithmetic work, how it meets integers, and how decimals go in and out of a
run. Division and rounding have a chapter of their own,
Division and rounding, and quantities such
as money and durations, which are built on decimals, are covered in
Quantities and money.
The basic rules for all of Captain's numbers, including integers and the three kinds of conversion, are in Integers, booleans and conversion. The operations are listed in Numbers, tagged values and secrets.
What a decimal is
A Decimal is two integers:
- a mantissa, a signed 128-bit integer, holding the digits;
- a scale, the number of those digits that come after the decimal point.
The value is the mantissa times ten to the power of minus the scale. So
19.99 is the mantissa 1999 at scale 2, and 0.5 is 5 at scale 1.
A 128-bit mantissa holds any number of up to 38 significant digits, wherever the point falls. That is enough for any amount of money in any currency to a fraction of a cent, and for capacity counted in bytes. Past 38 digits, Captain refuses rather than narrows: a literal that is too long is refused before the program runs, and a result that is too long stops the run.
Literals and their scale
A decimal literal is digits, a point and more digits: 19.99, 0.5,
1250.00. The scale of a literal is the number of digits written after the
point, so 2.5 has scale 1 and 2.50 has scale 2.
| Written | Is |
|---|---|
19.99 |
a Decimal, scale 2 |
2.50 |
a Decimal, scale 2 |
0.000 |
a Decimal, scale 3 |
5 |
an i32, not a decimal |
.5 |
refused, CAP-LEX-NUM: write 0.5 |
1_000.00 |
refused, CAP-LEX-NUM: there are no digit separators |
1.0e3 |
refused: there is no exponent form (e3 is read as an unknown unit) |
There is no negative literal, because there is no unary minus. Write
0.0 - 2.5.
A literal with more digits than the mantissa holds is refused, with
CAP-TYPE-031, as an integer literal that does not fit is:
module Example::Huge.
export huge -> Decimal [
ret 1234567890123456789012345678901.234567890.
].It is never silently made into something else.
Arithmetic
+, - and * take two decimals and give a decimal. All three are exact.
The scale of the result follows from the operands:
| Operation | Result scale | Example |
|---|---|---|
a + b, a - b |
the larger of the two scales | 1.5 + 2.25 is 3.75; 10 - 0.75 is 9.25 |
a * b |
the two scales added | 1.5 * 2.25 is 3.375; 1.10 * 1.10 is 1.2100 |
Adding never loses a digit, because both sides are first written at the larger scale. Multiplying never loses one either, because the product of a number with two places and a number with two places needs four places to be exact. Nothing is cut to fit.
/ is exact too, or the run fails; and the only operations that drop
digits are the rounding operations, which name the scale and the rounding
mode. Both are explained in
Division and rounding.
module Example::Invoice.
;; One invoice line: a unit price times a whole number of units.
export line price: Decimal units: i32 -> Decimal [
ret price * (units as: Decimal).
].
;; Net plus tax, where the tax rate is a fraction such as 0.20.
export gross net: Decimal rate: Decimal -> Decimal [
ret net + (net * rate).
].
export add a: Decimal b: Decimal -> Decimal [ ret a + b. ].
export sub a: Decimal b: Decimal -> Decimal [ ret a - b. ].
export mul a: Decimal b: Decimal -> Decimal [ ret a * b. ].
export tenths -> Decimal [ ret 0.1 + 0.2. ].
export negative -> Decimal [ ret 0.0 - 2.5. ].$ captain build invoice.capt -o invoice.capb
built invoice.capb ([..] bytes, sha256 [..])
$ captain run invoice.capb line price=19.99 units=3
run log: line.runlog
59.97
$ captain run invoice.capb gross net=59.97 rate=0.20
run log: gross.runlog
71.9640
$ captain run invoice.capb add a=1.5 b=2.25
run log: add.runlog
3.75
$ captain run invoice.capb sub a=10 b=0.75
run log: sub.runlog
9.25
$ captain run invoice.capb mul a=1.10 b=1.10
run log: mul.runlog
1.2100
$ captain run invoice.capb tenths
run log: tenths.runlog
0.3
$ captain run invoice.capb negative
run log: negative.runlog
-2.5The gross amount, 71.9640, carries four places because a two-place amount
was multiplied by a two-place rate. That is the exact answer. A program
that wants cents says so, with a rounding that names the scale and the
mode: (net * rate) scale: 2 rounding: #HalfUp. Where the rounding happens
matters: on each line or once on the total can give different cents, and
Division and rounding
shows both.
Comparison
=, !=, <, <=, > and >= compare two decimals by their values,
whatever their scales. 2.50 = 2.5 is true, and 2.49 < 2.5 is true.
The scale is still part of the value. 2.50 and 2.5 are equal, but they
are not the same result: Text from: writes them differently, and a run's
result identity tells them apart. A calculation that should give the same
result for both inputs rounds them to one scale first.
module Example::Compare.
export same a: Decimal b: Decimal -> bool [ ret a = b. ].
export less a: Decimal b: Decimal -> bool [ ret a < b. ].
export show x: Decimal -> Text [ ret Text from: x. ].$ captain build compare.capt -o compare.capb
built compare.capb ([..] bytes, sha256 [..])
$ captain run compare.capb same a=2.50 b=2.5
run log: same.runlog
true
$ captain run compare.capb less a=2.49 b=2.5
run log: less.runlog
true
$ captain run compare.capb show x=2.50
run log: show.runlog
"2.50"
$ captain run compare.capb show x=2.5
run log: show.runlog
"2.5"Text from: shows a decimal at the scale it has. It never drops trailing
zeros and never adds any. To show an amount with two places, round it to
scale 2 first, and the text has two places.
Overflow is a checked failure
A result whose mantissa does not fit 128 bits stops the run with
CAP-RUN-ARITH. It never wraps around, and it is never cut to fit. The
same goes for a scale that grows past what a 32-bit count holds.
The way a decimal usually overflows is not a large value but a long one. Each multiplication adds the scales, so a value multiplied again and again grows a digit or more every time. This program grows a balance by a monthly factor, once with nothing rounded and once rounded to the cent each month:
module Example::Growth.
;; A balance grown by a monthly factor, such as 1.005, with nothing rounded.
export grow balance: Decimal factor: Decimal months: i32 -> Decimal [
b := balance.
i := 0.
[ i < months ] whileTrue: [
b := b * factor.
i := i + 1.
].
ret b.
].
;; The same, rounded to the cent each month, as a statement would be.
export growCents balance: Decimal factor: Decimal months: i32 -> Decimal [
b := balance.
i := 0.
[ i < months ] whileTrue: [
b := (b * factor) scale: 2 rounding: #HalfEven.
i := i + 1.
].
ret b.
].$ captain build growth.capt -o growth.capb
built growth.capb ([..] bytes, sha256 [..])
$ captain run growth.capb grow balance=1000.00 factor=1.005 months=2
run log: grow.runlog
1010.02500000
$ captain run growth.capb grow balance=1000.00 factor=1.005 months=6
run log: grow.runlog
1030.37750939376562500000
$ captain run growth.capb grow balance=1000.00 factor=1.005 months=12
run log: grow.runlog
error[CAP-RUN-ARITH]: arithmetic failure (overflow or /0)
$ captain run growth.capb growCents balance=1000.00 factor=1.005 months=12
run log: growCents.runlog
1061.68
$ captain run growth.capb growCents balance=1000.00 factor=1.005 months=600
run log: growCents.runlog
19934.95After six months the exact balance has twenty places, and in the twelfth month it no longer fits. That is not a limit to work around: a real statement rounds each month, and the program that rounds each month runs for fifty years without trouble. The failure tells you that the program had not said where its digits go.
Integers meet decimals only through as:
An integer and a decimal do not mix. price * units, with price a
Decimal and units an i32, is refused, and the help line says what to
write:
module Example::Mix.
export line price: Decimal units: i32 -> Decimal [
ret price * units.
].$ captain check mix.capt
error[CAP-TYPE-015]: cannot apply arithmetic to Decimal and i32
--> mix.capt:4:7
4 | ret price * units.
| ^^^^^^^^^^^^^
help: an integer becomes a decimal only when you say so: `(n as: Decimal)`, exactly, at scale 0
checked 1 file as 1 module, 1 with errorsn as: Decimal turns an i32 or an i64 into a decimal with the same
value, at scale 0. It is exact and cannot fail, which is why it is written
as: (as: converts a number exactly).
as: is an ordinary keyword message, so it needs parentheses inside an
expression: price * (units as: Decimal), as in line price:units: above.
The other direction does not exist. A decimal does not become an integer,
because that would drop its fraction; x as: i32 is refused with
CAP-TYPE-033. When a program needs a whole number, it rounds to scale 0
with a named mode and keeps the value a Decimal:
x scale: 0 rounding: #Down.
Why so strict? A reviewer reading price * units cannot tell whether
units is a count or an amount, nor whether something was converted on the
way. Captain programs are increasingly written by machines and read by
people, and every conversion that is written out is one the reader can see
and the checker can test.
No floating point, and why
Captain has no floating-point type at all, not even as an option. 0.1 +
0.2 is 0.3, as the tenths entry above shows.
The reasons come from what Captain calculates:
- Money, percentages and capacity are decimal quantities. Prices, rates and sizes are written in decimal, and the answer is expected in decimal. Binary floating point cannot hold 0.1 exactly, and the error, however small, shows up in a total, a comparison or a rounding.
- Results must be auditable. An auditor can redo an exact calculation by hand and get the same digits. Floating-point results depend on the order of the operations and on details of the hardware and the compiler that nobody wants to audit.
- The same inputs give the same result identity. A pure entry's result is recorded and can be re-executed later to prove it (Determinism, run records and replay). That needs every step to give the same bits everywhere, which exact integer arithmetic on a mantissa does.
What exactness costs is that a program must say what to do when an answer has more digits than it wants. That is the subject of the next chapter.
Decimals in and out of captain run
On the command line, a Decimal argument is written as digits with an
optional minus sign and an optional point: price=19.99, amount=-3.5,
rate=0.20. The scale is the number of digits written after the point,
so amount=100 and amount=100.00 are different inputs: equal, but not
the same, and recorded as different arguments. Signs other than a leading
minus, digit separators and exponents are refused before the run:
$ captain run compare.capb show x=0.000
run log: show.runlog
"0.000"
$ captain run compare.capb show x=-0.5
run log: show.runlog
"-0.5"
$ captain run compare.capb show x=1,250.00
error: argument `x`: `1,250.00` is not a decimal
$ captain run compare.capb show x=1e3
error: argument `x`: `1e3` is not a decimalA decimal result is printed the way Text from: writes it, at its scale,
without quotes: 59.97, 71.9640.
A program embedded in Rust passes and receives decimals through the
Captain SDK as a mantissa and a scale,
InputValue::Decimal { mantissa: 1999, scale: 2 } for 19.99, and the
result comes back the same way. An integer is not accepted where a decimal
is wanted, even when its value would fit: the SDK converts nothing, as the
language does not. See
Values in and out.