louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#91 · main · 15 min 44 s
0Étoiles
0Forks
40Issues
| 1 | /// `Dec`: a 128-bit fixed-point decimal for money (ADR 0070). |
| 2 | /// |
| 3 | /// result.expect (dec.parse "0.1") "a" + result.expect (dec.parse "0.2") "b" |
| 4 | /// == result.expect (dec.parse "0.3") "c" // True |
| 5 | /// show (dec.from_int 1 / dec.from_int 3) // "0.333333333333333333" |
| 6 | /// show (dec.round (result.expect (dec.parse "2.5") "d") ~places: 0) |
| 7 | /// // "2" (ties to even) |
| 8 | /// |
| 9 | /// A `Dec` is an integer count of 10^-18 units: 18 decimal places, an |
| 10 | /// integral part up to about ±1.7·10^20. Every inexact operation rounds |
| 11 | /// to the nearest, ties to even. `+`, `-`, `*` and `/` panic on overflow |
| 12 | /// (money must not wrap); `/` panics on zero. There are no `Dec` |
| 13 | /// literals (the `U64` precedent): use `parse` or `from_int`. There is |
| 14 | /// no implicit `Float`↔`Dec` conversion, ever (ADR 0034's spirit). A |
| 15 | /// `Dec` is boxed, as a `Float` is: every result allocates. |
| 16 | |
| 17 | /// A fixed-point decimal. |
| 18 | @builtin |
| 19 | pub type Dec |
| 20 | |
| 21 | /// `n` units of 1. Panics when `n` is out of range (about ±1.7·10^20). |
| 22 | @extern c "tl_dec_from_int" |
| 23 | pub fn from_int :: n: Int -> Dec |
| 24 | |
| 25 | /// The whole units of `x`, truncated toward zero (`to_int 12.99` is 12, |
| 26 | /// `to_int (-12.99)` is -12). |
| 27 | @extern c "tl_dec_to_int" |
| 28 | pub fn to_int :: x: Dec -> Int |
| 29 | |
| 30 | /// The nearest `Dec` to `x`, through its shortest decimal text: `0.1` |
| 31 | /// the float is `"0.1"` the string is `0.1` the `Dec`, not the binary |
| 32 | /// expansion. An `Error` for NaN, an infinity, or a float out of range. |
| 33 | pub fn from_float :: x: Float -> Result<Dec, String> |
| 34 | { |
| 35 | let (ok, d) = from_float_raw x |
| 36 | if ok |
| 37 | { |
| 38 | Ok(d) |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | Error("not a finite decimal in range: " <> debug x) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | @extern c "tl_dec_from_float" |
| 47 | fn from_float_raw :: x: Float -> (Bool, Dec) |
| 48 | |
| 49 | /// The nearest Float to `x` (explicitly lossy: 53 bits of precision). |
| 50 | @extern c "tl_dec_to_float" |
| 51 | pub fn to_float :: x: Dec -> Float |
| 52 | |
| 53 | /// The `Dec` written in `s`: an optional sign, decimal digits, an |
| 54 | /// optional point and more digits (`"1"`, `"1.20"`, `".5"`; no |
| 55 | /// exponent). More than 18 fractional digits round half-even; an |
| 56 | /// out-of-range text is an `Error`. |
| 57 | pub fn parse :: s: String -> Result<Dec, String> |
| 58 | { |
| 59 | let (ok, d) = parse_raw s |
| 60 | if ok |
| 61 | { |
| 62 | Ok(d) |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | Error("not a decimal: " <> debug s) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @extern c "tl_dec_parse" |
| 71 | fn parse_raw :: s: String -> (Bool, Dec) |
| 72 | |
| 73 | /// `x` rounded to `places` decimal places, half-even (the money |
| 74 | /// operation: `round total ~places: 2`). Panics when `places` is outside |
| 75 | /// `0..=18` and on overflow. |
| 76 | @extern c "tl_dec_round" |
| 77 | pub fn round :: x: Dec ~places: Int -> Dec |
| 78 | |
| 79 | /// The raw count of 10^-18 units, as an `Int` (a big integer outside 63 |
| 80 | /// bits): what a binary codec encodes (PostgreSQL's `NUMERIC` does). |
| 81 | @extern c "tl_dec_scaled" |
| 82 | pub fn scaled :: x: Dec -> Int |
| 83 | |
| 84 | /// The `Dec` whose raw count of 10^-18 units is `n`; an `Error` when |
| 85 | /// `n` does not fit in 128 bits. |
| 86 | pub fn from_scaled :: n: Int -> Result<Dec, String> |
| 87 | { |
| 88 | let (ok, d) = from_scaled_raw n |
| 89 | if ok |
| 90 | { |
| 91 | Ok(d) |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | Error("out of range for a decimal: " <> debug n) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @extern c "tl_dec_from_scaled" |
| 100 | fn from_scaled_raw :: n: Int -> (Bool, Dec) |
| 101 | |
| 102 | /// 0 |
| 103 | pub fn zero :: -> Dec |
| 104 | { |
| 105 | from_int 0 |
| 106 | } |
| 107 | |
| 108 | /// 1 |
| 109 | pub fn one :: -> Dec |
| 110 | { |
| 111 | from_int 1 |
| 112 | } |
| 113 | |
| 114 | /// `x` without its sign. |
| 115 | pub fn abs :: x: Dec -> Dec |
| 116 | { |
| 117 | if x < zero () |
| 118 | { |
| 119 | -x |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | x |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @extern c "tl_dec_add" |
| 128 | fn add :: a: Dec b: Dec -> Dec |
| 129 | |
| 130 | @extern c "tl_dec_sub" |
| 131 | fn sub :: a: Dec b: Dec -> Dec |
| 132 | |
| 133 | @extern c "tl_dec_mul" |
| 134 | fn mul :: a: Dec b: Dec -> Dec |
| 135 | |
| 136 | @extern c "tl_dec_div" |
| 137 | fn div :: a: Dec b: Dec -> Dec |
| 138 | |
| 139 | @extern c "tl_dec_rem" |
| 140 | fn rem :: a: Dec b: Dec -> Dec |
| 141 | |
| 142 | @extern c "tl_dec_eq" |
| 143 | fn equal :: a: Dec b: Dec -> Bool |
| 144 | |
| 145 | @extern c "tl_dec_compare" |
| 146 | fn order :: a: Dec b: Dec -> Order |
| 147 | |
| 148 | @extern c "tl_dec_show" |
| 149 | fn decimal :: x: Dec -> String |
| 150 | |
| 151 | @extern c "tl_hash_dec" |
| 152 | fn hash_dec :: x: Dec -> Int |
| 153 | |
| 154 | impl Num<Dec> |
| 155 | { |
| 156 | fn add :: x y |
| 157 | { |
| 158 | add x y |
| 159 | } |
| 160 | fn sub :: x y |
| 161 | { |
| 162 | sub x y |
| 163 | } |
| 164 | fn mul :: x y |
| 165 | { |
| 166 | mul x y |
| 167 | } |
| 168 | fn div :: x y |
| 169 | { |
| 170 | div x y |
| 171 | } |
| 172 | fn rem :: x y |
| 173 | { |
| 174 | // The remainder of a truncating division (exact: the scales cancel). |
| 175 | rem x y |
| 176 | } |
| 177 | fn negate :: x |
| 178 | { |
| 179 | sub (zero ()) x |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | impl Eq<Dec> |
| 184 | { |
| 185 | fn eq :: x y |
| 186 | { |
| 187 | equal x y |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | impl Ord<Dec> |
| 192 | { |
| 193 | fn compare :: x y |
| 194 | { |
| 195 | order x y |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | impl Show<Dec> |
| 200 | { |
| 201 | fn show :: x |
| 202 | { |
| 203 | decimal x |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | impl Debug<Dec> |
| 208 | { |
| 209 | fn debug :: x |
| 210 | { |
| 211 | decimal x |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | impl Hash<Dec> |
| 216 | { |
| 217 | fn hash :: x |
| 218 | { |
| 219 | hash_dec x |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | impl Default<Dec> |
| 224 | { |
| 225 | fn default :: |
| 226 | { |
| 227 | zero () |
| 228 | } |
| 229 | } |