Decimal Api Documentation

Version: 1.0.2

decimal.core

<

(< v x)(< v x & more)

Returns true if the value of this Decimal is less than the value of x, otherwise returns false.

<=

(<= v x)(<= v x & more)

Returns true if the value of this Decimal is less than or equal to the value of x, otherwise returns false.

=

(= v x)(= v x & more)

Returns true if the value of this Decimal is equal to the value of x, otherwise returns false.

>

(> v x)(> v x & more)

Returns true if the value of this Decimal is greater than the value of x, otherwise returns false.

>=

(>= v x)(>= v x & more)

Returns true if the value of this Decimal is greater than or equal to the value of x, otherwise returns false.

abs

(abs v)

Returns a new Decimal whose value is the absolute value, i.e. the magnitude, of the value of this Decimal.

acos

(acos v)

Returns a new Decimal whose value is the inverse cosine in radians of the value of this Decimal.

acosh

(acosh v)

Returns a new Decimal whose value is the inverse hyperbolic cosine in radians of the value of this Decimal.

asin

(asin v)

Returns a new Decimal whose value is the inverse sine in radians of the value of this Decimal.

asinh

(asinh v)

Returns a new Decimal whose value is the inverse hyperbolic sine in radians of the value of this Decimal.

atan

(atan v)

Returns a new Decimal whose value is the inverse tangent in radians of the value of this Decimal.

atanh

(atanh v)

Returns a new Decimal whose value is the inverse hyperbolic tangent in radians of the value of this Decimal.

cbrt

(cbrt v)

Returns a new Decimal whose value is the cube root of this Decimal.

ceil

(ceil v)

Returns a new Decimal whose value is the value of this Decimal rounded to a whole number in the direction of positive Infinity.

cmp

(cmp v x)

Returns 1 if the value of this Decimal is greater than the value of x, -1 if the value of this Decimal is less than the value of x, 0 if the value of Decimal is equal to the value of x and NaN if the value of this Decimal or the value of x is NaN

config

(config options)

The same as config but returns an constructor of decimals that can be used for create new instances with provided configuration.

config!

(config! options)

Set the global configuration for the decimal constructor.

Possible options:

  • precision: The maximum number of significant digits of the result of an operation (integer 1 to 1e+9 inclusive, default: 20).
  • rounding: The default rounding mode used when rounding the result of an operation (integer 0 to 8 inclusive, default: :round-half-up).
  • min-e: The negative exponent limit, i.e. the exponent value below which underflow to zero occurs (integer, -9e15 to 0 inclusive, default: -9e15).
  • max-e: The positive exponent limit, i.e. the exponent value above which overflow to Infinity occurs (integer, 0 to 9e15 inclusive, default: 9e15).
  • to-exp-neg: The negative exponent value at and below which toString returns exponential notation. (integer, -9e15 to 0 inclusive, default: -7)
  • to-exp-pos: The positive exponent value at and above which toString returns exponential notation. (integer, 0 to 9e15 inclusive, default: 20)
  • modulo: The modulo mode used when calculating the modulus: a mod n. (integer, 0 to 9 inclusive, default: :round-down)
  • crypto: The value that determines whether cryptographically-secure pseudo-random number generation is used. (boolean, default: false)

Rounding modes

Rounding modes are:

Keyword Description
:round-up Rounds away from zero
:round-down Rounds towards zero
:round-ceil Rounds towards Infinity
:round-floor Rounds towards -Infinity
:round-half-up Rounds towards nearest neighbour. If equidistant, rounds away from zero
:round-half-down Rounds towards nearest neighbour. If equidistant, rounds towards zero
:round-half-even Rounds towards nearest neighbour. If equidistant, rounds towards even neighbour
:round-half-ceil Rounds towards nearest neighbour. If equidistant, rounds towards Infinity
:round-half-floor Rounds towards nearest neighbour. If equidistant, rounds towards -Infinity
:euclid Not a rounding mode, see modulo

Modulo modes

The modes that are most commonly used for the modulus/remainder operation are shown in the following table. Although the other rounding modes can be used, they may not give useful results.

Keyword Description
:round-up The remainder is positive if the dividend is negative, else is negative
:round-down The remainder has the same sign as the dividend. This uses truncating division and matches the behaviour of JavaScript’s remainder operator %.
:round-floor The remainder has the same sign as the divisor. (This matches Python’s % operator)
:round-half-even The IEEE 754 remainder function
:euclid The remainder is always positive.

Other options

The underlying library supports more options that and this function also accepts. You can read more about here: http://mikemcl.github.io/decimal.js/#Dconfig

cos

(cos v)

Returns a new Decimal whose value is the cosine of the value in radians of this Decimal.

cosh

(cosh v)

Returns a new Decimal whose value is the hyperbolic cosine of the value in radians of this Decimal.

decimal

(decimal v)

Create a new Decimal instance from v value.

decimal-places

(decimal-places v)

Returns the number of decimal places, i.e. the number of digits after the decimal point, of the value of this Decimal.

decimal?

(decimal? v)

Return true if v is a instance of Decimal.

div

(div v x)

Returns a new Decimal whose value is the value of this Decimal divided by x, rounded to significant digits.

div'

(div' v x)

Return a new Decimal whose value is the integer part of dividing this Decimal by x, rounded to significant digits.

exp

(exp v)

Returns a new Decimal whose value is the base e (Euler’s number, the base of the natural logarithm) exponential of the value of this Decimal.

The ln is the invese of this function.

finite?

(finite? v)

Returns true if the value of this Decimal is a finite number, otherwise returns false. The only possible non-finite values of a Decimal are NaN, Infinity and -Infinity.

floor

(floor v)

Returns a new Decimal whose value is the value of this Decimal rounded to a whole number in the direction of negative Infinity.

hypot

(hypot & params)

Returns a new Decimal whose value is the square root of the sum of the squares of the arguments.

IDecimal

protocol

members

-decimal

(-decimal v)

return a decimal instance.

integer?

(integer? v)

Returns true if the value of this Decimal is a whole number, otherwise returns false.

ln

(ln v)

Returns a new Decimal whose value is the natural logarithm of the value of this Decimal.

log

(log v)(log v x)

Returns a new Decimal whose value is the base x logarithm of the value of this Decimal. If x is omitted, the base 10 logarithm of the value of this Decimal will be returned.

log10

(log10 x)

Returns a new Decimal whose value is the base 10 logarithm of x.

log2

(log2 x)

Returns a new Decimal whose value is the base 2 logarithm of x.

max

(max a)(max a b)(max a b & more)

Returns a new Decimal whose value is the maximum.

min

(min a)(min a b)(min a b & more)

Returns a new Decimal whose value is the minimum.

minus

(minus v x)

Returns a new Decimal whose value is the value of this Decimal minus x, rounded to significant digits.

mod

(mod v x)

Returns a new Decimal whose value is the value of this Decimal modulo x.

The value returned, and in particular its sign, is dependent on the value of the modulo property of this Decimal’s constructor. If it is 1 (default value), the result will have the same sign as this Decimal, and it will match that of Javascript’s % operator (within the limits of double precision) and BigDecimal’s remainder method.

See config! function docstrings for a description and available options for modulo.

mul

(mul v x)

Returns a new Decimal whose value is the value of this Decimal times x, rounded to significant digits using.

NaN?

(NaN? v)

Returns true if the value of this Decimal is NaN, otherwise returns false.

neg

(neg v)

Returns a new Decimal whose value is the value of this Decimal negated.

neg?

(neg? v)

Returns true if the value of this Decimal is negative, otherwise returns false.

plus

(plus v x)

Returns a new Decimal whose value is the value of this Decimal plus x, rounded to significant digits.

pos?

(pos? v)

Returns true if the value of this Decimal is negative, otherwise returns false.

pow

(pow v x)

Returns a new Decimal whose value is the value of this Decimal raised to the power x, rounded to precision significant digits using rounding mode rounding.

The performance of this method degrades exponentially with increasing digits. For non-integer exponents in particular, the performance of this method may not be adequate.

precision

(precision v)(precision v include-zeros)

Returns the number of significant digits of the value of this Decimal.

If include-zeros is true, then the trailing zeros of the integer part will be included in the counter of significant digits.

random

(random dp)

Returns a new Decimal with a pseudo-random value equal to or greater than 0 and less than 1. The return value will have dp decimal places (or less if trailing zeros are produced). If dp is omitted then the default will be used.

Detailed doc: http://mikemcl.github.io/decimal.js/#Drandom

round

(round v)

Returns a new Decimal whose value is the value of this Decimal rounded to a whole number. To emulate Math.round, set rounding to 7 (or :round/hanf-ceil).

sign

(sign v)

Returns the sign of the value of this Decimal.

sin

(sin v)

Returns a new Decimal whose value is the sine of the value in radians of this Decimal.

sinh

(sinh v)

Returns a new Decimal whose value is the hyperbolic sine of the value in radians of this Decimal.

sqrt

(sqrt v)

Returns a new Decimal whose value is the square root of this Decimal.

tan

(tan v)

Returns a new Decimal whose value is the tangent of the value in radians of this Decimal.

tanh

(tanh v)

Returns a new Decimal whose value is the hyperbolic tangent of the value in radians of this Decimal.

to-binary

(to-binary v)(to-binary v sd)(to-binary v sd rm)

Returns a string representing the value of this Decimal in binary format.

If sd is specified, the return value will use binary exponential notation. If sd is omitted, the return value will be rounded to default significant digits. If rm is omitted, default rounding mode will be used.

to-decimal-places

(to-decimal-places v)(to-decimal-places v dp)(to-decimal-places v dp rm)

Returns a new Decimal whose value is the value of this Decimal rounded to dp decimal places using rounding mode rm.

If dp is omitted, the return value will have the same value as this Decimal.

If rm is omitted, rounding mode rounding will be used.

Throws on an invalid dp or rm value.

to-exponential

(to-exponential v)(to-exponential v dp)(to-exponential v dp rm)

Returns a string representing the value of this Decimal in exponential notation rounded using rounding mode rm to dp decimal places.

If the value of this Decimal in exponential notation has fewer than dp fraction digits, the return value will be appended with zeros accordingly.

If dp is omitted, the number of digits after the decimal point defaults to the minimum number of digits necessary to represent the value exactly.

If rm is omitted, rounding mode rounding is used.

Throws on an invalid dp or rm value.

to-fixed

(to-fixed v)(to-fixed v dp)(to-fixed v dp rm)

Returns a string representing the value of this Decimal in normal (fixed-point) notation rounded to dp decimal places using rounding mode rm.

If the value of this Decimal in normal notation has fewer than dp fraction digits, the return value will be appended with zeros accordingly.

Unlike Number/toFixed, which returns exponential notation if a number is greater or equal to 10^21, this method will always return normal notation.

If dp is omitted, the return value will be unrounded and in normal notation. This is unlike Number/toFixed, which returns the value to zero decimal places, but is useful when because of the current toExpNeg or toExpNeg values, toString returns exponential notation.

If rm is omitted, default rounding mode is used.

Throws on an invalid dp or rm value.

to-fraction

(to-fraction v)(to-fraction v max-denominator)

Returns an array of two Decimals representing the value of this Decimal as a simple fraction with an integer numerator and an integer denominator. The denominator will be a positive non-zero value less than or equal to max_denominator.

If a maximum denominator is omitted, the denominator will be the lowest value necessary to represent the number exactly.

Throws on an invalid max_denominator value.

to-hex

(to-hex v)(to-hex v sd)(to-hex v sd rm)

Returns a string representing the value of this Decimal in hexadecimal notation rounded to sd significant digits using rounding mode rm.

If sd is defined, the return value will use binary exponential notation.

If sd is omitted, the return value will be rounded to precision significant digits.

If rm is omitted, rounding mode rounding will be used.

Throws on an invalid sd or rm value.

to-nearest

(to-nearest v n)(to-nearest v n rm)

Returns a new Decimal whose value is the nearest multiple of x to the value of this Decimal.

If the value of this Decimal is equidistant from two multiples of x, the rounding mode rm, or rounding if rm is omitted, determines the direction of the nearest.

In this context, rounding mode :round-half-up is interpreted the same as rounding mode :round-up, and so on, i.e. the rounding is either up, down, to ceil, to floor or to even.

The return value will always have the same sign as this Decimal, unless either this Decimal or x is NaN, in which case the return value will be also be NaN.

The return value is not affected by the value of the precision setting.

to-number

(to-number v)

Returns the value of this Decimal converted to a primitive number.

Type coercion with, for example, JavaScript’s unary plus operator will also work, except that a Decimal with the value minus zero will convert to positive zero.

to-octal

(to-octal v)(to-octal v sd)(to-octal v sd rm)

Returns a string representing the value of this Decimal in octal notation rounded to sd significant digits using rounding mode rm.

If sd is defined, the return value will use binary exponential notation.

If sd is omitted, the return value will be rounded to precision significant digits.

If rm is omitted, rounding mode rounding will be used.

Throws on an invalid sd or rm value.

to-precision

(to-precision v)(to-precision v sd)(to-precision v sd rm)

Returns a string representing the value of this Decimal in rounded to sd significant digits using rounding mode rm.

If sd is less than the number of digits necessary to represent the integer part of the value in normal (fixed-point) notation, then exponential notation is used.

If sd is omitted, the return value is the same as to-string.

If rm is omitted, rounding mode rounding will be used.

Throws on an invalid sd or rm value.

to-significant-digits

(to-significant-digits v)(to-significant-digits v sd)(to-significant-digits v sd rm)

Returns a new Decimal whose value is the value of this Decimal rounded to sd significant digits using rounding mode rm.

If sd is omitted, the return value will be rounded to precision significant digits.

If rm is omitted, rounding mode rounding will be used.

Throws on an invalid sd or rm value.

to-string

(to-string v)

Returns a string representing the value of this Decimal.

If this Decimal has a positive exponent that is equal to or greater than to-exp-pos, or a negative exponent equal to or less than to-exp-neg, then exponential notation will be returned.

truncate

(truncate v)

Returns a new Decimal whose value is the value of this Decimal truncated to a whole number.

value-of

(value-of v)

As toString, but zero is signed.

with-config

macro

(with-config config & body)

zero?

(zero? v)

Returns true if the value of this Decimal is zero or minus zero, otherwise returns false.