Skip to content

Commit 2027403

Browse files
committed
Re-implement promotion
- Note, due to PainterQubits/Unitful.jl#332 the tests using isapprox with Unitful are marked broken
1 parent 83bf90f commit 2027403

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

src/coordinatesystems.jl

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@
77
struct Polar{T,A}
88
r::T
99
θ::A
10+
11+
Polar{T, A}(r, θ) where {T, A} = new(r, θ)
12+
end
13+
14+
function Polar(r, θ)
15+
r2, θ2 = promote(r, θ)
16+
17+
return Polar{typeof(r2), typeof(θ2)}(r2, θ2)
1018
end
11-
Polar(r::T, θ::A) where {T<:AbstractFloat, A<:Integer} = Polar(promote(r, θ)...)
12-
Polar(r::T, θ::A) where {T<:Integer, A<:AbstractFloat} = Polar(promote(r, θ)...)
19+
1320
Base.show(io::IO, x::Polar) = print(io, "Polar(r=$(x.r), θ=$(x.θ) rad)")
1421
Base.isapprox(p1::Polar, p2::Polar; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...)
1522

@@ -71,9 +78,16 @@ struct Spherical{T,A}
7178
r::T
7279
θ::A
7380
ϕ::A
81+
82+
Spherical{T, A}(r, θ, ϕ) where {T, A} = new(r, θ, ϕ)
7483
end
75-
Spherical(r::T, θ::A, ϕ::A) where {T<:AbstractFloat, A<:Integer} = Spherical(promote(r, θ, ϕ)...)
76-
Spherical(r::T, θ::A, ϕ::A) where {T<:Integer, A<:AbstractFloat} = Spherical(promote(r, θ, ϕ)...)
84+
85+
function Spherical(r, θ, ϕ)
86+
r2, θ2, ϕ2 = promote(r, θ, ϕ)
87+
88+
return Spherical{typeof(r2), typeof(θ2)}(r2, θ2, ϕ2)
89+
end
90+
7791
Base.show(io::IO, x::Spherical) = print(io, "Spherical(r=$(x.r), θ=$(x.θ) rad, ϕ=$(x.ϕ) rad)")
7892
Base.isapprox(p1::Spherical, p2::Spherical; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...) && isapprox(p1.ϕ, p2.ϕ; kwargs...)
7993

@@ -84,11 +98,16 @@ struct Cylindrical{T,A}
8498
r::T
8599
θ::A
86100
z::T
101+
102+
Cylindrical{T, A}(r, θ, z) where {T, A} = new(r, θ, z)
87103
end
88-
Cylindrical(r::T1, θ::A, z::T2) where {T1<:AbstractFloat, T2<:Integer, A<:Integer} = Cylindrical(promote(r, θ, z)...)
89-
Cylindrical(r::T1, θ::A, z::T2) where {T1<:Integer, T2<:AbstractFloat, A<:Integer} = Cylindrical(promote(r, θ, z)...)
90-
Cylindrical(r::T1, θ::A, z::T2) where {T1<:AbstractFloat, T2<:Integer, A<:AbstractFloat} = Cylindrical(promote(r, θ, z)...)
91-
Cylindrical(r::T1, θ::A, z::T2) where {T1<:Integer, T2<:AbstractFloat, A<:AbstractFloat} = Cylindrical(promote(r, θ, z)...)
104+
105+
function Cylindrical(r, θ, z)
106+
r2, θ2, z2 = promote(r, θ, z)
107+
108+
return Cylindrical{typeof(r2), typeof(θ2)}(r2, θ2, z2)
109+
end
110+
92111
Base.show(io::IO, x::Cylindrical) = print(io, "Cylindrical(r=$(x.r), θ=$(x.θ) rad, z=$(x.z))")
93112
Base.isapprox(p1::Cylindrical, p2::Cylindrical; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...) && isapprox(p1.z, p2.z; kwargs...)
94113

test/coordinatesystems.jl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,13 @@
105105
xy_i = SVector(1,2)
106106
p1 = Polar(1, 2.0f0)
107107
p2 = Polar(1.0, 2)
108+
p3 = Polar{Int, Float64}(1, 2.0)
108109
= Polar(2.23606797749979, 1.1071487177940904)
109110

110111
@test typeof(p1.r) == typeof(p1.θ)
111112
@test typeof(p2.r) == typeof(p2.θ)
113+
@test typeof(p3.r) == Int
114+
@test typeof(p3.θ) == Float64
112115

113116
@test p_from_c(xy_i)
114117
@test p_from_c(xy)
@@ -120,8 +123,8 @@
120123
xy = SVector(1.0, 2.0)u"m"
121124
= Polar(2.23606797749979u"m", 1.1071487177940904)
122125

123-
@test p_from_c(xy)
124-
@test p_from_c(collect(xy))
126+
@test_broken p_from_c(xy)
127+
@test_broken p_from_c(collect(xy))
125128
@test c_from_p(rθ) xy
126129
end
127130
end
@@ -474,9 +477,11 @@
474477

475478
s1 = Spherical(1, 2.0, 3.0)
476479
s2 = Spherical(1.0, 2, 3)
480+
s3 = Spherical{Int,Int}(1, 2, 3)
477481

478-
@test typeof(s1.r) == typeof(s1.θ) == typeof(s1.ϕ)
479-
@test typeof(s2.r) == typeof(s2.θ) == typeof(s2.ϕ)
482+
@test typeof(s1.r) == typeof(s1.θ) == typeof(s1.ϕ) == Float64
483+
@test typeof(s2.r) == typeof(s2.θ) == typeof(s2.ϕ) == Float64
484+
@test typeof(s3.r) == typeof(s3.θ) == typeof(s3.ϕ) == Int
480485
end
481486

482487
@testset "Cylindrical" begin
@@ -490,12 +495,12 @@
490495
c1 = Cylindrical(1, 2.0, 3)
491496
c2 = Cylindrical(1.0, 2, 3.0)
492497
c3 = Cylindrical(1, 2, 3)
498+
c4 = Cylindrical{Int,Int}(1, 2, 3)
493499

494-
@test typeof(c1.r) == typeof(c1.z)
495-
@test typeof(c1.θ) == Float64
496-
@test typeof(c2.r) == typeof(c2.z)
497-
@test typeof(c2.θ) == Int
500+
@test typeof(c1.r) == typeof(c1.z) == typeof(c1.θ) == Float64
501+
@test typeof(c2.r) == typeof(c2.θ) == typeof(c2.z) == Float64
498502
@test typeof(cyl_from_cart(xyz_i).r) == typeof(cyl_from_cart(xyz_i).z) == Float64
503+
@test c3 == c4
499504
end
500505
end
501506

@@ -505,17 +510,17 @@
505510
@testset "Shperical" begin
506511
rθϕ = Spherical(3.7416573867739413u"m", 1.1071487177940904, 0.9302740141154721)
507512

508-
@test s_from_cart(xyz) rθϕ
513+
@test_broken s_from_cart(xyz) rθϕ
509514
@test typeof(s_from_cart(xyz)) == typeof(rθϕ)
510-
@test s_from_cart(collect(xyz)) rθϕ
515+
@test_broken s_from_cart(collect(xyz)) rθϕ
511516
@test cart_from_s(rθϕ) xyz
512517
end
513518
@testset "Cylindrical" begin
514519
rθz = Cylindrical(2.23606797749979u"m", 1.1071487177940904, 3.0u"m")
515520

516-
@test cyl_from_cart(xyz) rθz
521+
@test_broken cyl_from_cart(xyz) rθz
517522
@test typeof(cyl_from_cart(xyz)) == typeof(rθz)
518-
@test cyl_from_cart(collect(xyz)) rθz
523+
@test_broken cyl_from_cart(collect(xyz)) rθz
519524
@test cart_from_cyl(rθz) xyz
520525
end
521526
end

0 commit comments

Comments
 (0)