Skip to content

Commit

Permalink
Add support for big (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
sostock authored Dec 31, 2024
1 parent 3c5ec20 commit 8d90bcb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Unitful

import Base: ==, <, <=, +, -, *, /, //, ^, isequal
import Base: show, convert
import Base: abs, abs2, angle, float, fma, muladd, inv, sqrt, cbrt
import Base: abs, abs2, angle, big, float, fma, muladd, inv, sqrt, cbrt
import Base: min, max, floor, ceil, real, imag, conj
import Base: complex, widen, reim # handled in complex.jl
import Base: exp, exp10, exp2, expm1, log, log10, log1p, log2
Expand Down
2 changes: 2 additions & 0 deletions src/logarithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function Base.float(x::Level{L,S}) where {L,S}
v = float(x.val)
return Level{L,S,typeof(v)}(v)
end
big(x::Level{L,S}) where {L,S} = Level{L,S}(big(x.val))
logunit(x::Level{L,S}) where {L,S} = MixedUnits{Level{L,S}}()
logunit(x::Type{T}) where {L,S,T<:Level{L,S}} = MixedUnits{Level{L,S}}()

Expand All @@ -25,6 +26,7 @@ function Base.float(x::Gain{L,S}) where {L,S}
v = float(x.val)
return Gain{L,S,typeof(v)}(v)
end
big(x::Gain{L,S}) where {L,S} = Gain{L,S}(big(x.val))
logunit(x::Gain{L,S}) where {L,S} = MixedUnits{Gain{L,S}}()
logunit(x::Type{T}) where {L,S, T<:Gain{L,S}} = MixedUnits{Gain{L,S}}()
abbr(x::Gain{L}) where {L} = abbr(L())
Expand Down
3 changes: 3 additions & 0 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ Returns a `Quantity` with the same units.
"""
Rational(x::AbstractQuantity) = Quantity(Rational(x.val), unit(x))

big(x::AbstractQuantity{T,D,U}) where {T,D,U} = Quantity{big(T),D,U}(big(x.val))
big(::Type{<:AbstractQuantity{T,D,U}}) where {T,D,U} = Quantity{big(T),D,U}

Base.hastypemax(::Type{<:AbstractQuantity{T}}) where {T} = Base.hastypemax(T)

typemin(::Type{<:AbstractQuantity{T,D,U}}) where {T,D,U} = typemin(T)*U()
Expand Down
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ end
@test typeof(convert(typeof(0.0°), 90°)) == typeof(0.0°)
@test (3.0+4.0im)*V == (3+4im)*V
@test im*V == Complex(0,1)*V
for x = (3, 3.0, 3//1, 1+2im, 1.0+2.0im, (1//2)+(3//4)im)
for u = (m, °C)
@test @inferred(big(x*u)) == big(x)*u
@test typeof(big(x*u)) == typeof(big(x)*u)
@test big(typeof(x*u)) == typeof(big(x)*u)
end
q = Quantity{typeof(x),NoDims,typeof(NoUnits)}(x)
big_q = Quantity{big(typeof(x)),NoDims,typeof(NoUnits)}(big(x))
@test @inferred(big(q)) == big_q
@test typeof(big(q)) == typeof(big_q)
@test big(typeof(q)) == typeof(big_q)
end
end
@testset ">> Intra-unit conversion" begin
# an essentially no-op uconvert should not disturb numeric type
Expand Down Expand Up @@ -1846,6 +1858,14 @@ end
@test float(3dB) == 3.0dB
@test float(@dB 3V/1V) === @dB 3.0V/1V

for x = (20, 20.0, 20//1)
for u = (dB, dB/m, dBV, dBV/m)
@test @inferred(big(x*u)) == big(x)*u
@test typeof(big(x*u)) == typeof(big(x)*u)
@test big(typeof(x*u)) == typeof(big(x)*u)
end
end

@test uconvert(V, (@dB 3V/2.14V)) === 3V
@test uconvert(V, (@dB 3V/1V)) === 3V
@test uconvert(mW/Hz, 0dBm/Hz) == 1mW/Hz
Expand Down

0 comments on commit 8d90bcb

Please sign in to comment.