4 Decimal Numbers
(require scramble/decimal) | package: scramble-lib |
Added in version 0.4 of package scramble-lib.
struct
unscaled : exact-integer? scale : exact-integer?
The scale value is also called precision throughout this document. When non-negative, it corresponds to the number of places after the decimal point. When negative, it indicates the number of places before the decimal point that are insignificant.
procedure
(real->decimal x precision) → decimal?
x : rational? precision : exact-integer?
> (real->decimal 1/8 4) #<decimal:0.1250>
> (real->decimal 1/3 3) #<decimal:0.333>
procedure
(string->decimal s) → (or/c decimal? #f)
s : string?
> (string->decimal "123.45") #<decimal:123.45>
> (string->decimal ".98") #<decimal:0.98>
> (string->decimal "-22.44") #<decimal:-22.44>
> (string->decimal "123##") #<decimal:123##>
> (string->decimal "-.") #f
procedure
(decimal->string dec) → string?
dec : decimal?
> (decimal->string (decimal 12345 2)) "123.45"
> (decimal->string (decimal 25 -3)) "25###"
procedure
(decimal->exact dec) → (and/c rational? exact?)
dec : (or/c decimal? rational?)
procedure
(decimal->inexact dec) → (and/c real? inexact?)
dec : (or/c decimal? real?)
> (decimal->exact (string->decimal "123.45")) 2469/20
> (decimal->exact (string->decimal "987")) 987
> (decimal->exact 1/4) 1/4
> (decimal->inexact (string->decimal "123.45")) 123.45
> (decimal->inexact +inf.0) +inf.0
procedure
(decimal-adjust dec precision) → decimal?
dec : decimal? precision : exact-integer?
> (decimal-adjust (string->decimal "123.456") 5) #<decimal:123.45600>
> (decimal-adjust (string->decimal "123.456") 1) #<decimal:123.5>
> (decimal-adjust (string->decimal "12.5") 0) #<decimal:12>
> (decimal-adjust (string->decimal "13.5") 0) #<decimal:14>
> (decimal-adjust (string->decimal "123456") -2) #<decimal:1235##>
procedure
v : (or/c decimal? real?)
procedure
v : (or/c decimal? real?)
procedure
v : (or/c decimal? real?)
The precision of an operation involving two decimals is the max of the operand precisions for addition and subtraction, and the sum of the precisions for multiplications. The precision of an operation involving a decimal and an ordinary real number is the precision of the decimal. Operations are performed left to right.
Warning: Arithmetic on mixtures of reals and decimals is neither associative nor generally commutative, due to the precision rules above. For example:
> (decimal+ (string->decimal "1.00") pi (string->decimal "0.0001")) #<decimal:4.1401>
> (decimal+ (string->decimal "0.0001") pi (string->decimal "1.00")) #<decimal:4.1417>
procedure
(decimal-cmp v1 v2) → (or/c '< '= '> #f)
v1 : (or/c decimal? real?) v2 : (or/c decimal? real?)
> (decimal-cmp (string->decimal "2.0") (string->decimal "2.000")) '=
> (decimal-cmp (string->decimal "1.000") 1) '=
> (decimal-cmp (string->decimal "123.45") 100) '>
> (decimal-cmp (string->decimal "999") +inf.0) '<
procedure
v : (or/c decimal? real?)
procedure
(decimal<=? v ...+) → boolean?
v : (or/c decimal? real?)
procedure
v : (or/c decimal? real?)
procedure
v : (or/c decimal? real?)
procedure
(decimal>=? v ...+) → boolean?
v : (or/c decimal? real?)
procedure
(decimal-min v ...+) → (or/c decimal? real?)
v : (or/c decimal? real?)
procedure
(decimal-max v ...+) → (or/c decimal? real?)
v : (or/c decimal? real?)
procedure
(decimal-abs dec) → decimal?
dec : decimal?