Core\Decimal
Calculations on decimal numbers that the operators cannot do, such as a division with a chosen rounding, or a split into parts that add up exactly.
Novis's decimal scalar carries its own arithmetic as operators — +, -, *, % and / all work directly on it, exactly, with no class in the way. Division is the one operation that may be inexact, and the / operator's answer to that is fixed in the language: round half to even, at the widest scale the result admits. No setting changes this precision for the whole program. Such a setting would be a global variable that unrelated code reads.
Core\Decimal is where a program says something the operator cannot. These questions come up in money code, and nowhere else does the type system help with them:
- "this division must come out even" —
divideExactanswers the quotient when it is exact and throwsArithmeticErrorwhen it is not. A third of a bill is not a number you should get back by accident. - "round it here, this way" —
divideRoundedtakes the number of places and the rounding mode as ordinary arguments, both required. Neither has a default, because a default for either would be that same ambient precision moved into a signature. The mode isCore\RoundMode, the same enumCore\Math::roundtakes. - "split this sum and lose none of it" —
allocatesplits an amount by a list of ratios into parts that add back to it exactly. Each part is its share truncated to the amount's own scale, and the units left over go one each to the earliest parts. The keys are the ratios', so a split written under the names of its parties answers under those names. - "raise it to a power, exactly" —
powermultiplies a base by itself as many times as you ask.**has no row for adecimalbase at all, because a power that does not come out exact would have to round without being asked; a negative power is written as the division it is. - "cut it to this many places" —
floor,ceil,truncateandround, which answer adecimalat the scale you name and not anint.roundis the one with a decision to make, so it takes the mode; the other three are directions. Converting tointafterwards is a second step, written out, because a conversion that rounded on its own would be rounding you never asked for.
divideRounded rounds once, from the exact quotient. It is not the / operator's answer rounded a second time — rounding twice is how a quotient one digit past the place you asked for carries a tie the exact value never had.
The scale you name is the scale you get: divideRounded($x, $y, 5, …) answers at five places even where fewer would do, because a rendered price needs its trailing zeros. A scale past 28 is refused rather than quietly narrowed, as is a quotient whose digits will not fit at the scale you asked for.
<?nvsdecimal $bill = 100.00;decimal $three = 3;decimal $four = 4;
// An even split answers exactly, at the scale the quotient needs.echo Core\Decimal::divideExact($bill, $four), "\n";
// An uneven one refuses, rather than handing back 33.33... and letting the// missing penny turn up in a reconciliation months later.try { echo Core\Decimal::divideExact($bill, $three);} catch (ArithmeticError $uneven) { echo "not an even split\n";}
// Where rounding is the business rule, it is written at the call site.echo Core\Decimal::divideRounded($bill, $three, 2, Core\RoundMode::HalfUp), "\n";echo Core\Decimal::divideRounded($bill, $three, 2, Core\RoundMode::Up), "\n";
// One eighth is 0.125 exactly, so two places is the tie the `Half*` modes are// named for -- and the scale is the answer's, so it pads.decimal $one = 1;decimal $eight = 8;echo Core\Decimal::divideRounded($one, $eight, 2, Core\RoundMode::HalfEven), '|', Core\Decimal::divideRounded($one, $eight, 2, Core\RoundMode::HalfUp), '|', Core\Decimal::divideRounded($one, $four, 5, Core\RoundMode::HalfEven), "\n";25not an even split33.3333.340.12|0.13|0.25000A split is the one place none of that is enough: three even shares of five cents do not exist, whatever rounding you name, and the penny has to land somewhere. allocate lands it on the earliest part and answers parts that add back to the amount exactly.
<?nvs// A five-cent bill split three ways. The parts cannot be equal, and they// still add back to the amount.decimal $nickel = 0.05;foreach (Core\Decimal::allocate($nickel, [1, 1, 1]) as decimal $part) { echo $part, "\n";}
// The keys are the ratios', so the parties keep their names -- and the// leftover unit goes to the one written first.array<decimal> $shares = ['alice' => 1, 'bob' => 2];foreach (Core\Decimal::allocate(10.00, $shares) as string $name => decimal $share) { echo $name, ' ', $share, "\n";}
// A power is repeated multiplication to the digit, scale included. The// reciprocal is a division, so it names the rounding it needs.decimal $rate = 1.05;echo Core\Decimal::power($rate, 2), '|', Core\Decimal::divideRounded(1, Core\Decimal::power($rate, 2), 4, Core\RoundMode::HalfEven), "\n";0.020.020.01alice 3.34bob 6.661.1025|0.9070Cutting a value to a number of places is the same question from the other end, and the four members that do it answer a decimal rather than an int — which is what makes the scale worth naming at all. The scale you ask for is the scale you get, so a cut pads a narrower value as well as trimming a wider one, and it defaults to zero: a whole number, still a decimal.
floor, ceil and truncate are directions and take no mode. round is the one with a decision to make — which of two neighbours a value exactly between them goes to — so it names the mode, and the mode is written before the scale because the scale is the argument you may leave out.
<?nvs// The scale you name is the scale you get, so a cut pads as well as trims.decimal $price = 1.005;echo Core\Decimal::floor($price, 2), '|', Core\Decimal::ceil($price, 2), "\n";
// Left out, it is zero: a whole number that is still a `decimal`, so the// conversion to `int` is written after it rather than hidden inside it.echo Core\Decimal::truncate($price), '|', Core\Decimal::floor($price) as int, "\n";
// Half a cent is the tie the `Half*` modes are named for, and the reason// `round` has no default mode.echo Core\Decimal::round($price, Core\RoundMode::HalfEven, 2), '|', Core\Decimal::round($price, Core\RoundMode::HalfUp, 2), "\n";
// Negative is where the three directions part: down the line, up the line,// and towards zero. A negative `decimal` is built by subtraction.decimal $loss = 0 - $price;echo Core\Decimal::floor($loss), '|', Core\Decimal::ceil($loss), '|', Core\Decimal::truncate($loss), "\n";1.00|1.011|11.00|1.01-2|-1|-1Members
8 members.
| Member | Signature |
|---|---|
divideExact | divideExact(decimal $value, decimal $divisor): decimal |
divideRounded | divideRounded(decimal $value, decimal $divisor, uint $scale, Core\RoundMode $mode): decimal |
allocate | allocate(decimal $amount, array<decimal> $ratios): array<decimal> |
power | power(decimal $base, uint $exponent): decimal |
floor | floor(decimal $value, uint $scale = 0): decimal |
ceil | ceil(decimal $value, uint $scale = 0): decimal |
truncate | truncate(decimal $value, uint $scale = 0): decimal |
round | round(decimal $value, Core\RoundMode $mode, uint $scale = 0): decimal |
