Skip to content

Commit 92cd083

Browse files
committed
fix: hyperbolic functions
1 parent 0facf27 commit 92cd083

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

lib/complex.ex

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -884,11 +884,26 @@ defmodule Complex do
884884
def exp(:nan), do: :nan
885885
def exp(n) when is_number(n), do: :math.exp(n)
886886

887-
def exp(%Complex{re: :neg_infinity, im: _}), do: 0
887+
def exp(%Complex{re: :neg_infinity, im: _}), do: new(0, 0)
888888
def exp(%Complex{re: :infinity, im: :nan}), do: new(:infinity, :nan)
889889

890-
def exp(%Complex{re: :infinity, im: im}) when is_number(im),
891-
do: new(:infinity, multiply(:infinity, im))
890+
def exp(%Complex{re: :infinity, im: im}) when is_number(im) do
891+
re =
892+
case :math.cos(im) do
893+
x when x > 0 -> :infinity
894+
x when x < 0 -> :neg_infinity
895+
_ -> :nan
896+
end
897+
898+
im =
899+
case :math.sin(im) do
900+
x when x > 0 -> :infinity
901+
x when x < 0 -> :neg_infinity
902+
_ -> :nan
903+
end
904+
905+
new(re, im)
906+
end
892907

893908
def exp(%Complex{re: _, im: :neg_infinity}), do: new(:nan, :nan)
894909
def exp(%Complex{re: _, im: :infinity}), do: new(:nan, :nan)
@@ -1618,10 +1633,12 @@ defmodule Complex do
16181633
def sinh(n) when is_number(n), do: :math.sinh(n)
16191634

16201635
def sinh(z = %Complex{}) do
1621-
z
1622-
|> exp()
1623-
|> subtract(exp(negate(z)))
1624-
|> divide(2)
1636+
%Complex{re: re, im: im} =
1637+
z
1638+
|> exp()
1639+
|> subtract(exp(negate(z)))
1640+
1641+
new(divide(re, 2), divide(im, 2))
16251642
end
16261643

16271644
@doc """
@@ -1686,12 +1703,15 @@ defmodule Complex do
16861703
def cosh(:infinity), do: :infinity
16871704
def cosh(:neg_infinity), do: :infinity
16881705
def cosh(:nan), do: :nan
1706+
def cosh(n) when is_number(n), do: :math.cosh(n)
16891707

16901708
def cosh(z) do
1691-
z
1692-
|> exp()
1693-
|> add(exp(negate(z)))
1694-
|> divide(2)
1709+
%Complex{re: re, im: im} =
1710+
z
1711+
|> exp()
1712+
|> add(exp(negate(z)))
1713+
1714+
new(divide(re, 2), divide(im, 2))
16951715
end
16961716

16971717
@doc """

0 commit comments

Comments
 (0)