~/bend-docscommunity

decimal.bend checks

raw source on the hub · import 0xbf5d8bf07d7411858acb5142889fc4d7/decimal.bend as Decimal

Exact signed decimals: a BigInt coefficient over a power of ten.

2 imports
import Base
import ./bigint.bend as Big

Types

type Decimal source · line 8 · raw

Data

The value is unscaled / 10^scale. Scale is kept as written, so 1.50 stays 1.50 (money keeps its cents); cmp is numeric, and normalize strips trailing zeros. No operation rounds: an inexact request answers None.

type Class source · line 238 · raw

Data

A character of a number.

type Scan source · line 279 · raw

Data

Where the reader is. ds holds the coefficient's digits, last first, without leading zeros; sc counts fraction digits; e is the exponent's magnitude.

Definitions

def Decimal.zero source · line 11 · raw

Decimal

def Decimal.pow10 source · line 14 · raw

@n:Nat -> 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt

def Decimal.up source · line 18 · raw

@u:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @k:Nat -> 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt

u * 10^k, without a multiply when k is 0.

def Decimal.neg source · line 25 · raw

@d:Decimal -> Decimal

def Decimal.abs source · line 30 · raw

@d:Decimal -> Decimal

def Decimal.is_zero source · line 35 · raw

@d:Decimal -> Bool

def Decimal.add source · line 40 · raw

@a:Decimal -> @b:Decimal -> Decimal

def Decimal.sub source · line 46 · raw

@a:Decimal -> @b:Decimal -> Decimal

def Decimal.mul source · line 49 · raw

@a:Decimal -> @b:Decimal -> Decimal

def Decimal.cmp source · line 54 · raw

@a:Decimal -> @b:Decimal -> Cmp

def Decimal.quot source · line 61 · raw

@m:Maybe<&1, Pair(0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt)> -> 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt

The quotient of a division whose divisor is known to be nonzero.

def Decimal.divk.m source · line 69 · raw

@m:Maybe<&1, Pair(0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt)> -> Pair(Bool, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt)

(remainder is zero, quotient) of u / k, for a nonzero k.

def Decimal.divk source · line 76 · raw

@u:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @k:U32 -> Pair(Bool, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt)

def Decimal.is_one source · line 79 · raw

@u:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> Bool

def Decimal.lowest.fix source · line 82 · raw

@neg:Bool -> @n:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @d:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> Pair(0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt)

def Decimal.lowest.den source · line 90 · raw

@n:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @+d:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> Pair(0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt)

def Decimal.lowest.by source · line 93 · raw

@n:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @d:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @+g:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> Pair(0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt)

def Decimal.lowest source · line 99 · raw

@+n:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @+d:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> Pair(0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt)

n / d in lowest terms with a positive denominator; d must be nonzero.

def Decimal.frac.go source · line 104 · raw

@f:Nat -> @n:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @k:Nat -> @one:Bool -> @t2:Pair(Bool, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt) -> @t5:Pair(Bool, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt) -> Maybe<&2, Decimal>

Each step divides d by 2 or 5 and scales n to match, so a d of D digits needs fewer than 4D steps; f is that bound.

def Decimal.frac.run source · line 132 · raw

@p:Pair(0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt) -> Maybe<&2, Decimal>

def Decimal.frac.if source · line 139 · raw

@z:Bool -> @n:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @d:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> Maybe<&2, Decimal>

def Decimal.from_fraction source · line 149 · raw

@n:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @+d:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> Maybe<&2, Decimal>

n / d at the smallest exact scale, or None when d is zero or the decimal expansion does not terminate (d has a prime factor other than 2 and 5).

def Decimal.div_exact source · line 154 · raw

@a:Decimal -> @b:Decimal -> Maybe<&2, Decimal>

a / b exactly, at the smallest scale that holds it; None for a zero divisor or a repeating quotient (use Rational for those).

def Decimal.normalize.go source · line 160 · raw

@s:Nat -> @u:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @qr:Pair(Bool, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt) -> Decimal

def Decimal.normalize source · line 175 · raw

@d:Decimal -> Decimal

The same value at the smallest scale: 1.500 is 1.5, 0.00 is 0.

def Decimal.rescale.keep source · line 180 · raw

@z:Bool -> @q:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @t:Nat -> Maybe<&2, Decimal>

def Decimal.rescale.cut source · line 187 · raw

@t:Nat -> @m:Maybe<&1, Pair(0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt)> -> Maybe<&2, Decimal>

def Decimal.rescale.if source · line 195 · raw

@up:Bool -> @u:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @+s:Nat -> @+t:Nat -> Maybe<&2, Decimal>

def Decimal.rescale source · line 205 · raw

@d:Decimal -> @+t:Nat -> Maybe<&2, Decimal>

The same value at scale t, or None when that would drop a nonzero digit: rescale(1.5, 2n) is 1.50; rescale(1.25, 1n) is None, never 1.2 or 1.3.

def Decimal.show.split source · line 210 · raw

@+s:Nat -> @+ds:String -> String

def Decimal.show.point source · line 215 · raw

@+s:Nat -> @+ds:String -> String

Digits with a point s places from the right, zero-padded to one whole digit.

def Decimal.show.sign source · line 223 · raw

@neg:Bool -> @body:String -> String

def Decimal.show source · line 231 · raw

@d:Decimal -> String

Plain notation with every scale digit: -12.50, 0.001, 7. Never an exponent.

def Decimal.class.sym source · line 246 · raw

@minus:Bool -> @plus:Bool -> @point:Bool -> @expo:Bool -> Class

def Decimal.class.if source · line 266 · raw

@digit:Bool -> @+c:U32 -> Class

def Decimal.class source · line 274 · raw

@+c:U32 -> Class

def Decimal.read.lead source · line 290 · raw

@zero:Bool -> @c:U32 -> String

def Decimal.read.push source · line 297 · raw

@+c:U32 -> @ds:String -> String

def Decimal.read.exp source · line 306 · raw

@big:Bool -> @neg:Bool -> @ds:String -> @sc:Nat -> @eneg:Bool -> @e:U32 -> Scan

ponytail: exponents past 100000 in magnitude are refused, since 1e100000 materializes 100000 digits; raise the bound if a caller needs more.

def Decimal.read.step source · line 314 · raw

@st:Scan -> @cl:Class -> Scan

def Decimal.read.go source · line 382 · raw

@s:String -> @st:Scan -> Scan

def Decimal.read.abs source · line 389 · raw

@m:Maybe<&2, 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt> -> 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt

def Decimal.read.coef source · line 397 · raw

@neg:Bool -> @ds:String -> 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt

The coefficient; ds holds only digits, so read cannot fail.

def Decimal.read.digits source · line 404 · raw

@neg:Bool -> @ds:String -> 0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt

def Decimal.read.shift source · line 412 · raw

@u:0xbf5d8bf07d7411858acb5142889fc4d7/bigint.BigInt -> @+sc:Nat -> @eneg:Bool -> @+e:Nat -> Decimal

A negative exponent adds scale; a positive one spends scale, then multiplies.

def Decimal.read.fin source · line 419 · raw

@st:Scan -> Maybe<&2, Decimal>

def Decimal.read source · line 435 · raw

@s:String -> Maybe<&2, Decimal>

[+-]digits[.digits][(e|E)[+-]digits], a superset of JSON's number grammar (it also takes a leading + and leading zeros). The scale is the number of fraction digits less the exponent, floored at zero: "1.50" has scale 2, "1.5e1" is 15, "25e-3" is 0.025.