Skip to content

Commit 23a8212

Browse files
bzinbergsethaxen
andauthored
Remove unnecessary type conversion inside qrotation (#38)
* Add failing regression test for #8 (comment) * Remove unnecessary type conversion. Addresses #8. * minor: simplify the "test this doesn't crash" logic Co-authored-by: Seth Axen <[email protected]> Co-authored-by: Seth Axen <[email protected]>
1 parent 5a2bb75 commit 23a8212

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Quaternion.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,8 @@ function qrotation(axis::Vector{T}, theta) where {T <: Real}
208208
error("Must be a 3-vector")
209209
end
210210
u = normalize(axis)
211-
thetaT = convert(eltype(u), theta)
212-
s = sin(thetaT / 2)
213-
Quaternion(cos(thetaT / 2), s * u[1], s * u[2], s * u[3], true)
211+
s = sin(theta / 2)
212+
Quaternion(cos(theta / 2), s * u[1], s * u[2], s * u[3], true)
214213
end
215214

216215
# Variant of the above where norm(rotvec) encodes theta

test/test_Quaternion.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ let # test rotations
5858
@test angle(qrotation([0, 1, 0], pi / 4)) pi / 4
5959
@test angle(qrotation([0, 0, 1], pi / 2)) pi / 2
6060

61+
# Regression test for
62+
# https://github.com/JuliaGeometry/Quaternions.jl/issues/8#issuecomment-610640094
63+
struct MyReal <: Real
64+
val::Real
65+
end
66+
Base.:(/)(a::MyReal, b::Real) = a.val / b
67+
# this used to throw an error
68+
qrotation([1, 0, 0], MyReal(1.5))
69+
6170
let # test numerical stability of angle
6271
ax = randn(3)
6372
for θ in [1e-9, π - 1e-9]

0 commit comments

Comments
 (0)