Skip to content
Jennifer Programming Language

decimal - exact decimal arithmetic

Enable with use decimal;. Base-10 numbers that are exact: 0.1 + 0.2 is 0.3, a price times a quantity is the right number of cents, and nothing rounds unless the program says how. A float cannot do that (most decimal fractions have no exact binary form), which is why money, invoices, tax and measurements with a fixed number of places belong in a decimal.Decimal.

jennifer
use io;
use decimal;

def net as decimal.Decimal init decimal.mul("19.99", 3);              # 59.97
def tax as decimal.Decimal init decimal.round(decimal.mul($net, "0.20"), 2);
io.printf("%v\n", decimal.add($net, $tax));                             # 71.96

Runnable: examples/decimal.j.

A value is an opaque decimal.Decimal: an arbitrary-size integer coefficient and a scale, the number of digits after the point. 12.340 has scale 3. Values are immutable; every call returns a new one. Backed by Go's math/big; works on both binaries.

Operands

Every function that takes a number accepts a decimal.Decimal, an int, or a decimal string ("12.34", "-0.5", "1e3"), so literals need no wrapping: decimal.mul($price, 3), decimal.add($a, "0.05"). A float is refused as an operand: convert it explicitly with decimal.from, so a binary rounding error never slips in unnoticed.

A decimal string is [+-]digits[.digits], or [+-].digits, optionally followed by an exponent e / E [+-]digits. No spaces, digit separators, NaN or infinities.

Surface

CallReturnsNotes
decimal.from(x)DecimalFrom a decimal string, an int, or a float. A float goes in by its shortest decimal form, the one %v prints: from(0.1) is 0.1, not 0.1000000000000000055511151231257827.
decimal.add(a, b) / decimal.sub(a, b)DecimalExact. The result's scale is the larger of the two.
decimal.mul(a, b)DecimalExact. The scales add: 10.00 * 1.2 is 12.000.
decimal.div(a, b[, scale[, mode]])DecimalSee Division.
decimal.round(d, scale[, mode])DecimalExactly scale places: rounded by mode (default "half-even") when that drops digits, padded with zeros when it adds them (round("1.5", 2) is 1.50).
decimal.neg(d) / decimal.abs(d)DecimalNegation / absolute value.
decimal.normalize(d)DecimalDrop trailing zeros after the point: 12.3400 -> 12.34.
decimal.pow(d, n)Decimald to the power of an int n >= 0, exactly.
decimal.sum(xs)DecimalThe sum of a list of operands; 0 for an empty list.
decimal.compare(a, b)int-1, 0 or 1. The ordering operators do not apply to a Decimal; use this.
decimal.sign(d)int-1, 0 or 1.
decimal.scale(d)intThe number of digits after the point.
decimal.isInteger(d)boolNo fractional part (3.000 is an integer).
decimal.toInt(d)intOnly an integral value that fits an int; anything else is an error (round first).
decimal.toFloat(d)floatThe nearest float. A value too large for a float is an error.

Equality, ordering, display

== compares values: decimal.from("1.0") == decimal.from("1.00") is true. The scale is still kept and shown (%v of 1.00 prints 1.00), and decimal.scale tells the two apart when the number of places matters. For ordering use decimal.compare(a, b) < 0; < / > on a Decimal are a positioned error, like on any opaque value.

%v, convert.toString and the REPL print plain notation with exactly the value's scale, never an exponent: 12.340, -0.05, 1267650600228229401496703205376. An uninitialised def d as decimal.Decimal; is 0.

Division

Addition, subtraction and multiplication are always exact. Division is not (1 / 3 has no finite decimal form), so div makes the choice explicit:

  • div(a, b) (no scale) returns the exact quotient when it has a finite decimal form, and throws Error{kind: "decimal"} otherwise. div(1, 8) is 0.125; div(1, 3) throws. The result keeps the operands' scale difference (a's scale minus b's, at least 0) and gains places only where the quotient needs them, as Java's BigDecimal and Python's decimal do: 7.50 / 2.5 is 3.0, 1 / 4 is 0.25.
  • div(a, b, scale[, mode]) returns the quotient at exactly scale places, rounded by mode (default "half-even"): div(10, 3, 4) is 3.3333, div(2, 3, 2, "up") is 0.67.

Division by zero is Error{kind: "decimal"} either way.

Rounding modes

round and div take an optional mode; "half-even" is the default.

ModeRule2.5-2.51.6-1.1
"half-even"to nearest; a tie goes to the even neighbour (banker's rounding, no upward bias)2-22-1
"half-up"to nearest; a tie goes away from zero (schoolbook rounding)3-32-1
"half-down"to nearest; a tie goes toward zero2-22-1
"up"away from zero3-32-2
"down"toward zero (truncate)2-21-1
"ceiling"toward positive infinity3-22-1
"floor"toward negative infinity2-31-2

Rounding happens on the exact decimal value, so round("2.675", 2) is 2.68 (half-even keeps the even 8), unlike a float, where 2.675 is really 2.67499999....

Big integers

There is no separate big-integer type: a Decimal with scale 0 is one. decimal.pow(2, 100) is 1267650600228229401496703205376, and add, sub, mul, compare and isInteger work past the int range. toInt converts back when the value fits.

Limits and errors

Errors

All are catchable with try / catch.

KindCause
"decimal"A malformed decimal string, division by zero, a scale-less division with no finite result, toInt of a fractional or too-large value, toFloat past the float range.
"limit"A value with more than 1,000,000 digits (100,000 on jennifer-tiny; coefficient or scale), from pow, mul, add, div, round or a parsed string such as "1e1000000".
"runtime"An argument of the wrong kind: a float or bool operand, an unknown rounding mode, a negative scale (decimal.mul: a float operand must go through decimal.from first).

Parsing and arithmetic count digits the same way (leading zeros do not count), so convert.toString of any Decimal parses back. 543^2 (decimal.pow(5, 262144), 183,231 digits) fits on the default build, where printing or parsing a value at the cap takes about 0.3 s.

Deprecated

Still working; jennifer lint reports a call (L301).

CallUse insteadRemoved in
decimal.toString(d)convert.toString(d) or %v (identical text); for an int or string operand, convert.toString(decimal.from(x))0.29.0

Not here

Square roots, logarithms and other irrational functions (no finite decimal result; use math on a float); a global precision or rounding context (every rounding is stated at the call); locale-aware formatting with digit grouping or a decimal comma. Currency amounts with per-currency minor units, allocation and conversion are planned as a money deck on top of this library.

Academy talks

TalkWhat it shows
Jennifer 0.26: what's new, what's deprecated, what breaksthe new decimal library
Numbers and data: decimal, stats, linalg, ml, plot and dotexact amounts with explicit rounding modes and division