Skip to content

Commit c3506d8

Browse files
seanmor5polvalente
andauthored
Some fixes for logarithms (#21)
* Some fixes for logarithms * chore: format and tests * chore: bump patch Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com>
1 parent 1902097 commit c3506d8

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/complex.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,8 @@ defmodule Complex do
865865
def ln(:infinity), do: :infinity
866866
def ln(:neg_infinity), do: new(:infinity, :math.pi())
867867
def ln(:nan), do: :nan
868+
def ln(n) when is_number(n) and n == 0, do: :neg_infinity
869+
def ln(n) when is_number(n) and n < 0, do: :nan
868870
def ln(n) when is_number(n), do: :math.log(n)
869871

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

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

@@ -918,6 +922,8 @@ defmodule Complex do
918922
def log2(:infinity), do: :infinity
919923
def log2(:neg_infinity), do: divide(ln(:neg_infinity), :math.log(2))
920924
def log2(:nan), do: :nan
925+
def log2(n) when is_number(n) and n == 0, do: :neg_infinity
926+
def log2(n) when is_number(n) and n < 0, do: :nan
921927

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

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Complex.Mixfile do
44
def project do
55
[
66
app: :complex,
7-
version: "0.4.1",
7+
version: "0.4.2",
88
description: description(),
99
package: package(),
1010
elixir: "~> 1.12",

test/complex_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,22 @@ defmodule ComplexTest do
384384
assert_close Complex.log10(a), :math.log10(a)
385385
assert_close Complex.log2(a), :math.log2(a)
386386
assert_close Complex.power(a, b), :math.pow(a, b)
387+
388+
assert Complex.ln(0) == :neg_infinity
389+
assert Complex.log10(0) == :neg_infinity
390+
assert Complex.log2(0) == :neg_infinity
391+
392+
assert Complex.ln(:infinity) == :infinity
393+
assert Complex.log10(:infinity) == :infinity
394+
assert Complex.log2(:infinity) == :infinity
395+
396+
assert Complex.ln(-1) == :nan
397+
assert Complex.log10(-1) == :nan
398+
assert Complex.log2(-1) == :nan
399+
400+
assert Complex.ln(:nan) == :nan
401+
assert Complex.log10(:nan) == :nan
402+
assert Complex.log2(:nan) == :nan
387403
end
388404

389405
test "power (non-finite)" do

0 commit comments

Comments
 (0)