Arbitrary-precision decimal floating-point, homed on the Decimal class.
Literal syntax: 1.5d, -0.001d, 3d, 1.5e-8d. The generic operators
dispatch decimal operands through the type ops; mixed operands
resolve by the from-relation, and a double widens EXACTLY.
- % are exact; / sqrt ln exp log10 round half-even to
(Decimal precision). The transcendentals are series, not libm –
correct to the last digit; the series run in fixed point, so a
34-digit ln costs about a tenth of a second, not ten.
dec-adddec-subdec-muldec-divdec-moddec-ltdec-eqensure-decdec-exp-digitsdec-exp-signdec-fracdec-first-fracdec-intdec-signdecimal-typenumber?Test whether a value is any numeric type (now including decimals).
Parameters:
ANY — Value to testReturns: BOOL — True if x is a number
Examples:
(number? 1.5d) => #treal?Test whether a value is a real number (now including decimals).
Parameters:
ANY — Value to testReturns: BOOL — True if x is a real number
Examples:
(real? 1.5d) => #tDecimal(Decimal type)The decimal type handle, for Convert and Type.
Returns: ATOM — The DECIMAL type handle
(Decimal decimal? x)Test whether a value is an arbitrary-precision decimal.
Parameters:
ANY — Value to testReturns: BOOL — True if x is a decimal
Examples:
(list (Decimal decimal? 1.5d) (Decimal decimal? 1.5)) => (#t #f)(Decimal precision)The significant digits division and sqrt round to.
Returns: INTEGER — Current precision
(Decimal precision! n)Set the significant digits division and sqrt round to. Addition, subtraction and multiplication stay exact and are unaffected.
Parameters:
INTEGER — Significant digits, at least 1Returns: INTEGER — The new precision
(Decimal from x)Construct a decimal from any convertible value, through the conversion catalogue. A float converts EXACTLY; a rational rounds to the current precision. Raises a label ‘type when nothing converts.
Parameters:
ANY — An int, bigint, float, rational, numeric string, or decimal (identity)Returns: DECIMAL — Decimal instance
(Decimal make sig exp)Construct the decimal sig * 10^exp directly, without going through text.
Parameters:
INTEGER — Significand (int or bigint)INTEGER — Power-of-ten exponentReturns: DECIMAL — Decimal instance
(Decimal significand x)The decimal’s significand, with trailing zeros already stripped.
Parameters:
DECIMAL — Decimal valueReturns: INTEGER — Significand as an exact integer
Examples:
(Decimal significand 1.250d) => 125(Decimal exponent x)The decimal’s power-of-ten exponent.
Parameters:
DECIMAL — Decimal valueReturns: INTEGER — Exponent
Examples:
(Decimal exponent 1.25d) => -2(Decimal ->int x)Convert a decimal to an exact integer by truncation toward zero.
Parameters:
DECIMAL — Decimal valueReturns: INTEGER — Truncated integer value
Examples:
(Decimal ->int -2.75d) => -2(Decimal ->str x)The decimal’s text, without the d suffix that write adds for round-tripping.
Parameters:
DECIMAL — Decimal valueReturns: STRING — Decimal text
Examples:
(Decimal ->str 1.5d) => "1.5"(Decimal ->float x)Convert a decimal to the nearest IEEE 754 double. Lossy by definition; the rounding is strtod’s.
Parameters:
DECIMAL — Decimal valueReturns: FLOAT — Nearest double
Examples:
(Decimal ->float 0.5d) => 0.5(Decimal + a b)Add two decimals, exactly (other numerics promote).
Parameters:
NUMBER — First operandNUMBER — Second operandReturns: DECIMAL — Sum
Examples:
(Decimal + 0.1d 0.2d) => 0.3d(Decimal - a b)Subtract two decimals, exactly (other numerics promote).
Parameters:
NUMBER — First operandNUMBER — Second operandReturns: DECIMAL — Difference
Examples:
(Decimal - 1 0.25d) => 0.75d(Decimal * a b)Multiply two decimals, exactly (other numerics promote).
Parameters:
NUMBER — First operandNUMBER — Second operandReturns: DECIMAL — Product
Examples:
(Decimal * 1.5d 1.5d) => 2.25d(Decimal / a b)Divide two decimals, rounded half-even to the current precision (other numerics promote).
Parameters:
NUMBER — DividendNUMBER — DivisorReturns: DECIMAL — Quotient
Examples:
(Decimal / 1 4) => 0.25d(Decimal % a b)Truncating remainder of decimal division, exactly (other numerics promote).
Parameters:
NUMBER — DividendNUMBER — DivisorReturns: DECIMAL — Remainder, with the dividend’s sign
Examples:
(Decimal % 7.5d 2) => 1.5d(Decimal < a b)Test whether a is less than b (other numerics promote).
Parameters:
NUMBER — Left operandNUMBER — Right operandReturns: BOOL — True if a < b
Examples:
(Decimal < 0.1d 0.2d) => #t(Decimal = a b)Test whether a equals b (other numerics promote).
Parameters:
NUMBER — Left operandNUMBER — Right operandReturns: BOOL — True if a equals b
Examples:
(Decimal = 0.5d 0.50d) => #t(Decimal compare a b)Three-way comparison of two decimals (other numerics promote).
Parameters:
NUMBER — Left operandNUMBER — Right operandReturns: INTEGER — -1 if a < b, 0 if equal, 1 if a > b
Examples:
(Decimal compare 1.5d 2) => -1(Decimal neg x)Negate a decimal.
Parameters:
NUMBER — Decimal valueReturns: DECIMAL — The negated value
Examples:
(Decimal neg 1.5d) => -1.5d(Decimal abs x)The absolute value of a decimal.
Parameters:
NUMBER — Decimal valueReturns: DECIMAL — Magnitude
Examples:
(Decimal abs -1.5d) => 1.5d(Decimal zero? x)Test whether a decimal is zero.
Parameters:
NUMBER — Decimal valueReturns: BOOL — True if x is zero
Examples:
(list (Decimal zero? 0.0d) (Decimal zero? 1d)) => (#t #f)(Decimal round x places)Round a decimal to a number of decimal places, half-even.
Parameters:
NUMBER — Decimal valueINTEGER — Decimal places to keep; negative rounds to tens, hundreds, …Returns: DECIMAL — Rounded value
(Decimal rescale x exp)Restate a decimal at a given exponent, rounding half-even when that drops digits. The exponent-facing form of round, for a caller that thinks in scales.
Parameters:
NUMBER — Decimal valueINTEGER — Target power-of-ten exponentReturns: DECIMAL — Value at the requested exponent
Examples:
(Decimal rescale 1.234d -2) => 1.23d(Decimal trunc x)Truncate a decimal toward zero, as an integral decimal.
Parameters:
NUMBER — Decimal valueReturns: DECIMAL — Integer part
Examples:
(Decimal trunc -2.7d) => -2d(Decimal floor x)The largest integral decimal not greater than x.
Parameters:
NUMBER — Decimal valueReturns: DECIMAL — Floor of x
Examples:
(Decimal floor -2.5d) => -3d(Decimal ceil x)The smallest integral decimal not less than x.
Parameters:
NUMBER — Decimal valueReturns: DECIMAL — Ceiling of x
Examples:
(Decimal ceil 2.1d) => 3d(Decimal sqrt x)The square root of a decimal, rounded half-even to the current precision.
Parameters:
NUMBER — Non-negative decimalReturns: DECIMAL — Square root of x
Examples:
(Decimal sqrt 2.25d) => 1.5d(Decimal pow x n)Raise a decimal to an integer power. A non-negative exponent is exact; a negative one divides once, at the current precision.
Parameters:
NUMBER — BaseINTEGER — Integer exponentReturns: DECIMAL — x raised to the power n
Examples:
(Decimal pow 1.5d 2) => 2.25d(Decimal exp x)Raise e to a power, to the current precision.
Parameters:
NUMBER — ExponentReturns: DECIMAL — e raised to the power x
(Decimal ln x)The natural logarithm of a decimal, to the current precision. Raises a label ‘value for zero or a negative.
Parameters:
NUMBER — Positive decimalReturns: DECIMAL — Natural logarithm of x
(Decimal log10 x)The base-10 logarithm of a decimal, to the current precision. An exact power of ten answers its exponent exactly. Raises a label ‘value for zero or a negative.
Parameters:
NUMBER — Positive decimalReturns: DECIMAL — log10(x)