Skip to content

Commit

Permalink
Fix compare against 0 (#197)
Browse files Browse the repository at this point in the history
Co-authored-by: vshev4enko <[email protected]>
  • Loading branch information
ericmj and vshev4enko authored Apr 26, 2023
1 parent 0ae3bd5 commit eed830a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
16 changes: 8 additions & 8 deletions lib/decimal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ defmodule Decimal do

def compare(%Decimal{coef: 0}, %Decimal{coef: 0}), do: :eq

def compare(%Decimal{sign: 1}, %Decimal{coef: 0}), do: :gt
def compare(%Decimal{coef: 0}, %Decimal{sign: 1}), do: :lt
def compare(%Decimal{sign: -1}, %Decimal{coef: 0}), do: :lt
def compare(%Decimal{coef: 0}, %Decimal{sign: -1}), do: :gt

def compare(%Decimal{sign: 1}, %Decimal{sign: -1}), do: :gt
def compare(%Decimal{sign: -1}, %Decimal{sign: 1}), do: :lt

Expand All @@ -358,14 +363,9 @@ defmodule Decimal do
padded_num2 = pad_num(num2, num2.exp - num1.exp)

cond do
padded_num1 == padded_num2 ->
0

padded_num1 < padded_num2 ->
-num1.sign

true ->
num1.sign
padded_num1 == padded_num2 -> 0
padded_num1 < padded_num2 -> -num1.sign
true -> num1.sign
end

adjusted_exp1 < adjusted_exp2 ->
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ defmodule Decimal.Mixfile do

defp deps() do
[
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
{:ex_doc, ">= 0.0.0", only: :dev},
{:stream_data, "~> 0.5.0", only: :test}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.0", "9e18a119d9efc3370a3ef2a937bf0b24c088d9c4bf0ba9d7c3751d49d347d035", [:mix], [], "hexpm", "7977f183127a7cbe9346981e2f480dc04c55ffddaef746bd58debd566070eef8"},
"stream_data": {:hex, :stream_data, "0.5.0", "b27641e58941685c75b353577dc602c9d2c12292dd84babf506c2033cd97893e", [:mix], [], "hexpm", "012bd2eec069ada4db3411f9115ccafa38540a3c78c4c0349f151fc761b9e271"},
}
48 changes: 48 additions & 0 deletions test/decimal/property_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule Decimal.PropertyTest do
use ExUnit.Case, async: true
use ExUnitProperties

describe "compare/2" do
test "integer equality" do
check all(
first <- StreamData.integer(),
second <- StreamData.integer()
) do
assert Decimal.compare(first, second) == term_compare(first, second)
end
end

test "float equality" do
check all(
first <- StreamData.float(),
second <- StreamData.float()
) do
assert Decimal.compare(to_dec(first), to_dec(second)) == term_compare(first, second)
end
end

test "number equality" do
check all(
first <- stream_data_number(),
second <- stream_data_number()
) do
assert Decimal.compare(to_dec(first), to_dec(second)) == term_compare(first, second)
end
end
end

defp to_dec(float) when is_float(float), do: Decimal.from_float(float)
defp to_dec(other), do: Decimal.new(other)

defp term_compare(first, second) do
cond do
first < second -> :lt
first > second -> :gt
true -> :eq
end
end

defp stream_data_number() do
StreamData.one_of([StreamData.integer(), StreamData.float()])
end
end
6 changes: 6 additions & 0 deletions test/decimal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ defmodule DecimalTest do
assert Decimal.compare(~d"0", ~d"inf") == :lt
assert Decimal.compare(~d"0", ~d"-inf") == :gt

assert Decimal.compare(~d"0.123", ~d"0") == :gt
assert Decimal.compare(~d"0.123", ~d"0.122") == :gt
assert Decimal.compare(~d"0.123", ~d"0.123") == :eq
assert Decimal.compare(~d"0.123", ~d"0.124") == :lt
assert Decimal.compare(~d"0.0123", ~d"0.124") == :lt

assert Decimal.compare("Inf", "Inf") == :eq

assert Decimal.compare(~d"5e10000000000", ~d"0") == :gt
Expand Down

0 comments on commit eed830a

Please sign in to comment.