mirror of
https://github.com/tgorordo/WignerSymbols.jl.git
synced 2026-06-05 15:42:15 -07:00
update HalfInteger
This commit is contained in:
parent
8854cfbb97
commit
39bbd0ced8
2 changed files with 38 additions and 3 deletions
|
|
@ -21,11 +21,18 @@ function Base.convert(::Type{HalfInteger}, r::Rational)
|
|||
elseif r.den == 2
|
||||
return HalfInteger(r.num)
|
||||
else
|
||||
throw(InexactError())
|
||||
throw(InexactError(:HalfInteger, HalfInteger, r))
|
||||
end
|
||||
end
|
||||
Base.convert(::Type{HalfInteger}, r::Real) = convert(HalfInteger, convert(Rational, r))
|
||||
Base.convert(T::Type{<:Integer}, s::HalfInteger) = iseven(s.num) ? convert(T, s.num>>1) : throw(InexactError())
|
||||
function Base.convert(::Type{HalfInteger}, r::Real)
|
||||
num = 2*r
|
||||
if isinteger(num)
|
||||
return HalfInteger(convert(Int, num))
|
||||
else
|
||||
throw(InexactError(:HalfInteger, HalfInteger, r))
|
||||
end
|
||||
end
|
||||
Base.convert(T::Type{<:Integer}, s::HalfInteger) = iseven(s.num) ? convert(T, s.num>>1) : throw(InexactError(Symbol(T), T, s))
|
||||
Base.convert(T::Type{<:Rational}, s::HalfInteger) = convert(T, s.num//2)
|
||||
Base.convert(T::Type{<:Real}, s::HalfInteger) = convert(T, s.num/2)
|
||||
Base.convert(::Type{HalfInteger}, s::HalfInteger) = s
|
||||
|
|
|
|||
|
|
@ -2,9 +2,37 @@ using Test
|
|||
using WignerSymbols
|
||||
using LinearAlgebra
|
||||
|
||||
using WignerSymbols: HalfInteger
|
||||
smalljlist = 0:1//2:10
|
||||
largejlist = 0:1//2:1000
|
||||
|
||||
@testset "HalfInteger" begin
|
||||
@test convert(HalfInteger, 2) == HalfInteger(4)
|
||||
@test convert(HalfInteger, 1//2) == HalfInteger(1)
|
||||
@test convert(HalfInteger, 1.5) == HalfInteger(3)
|
||||
@test_throws InexactError convert(HalfInteger, 1//3)
|
||||
@test_throws InexactError convert(HalfInteger, 0.6)
|
||||
@test convert(HalfInteger, 2) == 2
|
||||
@test convert(HalfInteger, 1//2) == 1//2
|
||||
@test convert(HalfInteger, 1.5) == 1.5
|
||||
@test_throws InexactError convert(Integer, HalfInteger(1))
|
||||
a = HalfInteger(4)
|
||||
b = HalfInteger(3)
|
||||
@test a + b == 2 + 3//2
|
||||
@test a - b == 2 - 3//2
|
||||
@test zero(a) == 0
|
||||
@test one(a) == 1
|
||||
@test a > b
|
||||
@test b < a
|
||||
@test b <= a
|
||||
@test a >= b
|
||||
@test a == a
|
||||
@test a != b
|
||||
@test hash(a) == hash(2)
|
||||
@test hash(b) == hash(1.5)
|
||||
@test hash(b) == hash(3//2)
|
||||
end
|
||||
|
||||
@testset "triangle coefficient" begin
|
||||
for j1 in smalljlist, j2 in smalljlist
|
||||
for j3 = abs(j1-j2):(j1+j2)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue