DecimalEnv.Operators (DecimalEnv v1.0.1)

Copy Markdown View Source

This module redefines Elixir's numeric operators so we can use Decimal seemlessly.

Summary

Types

Supported inputs.

Functions

Arithmetic multiplication operator.

Arithmetic positive unary operator.

Arithmetic addition operator.

Arithmetic negative unary operator.

Arithmetic substraction operator.

Arithmetic division operator.

Whether a number is not equal to the other or not.

Imports the Decimal operators instead of the Kernel ones.

Whether a number is less than other or not.

Whether a number is less than or equal to the other or not.

Whether a number is equal to the other or not.

Whether a number is greater than than other or not.

Whether a number is greater than or equal to the other or not.

The absolute value of given number. Sets the number's sign to positive.

Returns the smallest number larger than or equal to number.

Divides two numbers and returns the integer part.

Returns the largest number smaller than or equal to number.

Generates infinity.

Whether the number is infinite or not.

Whether the number is an integer or not.

Compares two values numerically and returns the maximum.

Compares two values numerically and returns the minimum.

Whether the value is a number or not.

Remainder of integer division of two numbers. The result will have the sign of the first number.

Rounds the given number to specified decimal places with the given strategy (default is to round to nearest one). If places is negative, at least that many digits to the left of the decimal point will be zero.

Finds the square root of a number.

Types

input()

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

Supported inputs.

Functions

a * b

@spec input() * input() :: Decimal.t()

Arithmetic multiplication operator.

Examples

iex> use DecimalEnv.Operators
iex> "21" * "2" == Decimal.new("42")
true
iex> 21 * 2 == Decimal.new("42")
true
iex> 21.0 * 2.0 == Decimal.new("42")
true
iex> 21.0 * "2.0" == Decimal.new("42")
true

+number

@spec +input() :: Decimal.t()

Arithmetic positive unary operator.

Examples

iex> use DecimalEnv.Operators
iex> +"42" == Decimal.new("42")
true
iex> +42 == Decimal.new("42")
true
iex> +42.0 == Decimal.new("42")
true

a + b

@spec input() + input() :: Decimal.t()

Arithmetic addition operator.

Examples

iex> use DecimalEnv.Operators
iex> "21" + "21" == Decimal.new("42")
true
iex> 21 + 21 == Decimal.new("42")
true
iex> 21.0 + 21.0 == Decimal.new("42")
true
iex> 21.0 + "21.0" == Decimal.new("42")
true

-number

@spec -input() :: Decimal.t()

Arithmetic negative unary operator.

Examples

iex> use DecimalEnv.Operators
iex> -"42" == Decimal.new("-42")
true
iex> -42 == Decimal.new("-42")
true
iex> -42.0 == Decimal.new("-42")
true

a - b

@spec input() - input() :: Decimal.t()

Arithmetic substraction operator.

Examples

iex> use DecimalEnv.Operators
iex> "84" - "42" == Decimal.new("42")
true
iex> 84 - 42 == Decimal.new("42")
true
iex> 84.0 - 42.0 == Decimal.new("42")
true
iex> 84.0 - "42.0" == Decimal.new("42")
true

a / b

@spec input() / input() :: Decimal.t()

Arithmetic division operator.

Examples

iex> use DecimalEnv.Operators
iex> "84" / "2" == Decimal.new("42")
true
iex> 84 / 2 == Decimal.new("42")
true
iex> 84.0 / 2.0 == Decimal.new("42")
true
iex> 84.0 / "2.0" == Decimal.new("42")
true

a != b

@spec input() != input() :: boolean()

Whether a number is not equal to the other or not.

Examples

iex> use DecimalEnv.Operators
iex> "42" != "41"
true
iex> 42 != 41
true
iex> 42.0 != 41.0
true
iex> 42.0 != "41.0"
true
iex> 42 != 42
false

__using__(options)

(macro)

Imports the Decimal operators instead of the Kernel ones.

a < b

@spec input() < input() :: boolean()

Whether a number is less than other or not.

Examples

iex> use DecimalEnv.Operators
iex> "41" < "42"
true
iex> 41 < 42
true
iex> 41.0 < 42.0
true
iex> 41.0 < "42.0"
true
iex> 42 < 41
false

a <= b

@spec input() <= input() :: boolean()

Whether a number is less than or equal to the other or not.

Examples

iex> use DecimalEnv.Operators
iex> "41" <= "42"
true
iex> 41 <= 42
true
iex> 41.0 <= 42.0
true
iex> 41.0 <= "42.0"
true
iex> 42 <= 42
true
iex> 42 <= 41
false

a == b

@spec input() == input() :: boolean()

Whether a number is equal to the other or not.

Examples

iex> use DecimalEnv.Operators
iex> "42" == "42"
true
iex> 42 == 42
true
iex> 42.0 == 42.0
true
iex> 42.0 == "42.0"
true
iex> 42 == 41
false

a > b

@spec input() > input() :: boolean()

Whether a number is greater than than other or not.

Examples

iex> use DecimalEnv.Operators
iex> "42" > "41"
true
iex> 42 > 41
true
iex> 42.0 > 41.0
true
iex> 42.0 > "41.0"
true
iex> 41 > 42
false

a >= b

@spec input() >= input() :: boolean()

Whether a number is greater than or equal to the other or not.

Examples

iex> use DecimalEnv.Operators
iex> "42" >= "41"
true
iex> 42 >= 41
true
iex> 42.0 >= 41.0
true
iex> 42.0 >= "41.0"
true
iex> 42 >= 42
true
iex> 41 >= 42
false

abs(number)

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

The absolute value of given number. Sets the number's sign to positive.

Examples

iex> use DecimalEnv.Operators
iex> abs("42") == Decimal.new("42")
true
iex> abs("-42") == Decimal.new("42")
true
iex> abs(42) == Decimal.new("42")
true
iex> abs(-42) == Decimal.new("42")
true
iex> abs(42.0) == Decimal.new("42")
true
iex> abs(-42.0) == Decimal.new("42")
true

ceil(number)

@spec ceil(input()) :: Decimal.t()

Returns the smallest number larger than or equal to number.

Examples

iex> use DecimalEnv.Operators
iex> ceil(42) == Decimal.new("42")
true
iex> ceil("41.1") == Decimal.new("42")
true
iex> ceil(41.1) == Decimal.new("42")
true

div(a, b)

@spec div(input(), input()) :: Decimal.t()

Divides two numbers and returns the integer part.

Examples

iex> use DecimalEnv.Operators
iex> div(5, 2) == Decimal.new("2")
true
iex> div("5", "2") == Decimal.new("2")
true
iex> div(5.0, 2.0) == Decimal.new("2")
true
iex> div(5.0, "2.0") == Decimal.new("2")
true

floor(number)

@spec floor(input()) :: Decimal.t()

Returns the largest number smaller than or equal to number.

Examples

iex> use DecimalEnv.Operators
iex> floor(42) == Decimal.new("42")
true
iex> floor("42.9") == Decimal.new("42")
true
iex> floor(42.9) == Decimal.new("42")
true

inf()

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

Generates infinity.

Examples

iex> use DecimalEnv.Operators
iex> inf() == Decimal.new("+Inf")
true
iex> -inf() == Decimal.new("-Inf")
true

inf?(number)

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

Whether the number is infinite or not.

Examples

iex> use DecimalEnv.Operators
iex> inf?(inf())
true
iex> inf?(-inf())
true

integer?(number)

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

Whether the number is an integer or not.

Examples

iex> use DecimalEnv.Operators
iex> integer?(42)
true
iex> integer?("42")
true
iex> integer?(42.0)
true
iex> integer?(42.5)
false
iex> integer?("42.5")
false

max(a, b)

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

Compares two values numerically and returns the maximum.

Examples

iex> use DecimalEnv.Operators
iex> max("42", "41") == Decimal.new("42")
true
iex> max(42, 41) == Decimal.new("42")
true
iex> max(42.0, 41.0) == Decimal.new("42")
true
iex> max(42.0, "41.0") == Decimal.new("42")
true
iex> max(41.0, "42.0") == Decimal.new("42")
true

min(a, b)

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

Compares two values numerically and returns the minimum.

Examples

iex> use DecimalEnv.Operators
iex> min("43", "42") == Decimal.new("42")
true
iex> min(43, 42) == Decimal.new("42")
true
iex> min(43.0, 42.0) == Decimal.new("42")
true
iex> min(43.0, "42.0") == Decimal.new("42")
true
iex> min(42.0, "43.0") == Decimal.new("42")
true

number?(number)

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

Whether the value is a number or not.

Examples

iex> use DecimalEnv.Operators
iex> number?(42)
true
iex> number?("42.0")
true
iex> number?(42.0)
true
iex> number?("+Inf")
true
iex> number?("-Inf")
true
iex> number?(%Decimal{coef: :NaN})
false

rem(a, b)

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

Remainder of integer division of two numbers. The result will have the sign of the first number.

Examples

iex> use DecimalEnv.Operators
iex> rem(5, 2) == Decimal.new("1")
true
iex> rem("5", "2") == Decimal.new("1")
true
iex> rem("5.0", "2.0") == Decimal.new("1")
true
iex> rem(5.0, "2.0") == Decimal.new("1")
true

round(number, places \\ 0, strategy \\ :half_up)

@spec round(input(), non_neg_integer(), Decimal.rounding()) :: Decimal.t()

Rounds the given number to specified decimal places with the given strategy (default is to round to nearest one). If places is negative, at least that many digits to the left of the decimal point will be zero.

The available strategies are:

  • :down
  • :half_up
  • :half_even
  • :ceiling
  • :floor
  • :half_down
  • :up

Examples

iex> use DecimalEnv.Operators
iex> round(42) == Decimal.new("42")
true
iex> round(41.5) == Decimal.new("42")
true
iex> round("41.5") == Decimal.new("42")
true
iex> round("42.4") == Decimal.new("42")
true

sqrt(number)

@spec sqrt(input()) :: Decimal.t()

Finds the square root of a number.

Examples

iex> use DecimalEnv.Operators
iex> sqrt(4) == Decimal.new("2")
true
iex> sqrt("4") == Decimal.new("2")
true
iex> sqrt(4.0) == Decimal.new("2")
true