x-lang

← Index

x/num/decimal

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.

(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.

Exact integer helpers

Construction

Rounding

Arithmetic

Comparison

Roots and powers

Logarithms and the exponential

Conversion

Predicates

Type registration

Operator Overrides

number?

Test whether a value is any numeric type (now including decimals).

Parameters:

Returns: BOOL — True if x is a number

real?

Test whether a value is a real number (now including decimals).

Parameters:

Returns: BOOL — True if x is a real number

Class Decimal

(Decimal decimal? x)

Test whether a value is an arbitrary-precision decimal.

Parameters:

Returns: BOOL — True if x is a decimal

(Decimal precision)

The significant digits division and sqrt round to.

Returns: INT — Current precision

(Decimal precision! n)

Set the significant digits division and sqrt round to. Addition, subtraction and multiplication stay exact and are unaffected.

Parameters:

Returns: INT — The new precision

(Decimal from x)

Construct a decimal from any convertible value, through the conversion catalog. A float converts EXACTLY; a rational rounds to the current precision. Raises tag ‘type when nothing converts.

Parameters:

Returns: DECIMAL — Decimal instance

(Decimal make sig exp)

Construct the decimal sig * 10^exp directly, without going through text.

Parameters:

Returns: DECIMAL — Decimal instance

(Decimal significand x)

The decimal’s significand, with trailing zeros already stripped.

Parameters:

Returns: INT — Significand as an exact integer

(Decimal exponent x)

The decimal’s power-of-ten exponent.

Parameters:

Returns: INT — Exponent

(Decimal ->int x)

Convert a decimal to an exact integer by truncation toward zero.

Parameters:

Returns: INT — Truncated integer value

(Decimal ->str x)

The decimal’s text, without the d suffix that write adds for round-tripping.

Parameters:

Returns: STRING — Decimal text

(Decimal ->float x)

Convert a decimal to the nearest IEEE 754 double. Lossy by definition; the rounding is strtod’s.

Parameters:

Returns: FLOAT — Nearest double

(Decimal + a b)

Add two decimals, exactly (other numerics coerce).

Parameters:

Returns: DECIMAL — Sum

(Decimal - a b)

Subtract two decimals, exactly (other numerics coerce).

Parameters:

Returns: DECIMAL — Difference

(Decimal * a b)

Multiply two decimals, exactly (other numerics coerce).

Parameters:

Returns: DECIMAL — Product

(Decimal / a b)

Divide two decimals, rounded half-even to the current precision (other numerics coerce).

Parameters:

Returns: DECIMAL — Quotient

(Decimal % a b)

Truncating remainder of decimal division, exactly (other numerics coerce).

Parameters:

Returns: DECIMAL — Remainder, with the dividend’s sign

(Decimal < a b)

Test whether a is less than b (other numerics coerce).

Parameters:

Returns: BOOL — True if a < b

(Decimal = a b)

Test whether a equals b (other numerics coerce).

Parameters:

Returns: BOOL — True if a equals b

(Decimal compare a b)

Three-way comparison of two decimals (other numerics coerce).

Parameters:

Returns: INT — -1 if a < b, 0 if equal, 1 if a > b

(Decimal neg x)

Negate a decimal.

Parameters:

Returns: DECIMAL — The negated value

(Decimal abs x)

The absolute value of a decimal.

Parameters:

Returns: DECIMAL — Magnitude

(Decimal zero? x)

Test whether a decimal is zero.

Parameters:

Returns: BOOL — True if x is zero

(Decimal round x places)

Round a decimal to a number of decimal places, half-even.

Parameters:

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:

Returns: DECIMAL — Value at the requested exponent

(Decimal trunc x)

Truncate a decimal toward zero, as an integral decimal.

Parameters:

Returns: DECIMAL — Integer part

(Decimal floor x)

The largest integral decimal not greater than x.

Parameters:

Returns: DECIMAL — Floor of x

(Decimal ceil x)

The smallest integral decimal not less than x.

Parameters:

Returns: DECIMAL — Ceiling of x

(Decimal sqrt x)

The square root of a decimal, rounded half-even to the current precision.

Parameters:

Returns: DECIMAL — Square root of x

(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:

Returns: DECIMAL — x raised to the power n

(Decimal exp x)

Raise e to a power, to the current precision.

Parameters:

Returns: DECIMAL — e raised to the power x

(Decimal ln x)

The natural logarithm of a decimal, to the current precision. Raises tag ‘value for zero or a negative.

Parameters:

Returns: 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 tag ‘value for zero or a negative.

Parameters:

Returns: DECIMAL — log10(x)