/// `Dec`: a 128-bit fixed-point decimal for money (ADR 0070). /// /// result.expect (dec.parse "0.1") "a" + result.expect (dec.parse "0.2") "b" /// == result.expect (dec.parse "0.3") "c" // True /// show (dec.from_int 1 / dec.from_int 3) // "0.333333333333333333" /// show (dec.round (result.expect (dec.parse "2.5") "d") ~places: 0) /// // "2" (ties to even) /// /// A `Dec` is an integer count of 10^-18 units: 18 decimal places, an /// integral part up to about ±1.7·10^20. Every inexact operation rounds /// to the nearest, ties to even. `+`, `-`, `*` and `/` panic on overflow /// (money must not wrap); `/` panics on zero. There are no `Dec` /// literals (the `U64` precedent): use `parse` or `from_int`. There is /// no implicit `Float`↔`Dec` conversion, ever (ADR 0034's spirit). A /// `Dec` is boxed, as a `Float` is: every result allocates. /// A fixed-point decimal. @builtin pub type Dec /// `n` units of 1. Panics when `n` is out of range (about ±1.7·10^20). @extern c "tl_dec_from_int" pub fn from_int :: n: Int -> Dec /// The whole units of `x`, truncated toward zero (`to_int 12.99` is 12, /// `to_int (-12.99)` is -12). @extern c "tl_dec_to_int" pub fn to_int :: x: Dec -> Int /// The nearest `Dec` to `x`, through its shortest decimal text: `0.1` /// the float is `"0.1"` the string is `0.1` the `Dec`, not the binary /// expansion. An `Error` for NaN, an infinity, or a float out of range. pub fn from_float :: x: Float -> Result { let (ok, d) = from_float_raw x if ok { Ok(d) } else { Error("not a finite decimal in range: " <> debug x) } } @extern c "tl_dec_from_float" fn from_float_raw :: x: Float -> (Bool, Dec) /// The nearest Float to `x` (explicitly lossy: 53 bits of precision). @extern c "tl_dec_to_float" pub fn to_float :: x: Dec -> Float /// The `Dec` written in `s`: an optional sign, decimal digits, an /// optional point and more digits (`"1"`, `"1.20"`, `".5"`; no /// exponent). More than 18 fractional digits round half-even; an /// out-of-range text is an `Error`. pub fn parse :: s: String -> Result { let (ok, d) = parse_raw s if ok { Ok(d) } else { Error("not a decimal: " <> debug s) } } @extern c "tl_dec_parse" fn parse_raw :: s: String -> (Bool, Dec) /// `x` rounded to `places` decimal places, half-even (the money /// operation: `round total ~places: 2`). Panics when `places` is outside /// `0..=18` and on overflow. @extern c "tl_dec_round" pub fn round :: x: Dec ~places: Int -> Dec /// The raw count of 10^-18 units, as an `Int` (a big integer outside 63 /// bits): what a binary codec encodes (PostgreSQL's `NUMERIC` does). @extern c "tl_dec_scaled" pub fn scaled :: x: Dec -> Int /// The `Dec` whose raw count of 10^-18 units is `n`; an `Error` when /// `n` does not fit in 128 bits. pub fn from_scaled :: n: Int -> Result { let (ok, d) = from_scaled_raw n if ok { Ok(d) } else { Error("out of range for a decimal: " <> debug n) } } @extern c "tl_dec_from_scaled" fn from_scaled_raw :: n: Int -> (Bool, Dec) /// 0 pub fn zero :: -> Dec { from_int 0 } /// 1 pub fn one :: -> Dec { from_int 1 } /// `x` without its sign. pub fn abs :: x: Dec -> Dec { if x < zero () { -x } else { x } } @extern c "tl_dec_add" fn add :: a: Dec b: Dec -> Dec @extern c "tl_dec_sub" fn sub :: a: Dec b: Dec -> Dec @extern c "tl_dec_mul" fn mul :: a: Dec b: Dec -> Dec @extern c "tl_dec_div" fn div :: a: Dec b: Dec -> Dec @extern c "tl_dec_rem" fn rem :: a: Dec b: Dec -> Dec @extern c "tl_dec_eq" fn equal :: a: Dec b: Dec -> Bool @extern c "tl_dec_compare" fn order :: a: Dec b: Dec -> Order @extern c "tl_dec_show" fn decimal :: x: Dec -> String @extern c "tl_hash_dec" fn hash_dec :: x: Dec -> Int impl Num { fn add :: x y { add x y } fn sub :: x y { sub x y } fn mul :: x y { mul x y } fn div :: x y { div x y } fn rem :: x y { // The remainder of a truncating division (exact: the scales cancel). rem x y } fn negate :: x { sub (zero ()) x } } impl Eq { fn eq :: x y { equal x y } } impl Ord { fn compare :: x y { order x y } } impl Show { fn show :: x { decimal x } } impl Debug { fn debug :: x { decimal x } } impl Hash { fn hash :: x { hash_dec x } } impl Default { fn default :: { zero () } }