Skip to content

Commit

Permalink
Some fixes for logarithms (#21)
Browse files Browse the repository at this point in the history
* Some fixes for logarithms

* chore: format and tests

* chore: bump patch

Co-authored-by: Paulo Valente <[email protected]>
  • Loading branch information
seanmor5 and polvalente authored Aug 13, 2022
1 parent 1902097 commit c3506d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/complex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,8 @@ defmodule Complex do
def ln(:infinity), do: :infinity
def ln(:neg_infinity), do: new(:infinity, :math.pi())
def ln(:nan), do: :nan
def ln(n) when is_number(n) and n == 0, do: :neg_infinity
def ln(n) when is_number(n) and n < 0, do: :nan
def ln(n) when is_number(n), do: :math.log(n)

def ln(z = %Complex{}) do
Expand All @@ -891,6 +893,8 @@ defmodule Complex do
def log10(:infinity), do: :infinity
def log10(:neg_infinity), do: divide(ln(:neg_infinity), :math.log(10))
def log10(:nan), do: :nan
def log10(n) when is_number(n) and n == 0, do: :neg_infinity
def log10(n) when is_number(n) and n < 0, do: :nan

def log10(n) when is_number(n), do: :math.log10(n)

Expand Down Expand Up @@ -918,6 +922,8 @@ defmodule Complex do
def log2(:infinity), do: :infinity
def log2(:neg_infinity), do: divide(ln(:neg_infinity), :math.log(2))
def log2(:nan), do: :nan
def log2(n) when is_number(n) and n == 0, do: :neg_infinity
def log2(n) when is_number(n) and n < 0, do: :nan

def log2(n) when is_number(n), do: :math.log2(n)

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Complex.Mixfile do
def project do
[
app: :complex,
version: "0.4.1",
version: "0.4.2",
description: description(),
package: package(),
elixir: "~> 1.12",
Expand Down
16 changes: 16 additions & 0 deletions test/complex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,22 @@ defmodule ComplexTest do
assert_close Complex.log10(a), :math.log10(a)
assert_close Complex.log2(a), :math.log2(a)
assert_close Complex.power(a, b), :math.pow(a, b)

assert Complex.ln(0) == :neg_infinity
assert Complex.log10(0) == :neg_infinity
assert Complex.log2(0) == :neg_infinity

assert Complex.ln(:infinity) == :infinity
assert Complex.log10(:infinity) == :infinity
assert Complex.log2(:infinity) == :infinity

assert Complex.ln(-1) == :nan
assert Complex.log10(-1) == :nan
assert Complex.log2(-1) == :nan

assert Complex.ln(:nan) == :nan
assert Complex.log10(:nan) == :nan
assert Complex.log2(:nan) == :nan
end

test "power (non-finite)" do
Expand Down

0 comments on commit c3506d8

Please sign in to comment.