DecimalEnv.Operators (DecimalEnv v1.0.0) View Source

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

Link to this section Summary

Types

Supported inputs.

Functions

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

Arithmetic multiplication operator.

Arithmetic positive unary operator.

Arithmetic addition operator.

Arithmetic negative unary operator.

Arithmetic substraction operator.

Arithmetic division operator.

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.

Imports the Decimal operators instead of the Kernel ones.

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.

Link to this section Types

Specs

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

Supported inputs.

Link to this section Functions

Specs

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

Specs

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

Specs

+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

Specs

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

Specs

-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

Specs

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

Specs

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

Specs

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

Specs

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

Specs

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

Specs

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

Specs

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
Link to this macro

__using__(options)

View Source (macro)

Imports the Decimal operators instead of the Kernel ones.

Specs

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

Specs

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

Specs

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

Specs

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

Specs

inf() :: Decimal.t()

Generates infinity.

Examples

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

Specs

inf?(input()) :: boolean()

Whether the number is infinite or not.

Examples

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

Specs

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

Specs

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

Specs

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

Specs

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

Specs

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
Link to this function

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

View Source

Specs

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

Specs

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