Skip to content

Commit

Permalink
add gte?/2 and lte?/2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazuh committed Apr 2, 2024
1 parent 89e7eae commit ee6a898
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
72 changes: 70 additions & 2 deletions lib/decimal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ defmodule Decimal do
def eq?(num1, num2), do: compare(num1, num2) == :eq

@doc """
Compares two numbers numerically and returns `true` if the the first argument
Compares two numbers numerically and returns `true` if the first argument
is greater than the second, otherwise `false`. If one the operands is a
quiet NaN this operation will always return `false`.
Expand All @@ -467,7 +467,7 @@ defmodule Decimal do
def gt?(num1, num2), do: compare(num1, num2) == :gt

@doc """
Compares two numbers numerically and returns `true` if the the first number is
Compares two numbers numerically and returns `true` if the first number is
less than the second number, otherwise `false`. If one of the operands is a
quiet NaN this operation will always return `false`.
Expand All @@ -486,6 +486,74 @@ defmodule Decimal do
def lt?(_num1, %Decimal{coef: :NaN}), do: false
def lt?(num1, num2), do: compare(num1, num2) == :lt

@doc """
Compares two numbers numerically and returns `true` if
the first argument is greater than or equal the second,
otherwise `false`.
If one the operands is a quiet NaN this operation
will always return `false`.
## Examples
iex> Decimal.gte?("1.3", "1.3")
true
iex> Decimal.gte?("1.3", "1.2")
true
iex> Decimal.gte?("1.2", "1.3")
false
"""
doc_since("2.1.0")
@spec gte?(decimal, decimal) :: boolean

def gte?(%Decimal{coef: :NaN}, _num2), do: false
def gte?(_num1, %Decimal{coef: :NaN}), do: false

def gte?(num1, num2) do
case compare(num1, num2) do
:gt -> true
:eq -> true
_ -> false
end
end

@doc """
Compares two numbers numerically and returns `true` if
the first number is less than or equal the second number,
otherwise `false`.
If one of the operands is a quiet NaN this operation
will always return `false`.
## Examples
iex> Decimal.lte?("1.1", "1.1")
true
iex> Decimal.lte?("1.1", "1.2")
true
iex> Decimal.lte?("1.4", "1.2")
false
"""
doc_since("2.1.0")
@spec lte?(decimal, decimal) :: boolean

def lte?(%Decimal{coef: :NaN}, _num2), do: false
def lte?(_num1, %Decimal{coef: :NaN}), do: false

def lte?(num1, num2) do
case compare(num1, num2) do
:lt -> true
:eq -> true
_ -> false
end
end

@doc """
Divides two numbers.
Expand Down
18 changes: 18 additions & 0 deletions test/decimal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,24 @@ defmodule DecimalTest do
refute Decimal.lt?(~d"1", ~d"nan")
end

test "gte?/2" do
assert Decimal.gte?(~d"420", ~d"42e1")
assert Decimal.gte?(~d"1", ~d"0")
refute Decimal.gte?(~d"0", ~d"1")
assert Decimal.gte?(~d"0", ~d"-0")
refute Decimal.gte?(~d"nan", ~d"1")
refute Decimal.gte?(~d"1", ~d"nan")
end

test "lte?/2" do
assert Decimal.lte?(~d"420", ~d"42e1")
refute Decimal.lte?(~d"1", ~d"0")
assert Decimal.lte?(~d"0", ~d"1")
assert Decimal.lte?(~d"0", ~d"-0")
refute Decimal.lte?(~d"nan", ~d"1")
refute Decimal.lte?(~d"1", ~d"nan")
end

test "div/2" do
Context.with(%Context{precision: 5, rounding: :half_up}, fn ->
assert Decimal.div(~d"1", ~d"3") == d(1, 33333, -5)
Expand Down

0 comments on commit ee6a898

Please sign in to comment.