• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #38182

15 Aug 2025 03:55AM UTC coverage: 77.87% (-0.4%) from 78.28%
#38182

push

local

web-flow
🤖 [master] Bump the SparseArrays stdlib from 30201ab to bb5ecc0 (#59263)

Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: main
Julia branch: master
Old commit: 30201ab
New commit: bb5ecc0
Julia version: 1.13.0-DEV
SparseArrays version: 1.13.0
Bump invoked by: @ViralBShah
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
https://github.com/JuliaSparse/SparseArrays.jl/compare/30201abcb...bb5ecc091

```
$ git log --oneline 30201ab..bb5ecc0
bb5ecc0 fast quadratic form for dense matrix, sparse vectors (#640)
34ece87 Extend 3-arg `dot` to generic `HermOrSym` sparse matrices (#643)
095b685 Exclude unintended complex symmetric sparse matrices from 3-arg `dot` (#642)
8049287 Fix signature for 2-arg matrix-matrix `dot` (#641)
cff971d Make cond(::SparseMatrix, 1 / Inf) discoverable from 2-norm error (#629)
```

Co-authored-by: ViralBShah <744411+ViralBShah@users.noreply.github.com>

48274 of 61993 relevant lines covered (77.87%)

9571166.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.87
/base/complex.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4
    Complex{T<:Real} <: Number
5

6
Complex number type with real and imaginary part of type `T`.
7

8
`ComplexF16`, `ComplexF32` and `ComplexF64` are aliases for
9
`Complex{Float16}`, `Complex{Float32}` and `Complex{Float64}` respectively.
10

11
See also: [`Real`](@ref), [`complex`](@ref), [`real`](@ref).
12
"""
13
struct Complex{T<:Real} <: Number
14
    re::T
67,087,272✔
15
    im::T
16
end
17
Complex(x::Real, y::Real) = Complex(promote(x,y)...)
6,031✔
18
Complex(x::Real) = Complex(x, zero(x))
15,201✔
19

20
"""
21
    im
22

23
The imaginary unit.
24

25
See also: [`imag`](@ref), [`angle`](@ref), [`complex`](@ref).
26

27
# Examples
28
```jldoctest
29
julia> im * im
30
-1 + 0im
31

32
julia> (2.0 + 3im)^2
33
-5.0 + 12.0im
34
```
35
"""
36
const im = Complex(false, true)
37

38
const ComplexF64  = Complex{Float64}
39
const ComplexF32  = Complex{Float32}
40
const ComplexF16  = Complex{Float16}
41

42
Complex{T}(x::Real) where {T<:Real} = Complex{T}(x,0)
6,343,007✔
43
Complex{T}(z::Complex) where {T<:Real} = Complex{T}(real(z),imag(z))
1,267,789✔
44
(::Type{T})(z::Complex) where {T<:Real} =
1,001,617✔
45
    isreal(z) ? T(real(z))::T : throw(InexactError(nameof(T), T, z))
46

47
Complex(z::Complex) = z
4,116✔
48

49
promote_rule(::Type{Complex{T}}, ::Type{S}) where {T<:Real,S<:Real} =
55,148✔
50
    Complex{promote_type(T,S)}
51
promote_rule(::Type{Complex{T}}, ::Type{Complex{S}}) where {T<:Real,S<:Real} =
41,390✔
52
    Complex{promote_type(T,S)}
53

54
widen(::Type{Complex{T}}) where {T} = Complex{widen(T)}
7,962✔
55

56
float(::Type{Complex{T}}) where {T<:AbstractFloat} = Complex{T}
5,200✔
57
float(::Type{Complex{T}}) where {T} = Complex{float(T)}
36✔
58

59
"""
60
    real(z)
61

62
Return the real part of the complex number `z`.
63

64
See also: [`imag`](@ref), [`reim`](@ref), [`complex`](@ref), [`isreal`](@ref), [`Real`](@ref).
65

66
# Examples
67
```jldoctest
68
julia> real(1 + 3im)
69
1
70
```
71
"""
72
real(z::Complex) = z.re
172,532,861✔
73

74
"""
75
    imag(z)
76

77
Return the imaginary part of the complex number `z`.
78

79
See also: [`conj`](@ref), [`reim`](@ref), [`adjoint`](@ref), [`angle`](@ref).
80

81
# Examples
82
```jldoctest
83
julia> imag(1 + 3im)
84
3
85
```
86
"""
87
imag(z::Complex) = z.im
174,552,775✔
88
real(x::Real) = x
98,731✔
89
imag(x::Real) = zero(x)
6,162✔
90

91
"""
92
    reim(z)
93

94
Return a tuple of the real and imaginary parts of the complex number `z`.
95

96
# Examples
97
```jldoctest
98
julia> reim(1 + 3im)
99
(1, 3)
100
```
101
"""
102
reim(z) = (real(z), imag(z))
1,054,216✔
103

104
"""
105
    real(T::Type)
106

107
Return the type that represents the real part of a value of type `T`.
108
e.g: for `T == Complex{R}`, returns `R`.
109
Equivalent to `typeof(real(zero(T)))`.
110

111
# Examples
112
```jldoctest
113
julia> real(Complex{Int})
114
Int64
115

116
julia> real(Float64)
117
Float64
118
```
119
"""
120
real(T::Type) = typeof(real(zero(T)))
24✔
121
real(::Type{T}) where {T<:Real} = T
52,668✔
122
real(C::Type{<:Complex}) = fieldtype(C, 1)
23,666✔
123
real(::Type{Union{}}, slurp...) = Union{}(im)
×
124

125
"""
126
    isreal(x)::Bool
127

128
Test whether `x` or all its elements are numerically equal to some real number
129
including infinities and NaNs. `isreal(x)` is true if `isequal(x, real(x))`
130
is true.
131

132
# Examples
133
```jldoctest
134
julia> isreal(5.)
135
true
136

137
julia> isreal(1 - 3im)
138
false
139

140
julia> isreal(Inf + 0im)
141
true
142

143
julia> isreal([4.; complex(0,1)])
144
false
145
```
146
"""
147
isreal(x::Real) = true
×
148
isreal(z::Complex) = iszero(imag(z))
5,470,098✔
149
isinteger(z::Complex) = isreal(z) & isinteger(real(z))
3,072✔
150
isfinite(z::Complex) = isfinite(real(z)) & isfinite(imag(z))
136,505✔
151
isnan(z::Complex) = isnan(real(z)) | isnan(imag(z))
14✔
152
isinf(z::Complex) = isinf(real(z)) | isinf(imag(z))
5✔
153
iszero(z::Complex) = iszero(real(z)) & iszero(imag(z))
5,621,158✔
154
isone(z::Complex) = isone(real(z)) & iszero(imag(z))
17,770✔
155

156
"""
157
    complex(r, [i])
158

159
Convert real numbers or arrays to complex. `i` defaults to zero.
160

161
# Examples
162
```jldoctest
163
julia> complex(7)
164
7 + 0im
165
```
166
"""
167
complex(z::Complex) = z
6,022✔
168
complex(x::Real) = Complex(x)
3,861✔
169
complex(x::Real, y::Real) = Complex(x, y)
278,973✔
170

171
"""
172
    complex(T::Type)
173

174
Return an appropriate type which can represent a value of type `T` as a complex number.
175
Equivalent to `typeof(complex(zero(T)))` if `T` does not contain `Missing`.
176

177
# Examples
178
```jldoctest
179
julia> complex(Complex{Int})
180
Complex{Int64}
181

182
julia> complex(Int)
183
Complex{Int64}
184

185
julia> complex(Union{Int, Missing})
186
Union{Missing, Complex{Int64}}
187
```
188
"""
189
complex(::Type{T}) where {T<:Real} = Complex{T}
120✔
190
complex(::Type{Complex{T}}) where {T<:Real} = Complex{T}
1,503✔
191

192
flipsign(x::Complex, y::Real) = ifelse(signbit(y), -x, x)
8✔
193

194
function show(io::IO, z::Complex)
969,042✔
195
    r, i = reim(z)
969,042✔
196
    compact = get(io, :compact, false)::Bool
3,548,732✔
197
    show(io, r)
969,042✔
198
    if signbit(i) && !isnan(i)
969,042✔
199
        print(io, compact ? "-" : " - ")
137,614✔
200
        if isa(i,Signed) && !isa(i,BigInt) && i == typemin(typeof(i))
137,614✔
201
            show(io, -widen(i))
1✔
202
        else
203
            show(io, -i)
137,613✔
204
        end
205
    else
206
        print(io, compact ? "+" : " + ")
831,428✔
207
        show(io, i)
831,428✔
208
    end
209
    if !(isa(i,Signed) || isa(i,AbstractFloat) && isfinite(i))
969,044✔
210
        print(io, "*")
403,217✔
211
    end
212
    print(io, "im")
969,042✔
213
end
214
show(io::IO, z::Complex{Bool}) =
2✔
215
    print(io, z == im ? "im" : "Complex($(z.re),$(z.im))")
216

217
function show_unquoted(io::IO, z::Complex, ::Int, prec::Int)
29✔
218
    if operator_precedence(:+) <= prec
29✔
219
        print(io, "(")
22✔
220
        show(io, z)
22✔
221
        print(io, ")")
22✔
222
    else
223
        show(io, z)
7✔
224
    end
225
end
226

227
function read(s::IO, ::Type{Complex{T}}) where T<:Real
2✔
228
    r = read(s,T)
2✔
229
    i = read(s,T)
2✔
230
    Complex{T}(r,i)
2✔
231
end
232
function write(s::IO, z::Complex)
1✔
233
    write(s,real(z),imag(z))
1✔
234
end
235

236
## byte order swaps: real and imaginary part are swapped individually
237
bswap(z::Complex) = Complex(bswap(real(z)), bswap(imag(z)))
2✔
238

239
## equality and hashing of complex numbers ##
240

241
==(z::Complex, w::Complex) = (real(z) == real(w)) & (imag(z) == imag(w))
844,922✔
242
==(z::Complex, x::Real) = isreal(z) && real(z) == x
420,713✔
243
==(x::Real, z::Complex) = isreal(z) && real(z) == x
177✔
244

245
isequal(z::Complex, w::Complex) = isequal(real(z),real(w))::Bool & isequal(imag(z),imag(w))::Bool
56,850✔
246
isequal(z::Complex, w::Real) = isequal(real(z),w)::Bool & isequal(imag(z),zero(w))::Bool
16✔
247
isequal(z::Real, w::Complex) = isequal(z,real(w))::Bool & isequal(zero(z),imag(w))::Bool
×
248

249
in(x::Complex, r::AbstractRange{<:Real}) = isreal(x) && real(x) in r
8✔
250

251
if UInt === UInt64
252
    const h_imag = 0x32a7a07f3e7cd1f9
253
else
254
    const h_imag = 0x3e7cd1f9
255
end
256
const hash_0_imag = hash(0, h_imag)
257

258
function hash(z::Complex, h::UInt)
259
    # TODO: with default argument specialization, this would be better:
260
    # hash(real(z), h ⊻ hash(imag(z), h ⊻ h_imag) ⊻ hash(0, h ⊻ h_imag))
261
    hash(real(z), h ⊻ hash(imag(z), h_imag) ⊻ hash_0_imag)
47✔
262
end
263

264
## generic functions of complex numbers ##
265

266
"""
267
    conj(z)
268

269
Compute the complex conjugate of a complex number `z`.
270

271
See also: [`angle`](@ref), [`adjoint`](@ref).
272

273
# Examples
274
```jldoctest
275
julia> conj(1 + 3im)
276
1 - 3im
277
```
278
"""
279
conj(z::Complex) = Complex(real(z),-imag(z))
11,898,338✔
280
abs(z::Complex)  = hypot(real(z), imag(z))
28,406,686✔
281
abs2(z::Complex) = real(z)*real(z) + imag(z)*imag(z)
18,599,499✔
282
function inv(z::Complex)
25,850✔
283
    c, d = reim(z)
25,854✔
284
    (isinf(c) | isinf(d)) && return complex(copysign(zero(c), c), flipsign(-zero(d), d))
25,854✔
285
    complex(c, -d)/(c * c + d * d)
25,826✔
286
end
287
inv(z::Complex{<:Integer}) = inv(float(z))
1,441✔
288

289
+(z::Complex) = Complex(+real(z), +imag(z))
27✔
290
-(z::Complex) = Complex(-real(z), -imag(z))
271,208✔
291
+(z::Complex, w::Complex) = Complex(real(z) + real(w), imag(z) + imag(w))
22,330,470✔
292
-(z::Complex, w::Complex) = Complex(real(z) - real(w), imag(z) - imag(w))
8,501,844✔
293
*(z::Complex, w::Complex) = Complex(real(z) * real(w) - imag(z) * imag(w),
20,576,620✔
294
                                    real(z) * imag(w) + imag(z) * real(w))
295

296
muladd(z::Complex, w::Complex, x::Complex) =
3,609,160✔
297
    Complex(muladd(real(z), real(w), -muladd(imag(z), imag(w), -real(x))),
298
            muladd(real(z), imag(w),  muladd(imag(z), real(w),  imag(x))))
299

300
# handle Bool and Complex{Bool}
301
# avoid type signature ambiguity warnings
302
+(x::Bool, z::Complex{Bool}) = Complex(x + real(z), imag(z))
×
303
+(z::Complex{Bool}, x::Bool) = Complex(real(z) + x, imag(z))
×
304
-(x::Bool, z::Complex{Bool}) = Complex(x - real(z), - imag(z))
×
305
-(z::Complex{Bool}, x::Bool) = Complex(real(z) - x, imag(z))
×
306
*(x::Bool, z::Complex{Bool}) = Complex(x * real(z), x * imag(z))
×
307
*(z::Complex{Bool}, x::Bool) = Complex(real(z) * x, imag(z) * x)
×
308

309
+(x::Bool, z::Complex) = Complex(x + real(z), imag(z))
2,205✔
310
+(z::Complex, x::Bool) = Complex(real(z) + x, imag(z))
12,722✔
311
-(x::Bool, z::Complex) = Complex(x - real(z), - imag(z))
2,085✔
312
-(z::Complex, x::Bool) = Complex(real(z) - x, imag(z))
255,827✔
313
*(x::Bool, z::Complex) = Complex(x * real(z), x * imag(z))
18,521✔
314
*(z::Complex, x::Bool) = Complex(real(z) * x, imag(z) * x)
460,178✔
315

316
+(x::Real, z::Complex{Bool}) = Complex(x + real(z), imag(z))
4,000,516✔
317
+(z::Complex{Bool}, x::Real) = Complex(real(z) + x, imag(z))
24✔
318
function -(x::Real, z::Complex{Bool})
51✔
319
    # we don't want the default type for -(Bool)
320
    re = x-real(z)
3,092✔
321
    Complex(re, - oftype(re, imag(z)))
3,092✔
322
end
323
-(z::Complex{Bool}, x::Real) = Complex(real(z) - x, imag(z))
24✔
324
*(x::Real, z::Complex{Bool}) = Complex(x * real(z), x * imag(z))
42,220✔
325
*(z::Complex{Bool}, x::Real) = Complex(real(z) * x, imag(z) * x)
50,792✔
326

327
# adding or multiplying real & complex is common
328
+(x::Real, z::Complex) = Complex(x + real(z), imag(z))
4,406,477✔
329
+(z::Complex, x::Real) = Complex(x + real(z), imag(z))
24,218✔
330
function -(x::Real, z::Complex)
422,253✔
331
    # we don't want the default type for -(Bool)
332
    re = x - real(z)
4,500,530✔
333
    Complex(re, - oftype(re, imag(z)))
4,500,380✔
334
end
335
-(z::Complex, x::Real) = Complex(real(z) - x, imag(z))
109,089✔
336
*(x::Real, z::Complex) = Complex(x * real(z), x * imag(z))
2,995,782✔
337
*(z::Complex, x::Real) = Complex(x * real(z), x * imag(z))
5,268,969✔
338

339
muladd(x::Real, z::Complex, y::Union{Real,Complex}) = muladd(z, x, y)
1,547,868✔
340
muladd(z::Complex, x::Real, y::Real) = Complex(muladd(real(z),x,y), imag(z)*x)
1,016,489✔
341
muladd(z::Complex, x::Real, w::Complex) =
8,800,503✔
342
    Complex(muladd(real(z),x,real(w)), muladd(imag(z),x,imag(w)))
343
muladd(x::Real, y::Real, z::Complex) = Complex(muladd(x,y,real(z)), imag(z))
337✔
344
muladd(z::Complex, w::Complex, x::Real) =
17✔
345
    Complex(muladd(real(z), real(w), -muladd(imag(z), imag(w), -x)),
346
            muladd(real(z), imag(w), imag(z) * real(w)))
347

348
/(a::R, z::S) where {R<:Real,S<:Complex} = (T = promote_type(R,S); a*inv(T(z)))
45,030✔
349
/(z::Complex, x::Real) = Complex(real(z)/x, imag(z)/x)
224,990✔
350

351
function /(a::Complex{T}, b::Complex{T}) where T<:Real
307,622✔
352
    are = real(a); aim = imag(a); bre = real(b); bim = imag(b)
307,662✔
353
    if (isinf(bre) | isinf(bim))
307,662✔
354
        if isfinite(a)
4✔
355
            return complex(zero(T)*sign(are)*sign(bre), -zero(T)*sign(aim)*sign(bim))
2✔
356
        end
357
        return T(NaN)+T(NaN)*im
2✔
358
    end
359
    if abs(bre) <= abs(bim)
307,659✔
360
        r = bre / bim
39,038✔
361
        den = bim + r*bre
39,038✔
362
        Complex((are*r + aim)/den, (aim*r - are)/den)
39,038✔
363
    else
364
        r = bim / bre
268,620✔
365
        den = bre + r*bim
268,621✔
366
        Complex((are + aim*r)/den, (aim - are*r)/den)
268,620✔
367
    end
368
end
369

370
function /(z::Complex{T}, w::Complex{T}) where {T<:Union{Float16,Float32}}
2,521✔
371
    c, d = reim(widen(w))
2,628✔
372
    a, b = reim(widen(z))
2,628✔
373
    if (isinf(c) | isinf(d))
2,628✔
374
        if isfinite(z)
4✔
375
            return complex(zero(T)*sign(real(z))*sign(real(w)), -zero(T)*sign(imag(z))*sign(imag(w)))
2✔
376
        end
377
        return T(NaN)+T(NaN)*im
2✔
378
    end
379
    mag = inv(muladd(c, c, d^2))
2,624✔
380
    re_part = muladd(a, c, b*d)
2,624✔
381
    im_part = muladd(b, c, -a*d)
2,624✔
382
    return oftype(z, Complex(re_part*mag, im_part*mag))
2,624✔
383
end
384

385
# robust complex division for double precision
386
# variables are scaled & unscaled to avoid over/underflow, if necessary
387
# based on arxiv.1210.4539
388
#             a + i*b
389
#  p + i*q = ---------
390
#             c + i*d
391
function /(z::ComplexF64, w::ComplexF64)
392
    a, b = reim(z); c, d = reim(w)
4✔
393
    absa = abs(a); absb = abs(b);  ab = absa >= absb ? absa : absb # equiv. to max(abs(a),abs(b)) but without NaN-handling (faster)
4✔
394
    absc = abs(c); absd = abs(d);  cd = absc >= absd ? absc : absd
4✔
395
    if (isinf(c) | isinf(d))
4✔
396
        if isfinite(z)
×
397
            return complex(0.0*sign(a)*sign(c), -0.0*sign(b)*sign(d))
×
398
        end
399
        return NaN+NaN*im
×
400
    end
401
    halfov = 0.5*floatmax(Float64)              # overflow threshold
4✔
402
    twounϵ = floatmin(Float64)*2.0/eps(Float64) # underflow threshold
4✔
403

404
    # actual division operations
405
    if  ab>=halfov || ab<=twounϵ || cd>=halfov || cd<=twounϵ # over/underflow case
8✔
406
        p,q = scaling_cdiv(a,b,c,d,ab,cd) # scales a,b,c,d before division (unscales after)
4✔
407
    else
408
        p,q = cdiv(a,b,c,d)
×
409
    end
410

411
    return ComplexF64(p,q)
4✔
412
end
413

414
# sub-functionality for /(z::ComplexF64, w::ComplexF64)
415
@inline function cdiv(a::Float64, b::Float64, c::Float64, d::Float64)
416
    if abs(d)<=abs(c)
×
417
        p,q = robust_cdiv1(a,b,c,d)
×
418
    else
419
        p,q = robust_cdiv1(b,a,d,c)
×
420
        q = -q
×
421
    end
422
    return p,q
×
423
end
424
@noinline function scaling_cdiv(a::Float64, b::Float64, c::Float64, d::Float64, ab::Float64, cd::Float64)
×
425
    # this over/underflow functionality is outlined for performance, cf. #29688
426
    a,b,c,d,s = scaleargs_cdiv(a,b,c,d,ab,cd)
×
427
    p,q = cdiv(a,b,c,d)
×
428
    return p*s,q*s
×
429
end
430
function scaleargs_cdiv(a::Float64, b::Float64, c::Float64, d::Float64, ab::Float64, cd::Float64)
×
431
    ϵ      = eps(Float64)
×
432
    halfov = 0.5*floatmax(Float64)
×
433
    twounϵ = floatmin(Float64)*2.0/ϵ
×
434
    bs     = 2.0/(ϵ*ϵ)
×
435

436
    # scaling
437
    s = 1.0
×
438
    if ab >= halfov
×
439
        a*=0.5; b*=0.5; s*=2.0  # scale down a,b
×
440
    elseif ab <= twounϵ
×
441
        a*=bs;  b*=bs;  s/=bs   # scale up a,b
×
442
    end
443
    if cd >= halfov
×
444
        c*=0.5; d*=0.5; s*=0.5  # scale down c,d
×
445
    elseif cd <= twounϵ
×
446
        c*=bs;  d*=bs;  s*=bs   # scale up c,d
×
447
    end
448

449
    return a,b,c,d,s
×
450
end
451
@inline function robust_cdiv1(a::Float64, b::Float64, c::Float64, d::Float64)
×
452
    r = d/c
×
453
    t = 1.0/(c+d*r)
×
454
    p = robust_cdiv2(a,b,c,d,r,t)
×
455
    q = robust_cdiv2(b,-a,c,d,r,t)
×
456
    return p,q
×
457
end
458
function robust_cdiv2(a::Float64, b::Float64, c::Float64, d::Float64, r::Float64, t::Float64)
×
459
    if r != 0
×
460
        br = b*r
×
461
        return (br != 0 ? (a+br)*t : a*t + (b*t)*r)
×
462
    else
463
        return (a + d*(b/c)) * t
×
464
    end
465
end
466

467
function inv(z::Complex{T}) where T<:Union{Float16,Float32}
182✔
468
    c, d = reim(widen(z))
6,666✔
469
    (isinf(c) | isinf(d)) && return complex(copysign(zero(T), c), flipsign(-zero(T), d))
6,666✔
470
    mag = inv(muladd(c, c, d^2))
6,598✔
471
    return oftype(z, Complex(c*mag, -d*mag))
6,598✔
472
end
473
function inv(w::ComplexF64)
×
474
    c, d = reim(w)
×
475
    absc, absd = abs(c), abs(d)
×
476
    cd, dc = ifelse(absc>absd, (absc, absd), (absd, absc))
×
477
    # no overflow from abs2
478
    if sqrt(floatmin(Float64)/2) <= cd <= sqrt(floatmax(Float64)/2)
×
479
        return conj(w) / muladd(cd, cd, dc*dc)
×
480
    end
481
    (isinf(c) | isinf(d)) && return complex(copysign(0.0, c), flipsign(-0.0, d))
×
482

483
    ϵ  = eps(Float64)
×
484
    bs = 2/(ϵ*ϵ)
×
485

486
    # scaling
487
    s = 1.0
×
488
    if cd >= floatmax(Float64)/2
×
489
        c *= 0.5; d *= 0.5; s = 0.5 # scale down c, d
×
490
    elseif cd <= 2floatmin(Float64)/ϵ
×
491
        c *= bs;  d *= bs;  s = bs  # scale up c, d
×
492
    end
493

494
    # inversion operations
495
    if absd <= absc
×
496
        p, q = robust_cinv(c, d)
×
497
    else
498
        q, p = robust_cinv(-d, -c)
×
499
    end
500
    return ComplexF64(p*s, q*s)
×
501
end
502
function robust_cinv(c::Float64, d::Float64)
×
503
    r = d/c
×
504
    z = muladd(d, r, c)
×
505
    p = 1.0/z
×
506
    q = -r/z
×
507
    return p, q
×
508
end
509

510
function ssqs(x::T, y::T) where T<:Real
24,687✔
511
    k::Int = 0
25,045✔
512
    ρ = x*x + y*y
25,045✔
513
    if !isfinite(ρ) && (isinf(x) || isinf(y))
25,131✔
514
        ρ = convert(T, Inf)
190✔
515
    elseif isinf(ρ) || (ρ==0 && (x!=0 || y!=0)) || ρ<nextfloat(zero(T))/(2*eps(T)^2)
49,351✔
516
        m::T = max(abs(x), abs(y))
9✔
517
        k = m==0 ? m : exponent(m)
17✔
518
        xk, yk = ldexp(x,-k), ldexp(y,-k)
18✔
519
        ρ = xk*xk + yk*yk
9✔
520
    end
521
    ρ, k
25,045✔
522
end
523

524
function sqrt(z::Complex)
12,298✔
525
    z = float(z)
14,043✔
526
    x, y = reim(z)
14,043✔
527
    if x==y==0
14,043✔
528
        return Complex(zero(x),y)
2,121✔
529
    end
530
    ρ, k::Int = ssqs(x, y)
11,922✔
531
    if isfinite(x) ρ=ldexp(abs(x),-k)+sqrt(ρ) end
22,735✔
532
    if isodd(k)
11,922✔
533
        k = div(k-1,2)
1✔
534
    else
535
        k = div(k,2)-1
11,921✔
536
        ρ += ρ
11,921✔
537
    end
538
    ρ = ldexp(sqrt(ρ),k) #sqrt((abs(z)+abs(x))/2) without over/underflow
22,684✔
539
    ξ = ρ
11,922✔
540
    η = y
11,922✔
541
    if ρ != 0
11,922✔
542
        if isfinite(η) η=(η/ρ)/2 end
11,922✔
543
        if x<0
11,922✔
544
            ξ = abs(η)
1,377✔
545
            η = copysign(ρ,y)
1,377✔
546
        end
547
    end
548
    Complex(ξ,η)
11,922✔
549
end
550

551
# function sqrt(z::Complex)
552
#     rz = float(real(z))
553
#     iz = float(imag(z))
554
#     r = sqrt((hypot(rz,iz)+abs(rz))/2)
555
#     if r == 0
556
#         return Complex(zero(iz), iz)
557
#     end
558
#     if rz >= 0
559
#         return Complex(r, iz/r/2)
560
#     end
561
#     return Complex(abs(iz)/r/2, copysign(r,iz))
562
# end
563

564
"""
565
    cis(x)
566

567
More efficient method for `exp(im*x)` by using Euler's formula: ``\\cos(x) + i \\sin(x) = \\exp(i x)``.
568

569
See also [`cispi`](@ref), [`sincos`](@ref), [`exp`](@ref), [`angle`](@ref).
570

571
# Examples
572
```jldoctest
573
julia> cis(π) ≈ -1
574
true
575
```
576
"""
577
function cis end
578
function cis(theta::Real)
11✔
579
    s, c = sincos(theta)
15,904✔
580
    Complex(c, s)
15,904✔
581
end
582

583
function cis(z::Complex)
12✔
584
    v = exp(-imag(z))
40✔
585
    s, c = sincos(real(z))
40✔
586
    Complex(v * c, v * s)
40✔
587
end
588

589
"""
590
    cispi(x)
591

592
More accurate method for `cis(pi*x)` (especially for large `x`).
593

594
See also [`cis`](@ref), [`sincospi`](@ref), [`exp`](@ref), [`angle`](@ref).
595

596
# Examples
597
```jldoctest
598
julia> cispi(10000)
599
1.0 + 0.0im
600

601
julia> cispi(0.25 + 1im)
602
0.030556854645954562 + 0.03055685464595456im
603
```
604

605
!!! compat "Julia 1.6"
606
    This function requires Julia 1.6 or later.
607
"""
608
function cispi end
609
cispi(theta::Real) = Complex(reverse(sincospi(theta))...)
22✔
610

611
function cispi(z::Complex)
8✔
612
    v = exp(-(pi*imag(z)))
8✔
613
    s, c = sincospi(real(z))
8✔
614
    Complex(v * c, v * s)
8✔
615
end
616

617
"""
618
    angle(z)
619

620
Compute the phase angle in radians of a complex number `z`.
621

622
Returns a number `-pi ≤ angle(z) ≤ pi`, and is thus discontinuous
623
along the negative real axis.
624

625
See also: [`atan`](@ref), [`cis`](@ref), [`rad2deg`](@ref).
626

627
# Examples
628
```jldoctest
629
julia> rad2deg(angle(1 + im))
630
45.0
631

632
julia> rad2deg(angle(1 - im))
633
-45.0
634

635
julia> rad2deg(angle(-1 + 1e-20im))
636
180.0
637

638
julia> rad2deg(angle(-1 - 1e-20im))
639
-180.0
640
```
641
"""
642
angle(z::Complex) = atan(imag(z), real(z))
25,656✔
643

644
function log(z::Complex)
13,120✔
645
    z = float(z)
13,123✔
646
    T = typeof(real(z))
13,123✔
647
    T1  = convert(T,5)/convert(T,4)
13,123✔
648
    T2  = convert(T,3)
13,123✔
649
    ln2 = log(convert(T,2))  #0.6931471805599453
13,123✔
650
    x, y = reim(z)
13,123✔
651
    ρ, k = ssqs(x,y)
13,123✔
652
    ax = abs(x)
13,123✔
653
    ay = abs(y)
13,123✔
654
    if ax < ay
13,123✔
655
        θ, β = ax, ay
1,571✔
656
    else
657
        θ, β = ay, ax
11,552✔
658
    end
659
    if k==0 && (0.5 < β*β) && (β <= T1 || ρ < T2)
17,831✔
660
        ρρ = log1p((β-1)*(β+1)+θ*θ)/2
7,682✔
661
    else
662
        ρρ = log(ρ)/2 + k*ln2
5,441✔
663
    end
664
    Complex(ρρ, angle(z))
13,123✔
665
end
666

667
# function log(z::Complex)
668
#     ar = abs(real(z))
669
#     ai = abs(imag(z))
670
#     if ar < ai
671
#         r = ar/ai
672
#         re = log(ai) + log1p(r*r)/2
673
#     else
674
#         if ar == 0
675
#             re = isnan(ai) ? ai : -inv(ar)
676
#         elseif isinf(ai)
677
#             re = oftype(ar,Inf)
678
#         else
679
#             r = ai/ar
680
#             re = log(ar) + log1p(r*r)/2
681
#         end
682
#     end
683
#     Complex(re, angle(z))
684
# end
685

686
function log10(z::Complex)
15✔
687
    a = log(z)
17✔
688
    a/log(oftype(real(a),10))
17✔
689
end
690
function log2(z::Complex)
15✔
691
    a = log(z)
17✔
692
    a/log(oftype(real(a),2))
17✔
693
end
694

695
function exp(z::Complex)
13,466✔
696
    zr, zi = reim(z)
16,044✔
697
    if isnan(zr)
16,054✔
698
        Complex(zr, zi==0 ? zi : zr)
4✔
699
    elseif !isfinite(zi)
16,050✔
700
        if zr == Inf
10✔
701
            Complex(-zr, oftype(zr,NaN))
3✔
702
        elseif zr == -Inf
7✔
703
            Complex(-zero(zr), copysign(zero(zi), zi))
3✔
704
        else
705
            Complex(oftype(zr,NaN), oftype(zi,NaN))
4✔
706
        end
707
    else
708
        er = exp(zr)
16,040✔
709
        if iszero(zi)
16,032✔
710
            Complex(er, zi)
631✔
711
        else
712
            s, c = sincos(zi)
15,409✔
713
            Complex(er * c, er * s)
15,409✔
714
        end
715
    end
716
end
717

718
function expm1(z::Complex{T}) where T<:Real
55✔
719
    Tf = float(T)
55✔
720
    zr,zi = reim(z)
55✔
721
    if isnan(zr)
55✔
722
        Complex(zr, zi==0 ? zi : zr)
4✔
723
    elseif !isfinite(zi)
51✔
724
        if zr == Inf
10✔
725
            Complex(-zr, oftype(zr,NaN))
3✔
726
        elseif zr == -Inf
7✔
727
            Complex(-one(zr), copysign(zero(zi), zi))
3✔
728
        else
729
            Complex(oftype(zr,NaN), oftype(zi,NaN))
4✔
730
        end
731
    else
732
        erm1 = expm1(zr)
41✔
733
        if zi == 0
41✔
734
            Complex(erm1, zi)
10✔
735
        else
736
            er = erm1+one(erm1)
31✔
737
            if isfinite(er)
31✔
738
                wr = erm1 - 2 * er * (sin(convert(Tf, 0.5) * zi))^2
29✔
739
                return Complex(wr, er * sin(zi))
29✔
740
            else
741
                s, c = sincos(zi)
2✔
742
                return Complex(er * c, er * s)
2✔
743
            end
744
        end
745
    end
746
end
747

748
function log1p(z::Complex{T}) where T
3,449✔
749
    zr,zi = reim(z)
3,450✔
750
    if isfinite(zr)
3,450✔
751
        isinf(zi) && return log(z)
3,444✔
752
        # This is based on a well-known trick for log1p of real z,
753
        # allegedly due to Kahan, only modified to handle real(u) <= 0
754
        # differently to avoid inaccuracy near z==-2 and for correct branch cut
755
        u = one(float(T)) + z
3,442✔
756
        u == 1 ? convert(typeof(u), z) : real(u) <= 0 ? log(u) : log(u)*(z/(u-1))
3,442✔
757
    elseif isnan(zr)
6✔
758
        Complex(zr, zr)
1✔
759
    elseif isfinite(zi)
5✔
760
        Complex(T(Inf), copysign(zr > 0 ? zero(T) : convert(T, pi), zi))
4✔
761
    else
762
        Complex(T(Inf), T(NaN))
1✔
763
    end
764
end
765

766
function exp2(z::Complex{T}) where T
23✔
767
    z = float(z)
18✔
768
    er = exp2(real(z))
23✔
769
    theta = imag(z) * log(convert(float(T), 2))
23✔
770
    s, c = sincos(theta)
23✔
771
    Complex(er * c, er * s)
23✔
772
end
773

774
function exp10(z::Complex{T}) where T
23✔
775
    z = float(z)
18✔
776
    er = exp10(real(z))
23✔
777
    theta = imag(z) * log(convert(float(T), 10))
23✔
778
    s, c = sincos(theta)
23✔
779
    Complex(er * c, er * s)
23✔
780
end
781

782
# _cpow helper function to avoid method ambiguity with ^(::Complex,::Real)
783
function _cpow(z::Union{T,Complex{T}}, p::Union{T,Complex{T}}) where T
22,946✔
784
    z = float(z)
22,949✔
785
    p = float(p)
22,949✔
786
    Tf = float(T)
22,949✔
787
    if isreal(p)
22,949✔
788
        pᵣ = real(p)
10,581✔
789
        if isinteger(pᵣ) && abs(pᵣ) < typemax(Int32)
10,581✔
790
            # |p| < typemax(Int32) serves two purposes: it prevents overflow
791
            # when converting p to Int, and it also turns out to be roughly
792
            # the crossover point for exp(p*log(z)) or similar to be faster.
793
            if iszero(pᵣ) # fix signs of imaginary part for z^0
4,563✔
794
                zer = flipsign(copysign(zero(Tf),pᵣ), imag(z))
2,179✔
795
                return Complex(one(Tf), zer)
2,179✔
796
            end
797
            ip = convert(Int, pᵣ)
2,384✔
798
            if isreal(z)
2,384✔
799
                zᵣ = real(z)
1,605✔
800
                if ip < 0
1,605✔
801
                    iszero(z) && return Complex(Tf(NaN),Tf(NaN))
158✔
802
                    re = power_by_squaring(inv(zᵣ), -ip)
156✔
803
                    im = -imag(z)
156✔
804
                else
805
                    re = power_by_squaring(zᵣ, ip)
1,447✔
806
                    im = imag(z)
1,447✔
807
                end
808
                # slightly tricky to get the correct sign of zero imag. part
809
                return Complex(re, ifelse(iseven(ip) & signbit(zᵣ), -im, im))
1,603✔
810
            else
811
                return ip < 0 ? power_by_squaring(inv(z), -ip) : power_by_squaring(z, ip)
779✔
812
            end
813
        elseif isreal(z)
6,018✔
814
            # (note: if both z and p are complex with ±0.0 imaginary parts,
815
            #  the sign of the ±0.0 imaginary part of the result is ambiguous)
816
            if iszero(real(z))
2,880✔
817
                return pᵣ > 0 ? complex(z) : Complex(Tf(NaN),Tf(NaN)) # 0 or NaN+NaN*im
3✔
818
            elseif real(z) > 0
2,877✔
819
                return Complex(real(z)^pᵣ, z isa Real ? ifelse(real(z) < 1, -imag(p), imag(p)) : flipsign(imag(z), pᵣ))
2,573✔
820
            else
821
                zᵣ = real(z)
304✔
822
                rᵖ = (-zᵣ)^pᵣ
304✔
823
                if isfinite(pᵣ)
304✔
824
                    # figuring out the sign of 0.0 when p is a complex number
825
                    # with zero imaginary part and integer/2 real part could be
826
                    # improved here, but it's not clear if it's worth it…
827
                    return rᵖ * complex(cospi(pᵣ), flipsign(sinpi(pᵣ),imag(z)))
302✔
828
                else
829
                    iszero(rᵖ) && return zero(Complex{Tf}) # no way to get correct signs of 0.0
2✔
830
                    return Complex(Tf(NaN),Tf(NaN)) # non-finite phase angle or NaN input
2✔
831
                end
832
            end
833
        else
834
            rᵖ = abs(z)^pᵣ
3,162✔
835
            ϕ = pᵣ*angle(z)
3,138✔
836
        end
837
    elseif isreal(z)
12,368✔
838
        iszero(z) && return real(p) > 0 ? complex(z) : Complex(Tf(NaN),Tf(NaN)) # 0 or NaN+NaN*im
4,862✔
839
        zᵣ = real(z)
4,860✔
840
        pᵣ, pᵢ = reim(p)
4,860✔
841
        if zᵣ > 0
4,860✔
842
            rᵖ = zᵣ^pᵣ
4,138✔
843
            ϕ = pᵢ*log(zᵣ)
4,138✔
844
        else
845
            r = -zᵣ
722✔
846
            θ = copysign(Tf(π),imag(z))
722✔
847
            rᵖ = r^pᵣ * exp(-pᵢ*θ)
722✔
848
            ϕ = pᵣ*θ + pᵢ*log(r)
722✔
849
        end
850
    else
851
        pᵣ, pᵢ = reim(p)
7,506✔
852
        r = abs(z)
7,509✔
853
        θ = angle(z)
7,506✔
854
        rᵖ = r^pᵣ * exp(-pᵢ*θ)
7,506✔
855
        ϕ = pᵣ*θ + pᵢ*log(r)
7,506✔
856
    end
857

858
    if isfinite(ϕ)
15,504✔
859
        return rᵖ * cis(ϕ)
15,493✔
860
    else
861
        iszero(rᵖ) && return zero(Complex{Tf}) # no way to get correct signs of 0.0
11✔
862
        return Complex(Tf(NaN),Tf(NaN)) # non-finite phase angle or NaN input
11✔
863
    end
864
end
865
^(z::Complex{T}, p::Complex{T}) where T<:Real = _cpow(z, p)
9,014✔
866
^(z::Complex{T}, p::T) where T<:Real = _cpow(z, p)
7,178✔
867
^(z::T, p::Complex{T}) where T<:Real = _cpow(z, p)
6,757✔
868

869
^(z::Complex, n::Bool) = n ? z : one(z)
3✔
870
^(z::Complex, n::Integer) = z^Complex(n)
3✔
871

872
^(z::Complex{<:AbstractFloat}, n::Bool) = n ? z : one(z)  # to resolve ambiguity
2,058✔
873
^(z::Complex{<:Integer}, n::Bool) = n ? z : one(z)        # to resolve ambiguity
4,090✔
874

875
^(z::Complex{<:AbstractFloat}, n::Integer) =
1,315✔
876
    n>=0 ? power_by_squaring(z,n) : power_by_squaring(inv(z),-n)
877
^(z::Complex{<:Integer}, n::Integer) = power_by_squaring(z,n) # DomainError for n<0
32✔
878

879
function ^(z::Complex{T}, p::S) where {T<:Real,S<:Real}
21✔
880
    P = promote_type(T,S)
1,197✔
881
    return Complex{P}(z) ^ P(p)
1,197✔
882
end
883
function ^(z::T, p::Complex{S}) where {T<:Real,S<:Real}
93✔
884
    P = promote_type(T,S)
6,733✔
885
    return P(z) ^ Complex{P}(p)
6,733✔
886
end
887

888
function sin(z::Complex{T}) where T
2,256✔
889
    F = float(T)
2,256✔
890
    zr, zi = reim(z)
2,256✔
891
    if zr == 0
2,256✔
892
        Complex(F(zr), sinh(zi))
684✔
893
    elseif !isfinite(zr)
1,572✔
894
        if zi == 0 || isinf(zi)
108✔
895
            Complex(F(NaN), F(zi))
36✔
896
        else
897
            Complex(F(NaN), F(NaN))
27✔
898
        end
899
    else
900
        s, c = sincos(zr)
1,509✔
901
        Complex(s * cosh(zi), c * sinh(zi))
1,509✔
902
    end
903
end
904

905

906
function cos(z::Complex{T}) where T
297✔
907
    F = float(T)
297✔
908
    zr, zi = reim(z)
297✔
909
    if zr == 0
297✔
910
        Complex(cosh(zi), isnan(zi) ? F(zr) : -flipsign(F(zr),zi))
54✔
911
    elseif !isfinite(zr)
243✔
912
        if zi == 0
57✔
913
            Complex(F(NaN), isnan(zr) ? zero(F) : -flipsign(F(zi),zr))
12✔
914
        elseif isinf(zi)
45✔
915
            Complex(F(Inf), F(NaN))
18✔
916
        else
917
            Complex(F(NaN), F(NaN))
27✔
918
        end
919
    else
920
        s, c = sincos(zr)
186✔
921
        Complex(c * cosh(zi), -s * sinh(zi))
186✔
922
    end
923
end
924

925

926
function tan(z::Complex)
37✔
927
    zr, zi = reim(z)
108✔
928
    w = tanh(Complex(-zi, zr))
108✔
929
    Complex(imag(w), -real(w))
107✔
930
end
931

932
function asin(z::Complex)
184✔
933
    zr, zi = reim(z)
184✔
934
    if isinf(zr) && isinf(zi)
184✔
935
        return Complex(copysign(oftype(zr,pi)/4, zr),zi)
8✔
936
    elseif isnan(zi) && isinf(zr)
176✔
937
        return Complex(zi, oftype(zr, Inf))
4✔
938
    end
939
    ξ = zr == 0       ? zr :
332✔
940
        !isfinite(zr) ? oftype(zr,pi)/2 * sign(zr) :
941
        atan(zr, real(sqrt(1-z)*sqrt(1+z)))
942
    η = asinh(copysign(imag(sqrt(conj(1-z))*sqrt(1+z)), imag(z)))
172✔
943
    Complex(ξ,η)
172✔
944
end
945

946
function acos(z::Complex)
114✔
947
    z = float(z)
114✔
948
    zr, zi = reim(z)
114✔
949
    if isnan(zr)
114✔
950
        if isinf(zi) return Complex(zr, -zi)
5✔
951
        else         return Complex(zr, zr) end
3✔
952
    elseif isnan(zi)
109✔
953
        if isinf(zr) return Complex(zi, abs(zr))
5✔
954
        elseif zr==0 return Complex(oftype(zr,pi)/2, zi)
3✔
955
        else         return Complex(zi, zi) end
1✔
956
    elseif zr==zi==0
104✔
957
        return Complex(oftype(zr,pi)/2, -zi)
5✔
958
    elseif zr==Inf && zi===0.0
99✔
959
        return Complex(zi, -zr)
1✔
960
    elseif zr==-Inf && zi===-0.0
98✔
961
        return Complex(oftype(zi,pi), -zr)
1✔
962
    end
963
    ξ = 2*atan(real(sqrt(1-z)), real(sqrt(1+z)))
97✔
964
    η = asinh(imag(sqrt(conj(1+z))*sqrt(1-z)))
97✔
965
    if isinf(zr) && isinf(zi) ξ -= oftype(η,pi)/4 * sign(zr) end
97✔
966
    Complex(ξ,η)
97✔
967
end
968

969
function atan(z::Complex)
52✔
970
    w = atanh(Complex(-imag(z),real(z)))
145✔
971
    Complex(imag(w),-real(w))
145✔
972
end
973

974
function sinh(z::Complex)
23✔
975
    zr, zi = reim(z)
2,129✔
976
    w = sin(Complex(zi, zr))
2,129✔
977
    Complex(imag(w),real(w))
2,129✔
978
end
979

980
function cosh(z::Complex)
23✔
981
    zr, zi = reim(z)
174✔
982
    cos(Complex(zi,-zr))
174✔
983
end
984

985
function tanh(z::Complex{T}) where T
156✔
986
    z = float(z)
156✔
987
    Tf = float(T)
156✔
988
    Ω = prevfloat(typemax(Tf))
156✔
989
    ξ, η = reim(z)
156✔
990
    if isnan(ξ) && η==0 return Complex(ξ, η) end
156✔
991
    if 4*abs(ξ) > asinh(Ω) #Overflow?
152✔
992
        Complex(copysign(one(Tf),ξ),
28✔
993
                copysign(zero(Tf),η*(isfinite(η) ? sin(2*abs(η)) : one(η))))
994
    else
995
        t = tan(η)
124✔
996
        β = 1+t*t #sec(η)^2
120✔
997
        s = sinh(ξ)
133✔
998
        ρ = sqrt(1 + s*s) #cosh(ξ)
120✔
999
        if isinf(t)
120✔
1000
            Complex(ρ/s,1/t)
×
1001
        else
1002
            Complex(β*ρ*s,t)/(1+β*s*s)
120✔
1003
        end
1004
    end
1005
end
1006

1007
function asinh(z::Complex)
40✔
1008
    w = asin(Complex(-imag(z),real(z)))
127✔
1009
    Complex(imag(w),-real(w))
127✔
1010
end
1011

1012
function acosh(z::Complex)
107✔
1013
    zr, zi = reim(z)
107✔
1014
    if isnan(zr) || isnan(zi)
211✔
1015
        if isinf(zr) || isinf(zi)
10✔
1016
            return Complex(oftype(zr, Inf), oftype(zi, NaN))
4✔
1017
        else
1018
            return Complex(oftype(zr, NaN), oftype(zi, NaN))
2✔
1019
        end
1020
    elseif zr==-Inf && zi===-0.0 #Edge case is wrong - WHY?
101✔
1021
        return Complex(oftype(zr,Inf), oftype(zi, -pi))
1✔
1022
    end
1023
    ξ = asinh(real(sqrt(conj(z-1))*sqrt(z+1)))
100✔
1024
    η = 2*atan(imag(sqrt(z-1)),real(sqrt(z+1)))
100✔
1025
    if isinf(zr) && isinf(zi)
100✔
1026
        η -= oftype(η,pi)/4 * sign(zi) * sign(zr)
4✔
1027
    end
1028
    Complex(ξ, η)
100✔
1029
end
1030

1031
function atanh(z::Complex{T}) where T
1,942✔
1032
    z = float(z)
1,942✔
1033
    Tf = float(T)
1,942✔
1034
    x, y = reim(z)
1,942✔
1035
    ax = abs(x)
1,942✔
1036
    ay = abs(y)
1,942✔
1037
    θ = sqrt(floatmax(Tf))/4
1,942✔
1038
    if ax > θ || ay > θ #Prevent overflow
3,855✔
1039
        if isnan(y)
49✔
1040
            if isinf(x)
5✔
1041
                return Complex(copysign(zero(x),x), y)
4✔
1042
            else
1043
                return Complex(real(inv(z)), y)
1✔
1044
            end
1045
        end
1046
        if isinf(y)
44✔
1047
            return Complex(copysign(zero(x),x), copysign(oftype(y,pi)/2, y))
28✔
1048
        end
1049
        return Complex(real(inv(z)), copysign(oftype(y,pi)/2, y))
16✔
1050
    end
1051
    β = copysign(one(Tf), x)
1,893✔
1052
    z *= β
1,893✔
1053
    x, y = reim(z)
1,893✔
1054
    if x == 1
1,893✔
1055
        if y == 0
13✔
1056
            ξ = oftype(x, Inf)
5✔
1057
            η = y
5✔
1058
        else
1059
            ξ = log(sqrt(sqrt(muladd(y, y, 4)))/sqrt(ay))
8✔
1060
            η = copysign(oftype(y,pi)/2 + atan(ay/2), y)/2
8✔
1061
        end
1062
    else #Normal case
1063
        ysq = ay^2
1,880✔
1064
        if x == 0
1,880✔
1065
            ξ = x
68✔
1066
        else
1067
            ξ = log1p(4x/(muladd(1-x, 1-x, ysq)))/4
1,812✔
1068
        end
1069
        η = angle(Complex((1-x)*(1+x)-ysq, 2y))/2
1,880✔
1070
    end
1071
    β * Complex(ξ, η)
1,893✔
1072
end
1073

1074
#Rounding complex numbers
1075
#Requires two different RoundingModes for the real and imaginary components
1076
"""
1077
    round(z::Complex[, RoundingModeReal, [RoundingModeImaginary]])
1078
    round(z::Complex[, RoundingModeReal, [RoundingModeImaginary]]; digits=0, base=10)
1079
    round(z::Complex[, RoundingModeReal, [RoundingModeImaginary]]; sigdigits, base=10)
1080

1081
Return the nearest integral value of the same type as the complex-valued `z` to `z`,
1082
breaking ties using the specified [`RoundingMode`](@ref)s. The first
1083
[`RoundingMode`](@ref) is used for rounding the real components while the
1084
second is used for rounding the imaginary components.
1085

1086

1087
`RoundingModeReal` and `RoundingModeImaginary` default to [`RoundNearest`](@ref),
1088
which rounds to the nearest integer, with ties (fractional values of 0.5)
1089
being rounded to the nearest even integer.
1090

1091
# Examples
1092
```jldoctest
1093
julia> round(3.14 + 4.5im)
1094
3.0 + 4.0im
1095

1096
julia> round(3.14 + 4.5im, RoundUp, RoundNearestTiesUp)
1097
4.0 + 5.0im
1098

1099
julia> round(3.14159 + 4.512im; digits = 1)
1100
3.1 + 4.5im
1101

1102
julia> round(3.14159 + 4.512im; sigdigits = 3)
1103
3.14 + 4.51im
1104
```
1105
"""
1106
function round(z::Complex, rr::RoundingMode=RoundNearest, ri::RoundingMode=rr; kwargs...)
59✔
1107
    Complex(round(real(z), rr; kwargs...),
23✔
1108
            round(imag(z), ri; kwargs...))
1109
end
1110

1111

1112
float(z::Complex{<:AbstractFloat}) = z
772,609✔
1113
float(z::Complex) = Complex(float(real(z)), float(imag(z)))
234,836✔
1114

1115
big(::Type{Complex{T}}) where {T<:Real} = Complex{big(T)}
9✔
1116
big(z::Complex{T}) where {T<:Real} = Complex{big(T)}(z)
832✔
1117

1118
## Array operations on complex numbers ##
1119
"""
1120
    complex(A::AbstractArray)
1121

1122
Return an array containing the complex analog of each entry in array `A`.
1123

1124
Equivalent to `complex.(A)`, except that the return value may share memory with all or
1125
part of `A` in accordance with the behavior of `convert(T, A)` given output type `T`.
1126

1127
# Examples
1128
```jldoctest
1129
julia> complex([1, 2, 3])
1130
3-element Vector{Complex{Int64}}:
1131
 1 + 0im
1132
 2 + 0im
1133
 3 + 0im
1134
```
1135
"""
1136
complex(A::AbstractArray{<:Complex}) = A
1,248✔
1137

1138
function complex(A::AbstractArray{T}) where T
90✔
1139
    if !isconcretetype(T)
471✔
1140
        error("`complex` not defined on abstractly-typed arrays; please convert to a more specific type")
1✔
1141
    end
1142
    convert(AbstractArray{typeof(complex(zero(T)))}, A)
520✔
1143
end
1144

1145
## Machine epsilon for complex ##
1146

1147
eps(z::Complex{<:AbstractFloat}) = hypot(eps(real(z)), eps(imag(z)))
3✔
1148

1149
eps(::Type{Complex{T}}) where {T<:AbstractFloat} = sqrt(2*one(T))*eps(T)
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc