Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ function exp(z::Complex)
end
end

function expm1(z::Complex)
function expm1(z::Complex{T}) where T<:Real
Tf = float(T)
zr,zi = reim(z)
if isnan(zr)
Complex(zr, zi==0 ? zi : zr)
Expand All @@ -533,8 +534,12 @@ function expm1(z::Complex)
Complex(erm1, zi)
else
er = erm1+one(erm1)
wr = isfinite(er) ? erm1 - 2.0*er*(sin(0.5*zi))^2 : er*cos(zi)
Complex(wr, er*sin(zi))
if isfinite(er)
wr = erm1 - 2 * er * (sin(convert(Tf, 0.5) * zi))^2
Copy link
Member

@giordano giordano May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I'm a bit too late, but why not using zi / 2 instead of converting 0.5? The performance difference is sensible for BigFloat for this operation (not the whole function, though).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just trying to not change the result for machine types and being conservative about this transformation. I assume this transformation is exact for Float16, Float32 and Float64 (though floating point corner cases frequently surprises me...)? If that's the case then I think we can change this to / 2.

return Complex(wr, er * sin(zi))
else
return Complex(er * cos(zi), er * sin(zi))
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -951,3 +951,10 @@ end
@test log1p(complex(x, x)) ≈ log(1 + complex(x, x))
end
end

@testset "expm1 type stability" begin
x = @inferred expm1(0.1im)
@test x isa Complex128
x = @inferred expm1(0.1f0im)
@test x isa Complex64
end