FastDecimal.Compat (FastDecimal v1.0.1)

Copy Markdown View Source

Decimal-API-shaped facade. Drop in to migrate existing code:

alias FastDecimal.Compat, as: Decimal

Every Decimal.foo(...) call in the existing code then routes here, which delegates to FastDecimal. Inputs are auto-coerced (Decimal struct, FastDecimal struct, integer, binary, float).

Limitations

  • Struct literals don't translate under alias. %Decimal{sign: 1, coef: 123, exp: -2} resolves to the aliased module — there is no such struct here. Replace with Decimal.new(1, 123, -2) (3-arg form, shimmed).
  • Decimal.Context.* is intentionally not shimmed — FastDecimal does not carry an implicit precision context. See the FastDecimal moduledoc. Pass precision per-call via div/3's opts.
  • For Decimal.Macros.is_decimal/1, use FastDecimal.is_decimal/1 (same shape, importable as import FastDecimal, only: [is_decimal: 1]).

Summary

Types

input()

@type input() :: FastDecimal.t() | Decimal.t() | integer() | binary() | float() | nil

Functions

abs(a)

@spec abs(input()) :: FastDecimal.t()

add(a, b)

@spec add(input(), input()) :: FastDecimal.t()

cast(value)

@spec cast(input()) :: {:ok, FastDecimal.t()} | :error

Soft constructor — returns {:ok, t} | :error. Mirrors Ecto.Type.cast/1-like input handling.

cmp(a, b)

@spec cmp(input(), input()) :: :lt | :eq | :gt | :nan

compare(a, b)

@spec compare(input(), input()) :: :lt | :eq | :gt | :nan

div(a, b, opts \\ [])

@spec div(input(), input(), keyword()) :: FastDecimal.t()

Division. Defaults to precision 28 (matching Decimal's default context). Pass precision: and/or rounding: opts to override.

div_int(a, b)

@spec div_int(input(), input()) :: FastDecimal.t()

div_rem(a, b)

@spec div_rem(input(), input()) :: {FastDecimal.t(), FastDecimal.t()}

eq?(a, b)

@spec eq?(input(), input()) :: boolean()

See FastDecimal.Compat.equal?/2.

equal?(a, b)

@spec equal?(input(), input()) :: boolean()

finite?(a)

@spec finite?(input()) :: boolean()

from_float(float)

@spec from_float(float()) :: FastDecimal.t()

gt?(a, b)

@spec gt?(input(), input()) :: boolean()

inf()

@spec inf() :: FastDecimal.t()

See FastDecimal.inf/0.

inf?(a)

@spec inf?(input()) :: boolean()

integer?(d)

@spec integer?(input()) :: boolean()

lt?(a, b)

@spec lt?(input(), input()) :: boolean()

max(a, b)

@spec max(input(), input()) :: FastDecimal.t()

min(a, b)

@spec min(input(), input()) :: FastDecimal.t()

minus(a)

@spec minus(input()) :: FastDecimal.t()

mult(a, b)

@spec mult(input(), input()) :: FastDecimal.t()

multiply(a, b)

@spec multiply(input(), input()) :: FastDecimal.t()

See FastDecimal.Compat.mult/2.

nan()

@spec nan() :: FastDecimal.t()

See FastDecimal.nan/0.

nan?(a)

@spec nan?(input()) :: boolean()

neg_inf()

@spec neg_inf() :: FastDecimal.t()

See FastDecimal.neg_inf/0.

negate(a)

@spec negate(input()) :: FastDecimal.t()

See FastDecimal.Compat.minus/1.

negative?(a)

@spec negative?(input()) :: boolean()

new(input)

@spec new(input()) :: FastDecimal.t()

new(arg1, coef, exp)

@spec new(1 | -1, non_neg_integer(), integer()) :: FastDecimal.t()

Decimal-style 3-arg constructor: sign (1 or -1), coef, exp.

normalize(a)

@spec normalize(input()) :: FastDecimal.t()

plus(a)

@spec plus(input()) :: FastDecimal.t()

positive?(a)

@spec positive?(input()) :: boolean()

reduce(a)

@spec reduce(input()) :: FastDecimal.t()

See FastDecimal.Compat.normalize/1.

rem(a, b)

@spec rem(input(), input()) :: FastDecimal.t()

round(a, places \\ 0, mode \\ :half_up)

sqrt(a, opts \\ [])

@spec sqrt(
  input(),
  keyword()
) :: FastDecimal.t()

sub(a, b)

@spec sub(input(), input()) :: FastDecimal.t()

to_float(a)

@spec to_float(input()) :: float()

to_integer(a)

@spec to_integer(input()) :: integer()

to_string(a)

@spec to_string(input()) :: String.t()

to_string(a, format)

@spec to_string(input(), FastDecimal.to_string_format()) :: String.t()

zero?(a)

@spec zero?(input()) :: boolean()