FastDecimal.Compat (FastDecimal v1.0.0)

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)

add(a, b)

cast(value)

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

cmp(a, b)

compare(a, b)

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

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

div_int(a, b)

div_rem(a, b)

eq?(a, b)

See FastDecimal.Compat.equal?/2.

equal?(a, b)

finite?(a)

from_float(float)

gt?(a, b)

inf()

See FastDecimal.inf/0.

inf?(a)

integer?(d)

lt?(a, b)

max(a, b)

min(a, b)

minus(a)

mult(a, b)

multiply(a, b)

See FastDecimal.Compat.mult/2.

nan()

See FastDecimal.nan/0.

nan?(a)

neg_inf()

See FastDecimal.neg_inf/0.

negate(a)

See FastDecimal.Compat.minus/1.

negative?(a)

new(input)

new(arg1, coef, exp)

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

normalize(a)

plus(a)

positive?(a)

reduce(a)

See FastDecimal.Compat.normalize/1.

rem(a, b)

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

sqrt(a, opts \\ [])

sub(a, b)

to_float(a)

to_integer(a)

to_string(a)

to_string(a, format)

zero?(a)