Skip to content

Commit b846391

Browse files
ogautheinkydragon
andauthored
fix ellipk and ellipe (#499)
* fix ellipk and ellipe * use === * Apply suggestion from @inkydragon --------- Co-authored-by: Chengyu Han <cyhan.dev@outlook.com>
1 parent 2682252 commit b846391

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/ellip.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ As suggested in this paper, the domain is restricted to ``(-\infty,1]``.
3636
ellipk(m::Real) = _ellipk(float(m))
3737

3838
function _ellipk(m::Float64)
39+
isnan(m) && return NaN
40+
(m === -Inf) && return 0.0
41+
3942
flag_is_m_neg = m < 0.0
4043
if flag_is_m_neg
4144
x = m / (m-1) #dealing with negative args
@@ -47,7 +50,7 @@ function _ellipk(m::Float64)
4750
return Float64(halfπ)
4851

4952
elseif x == 1.0
50-
return Inf
53+
return flag_is_m_neg ? 0.0 : Inf
5154

5255
elseif x > 1.0
5356
throw(DomainError(m, "`m` must lie between -Inf and 1 ---- Domain: (-Inf,1.0]"))
@@ -206,6 +209,9 @@ As suggested in this paper, the domain is restricted to ``(-\infty,1]``.
206209
ellipe(m::Real) = _ellipe(float(m))
207210

208211
function _ellipe(m::Float64)
212+
isnan(m) && return NaN
213+
(m === -Inf) && return Inf
214+
209215
flag_is_m_neg = m < 0.0
210216
if flag_is_m_neg
211217
x = m / (m-1) #dealing with negative args
@@ -216,7 +222,7 @@ function _ellipe(m::Float64)
216222
if x == 0.0
217223
return Float64(halfπ)
218224
elseif x == 1.0
219-
return 1.0
225+
return flag_is_m_neg ? Inf : 1.0
220226

221227
elseif x > 1.0
222228
throw(DomainError(m,"`m` must lie between -Inf and 1 ---- Domain : (-inf,1.0]"))

test/ellip.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
@test ellipk(1.0) == Inf
1818
@test ellipk(Float16(0.92)) 2.683551406315229344 rtol=2*eps(Float16)
1919
@test ellipk(Float32(0.92)) 2.683551406315229344 rtol=2*eps(Float32)
20+
@test isnan(ellipk(NaN))
21+
@test ellipk(-Inf) == 0.0
22+
@test ellipk(-1e30) 0.0 atol=1e-13
2023
@test_throws MethodError ellipk(BigFloat(0.5))
2124
@test_throws DomainError ellipk(1.1)
2225
end
@@ -38,6 +41,9 @@
3841
@test ellipe(1.0) 1.00 rtol=2*eps()
3942
@test ellipe(Float16(0.865)) 1.1322436887003925 rtol=2*eps(Float16)
4043
@test ellipe(Float32(0.865)) 1.1322436887003925 rtol=2*eps(Float32)
44+
@test isnan(ellipe(NaN))
45+
@test ellipe(-Inf) == Inf
46+
@test ellipe(-1e16) > ellipe(-1e15)
4147
@test_throws MethodError ellipe(BigFloat(-1))
4248
@test_throws DomainError ellipe(1.2)
4349
end

0 commit comments

Comments
 (0)