-
Notifications
You must be signed in to change notification settings - Fork 24
Add AbstractRealQuantity #85
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
f4b8112
[WIP] Trying to add RealQuantity
MilesCranmer af8815f
`isapprox` should throw error on mismatched dimensions
MilesCranmer c811845
Fix isapprox usage in tests
MilesCranmer 54bb106
Generalize comparison operators
MilesCranmer adee292
Clean up more ambiguities
MilesCranmer d4cda51
Get simple complex operations working for RealQuantity
MilesCranmer 9424a22
More ambiguities found by Aqua
MilesCranmer 8f844a2
Get majority of tests working with RealQuantity
MilesCranmer 27247a2
Fix scitypes test
MilesCranmer 7bfbe2e
Fix conversion to base type
MilesCranmer 0e41261
Change back to `eltype`
MilesCranmer 4667128
Refactor some promotion rules
MilesCranmer 745cd9e
No need to redefine `identity`
MilesCranmer 79bfd78
Fix ambiguity in `div`
MilesCranmer 037b6f2
Refactor promotion rules
MilesCranmer e90d046
Test all ambiguities
MilesCranmer 814fa7e
Help coveralls see coverage
MilesCranmer d84bb6a
Reduce unnecessary methods
MilesCranmer 664b188
Improve coverage
MilesCranmer c643ea6
Back to `Quantity` as default
MilesCranmer 72d19eb
Cleanup docs
MilesCranmer 75fa9e0
Fix coverage
MilesCranmer bc1a90b
Export `AbstractRealQuantity`
MilesCranmer 2694747
Proper definition of `mod` and `rem`
MilesCranmer 8c24478
Clean up conversion to numbers
MilesCranmer 807bd21
Refactor div definition
MilesCranmer 7d9aad8
Add unittest for ranges
MilesCranmer 8eb83f7
Use `===` instead of `==`
MilesCranmer 88ea317
Refactor disambiguities
MilesCranmer 5b2b067
Split up `rem`/`mod` definitions
MilesCranmer cd339b6
Reduce invalidations
MilesCranmer 4786b65
Expand measurements testing
MilesCranmer 466c907
Clean up method invalidations
MilesCranmer b05cf1d
Delete useless `div` methods
MilesCranmer 80cd5d3
Refactor definition of `mod`
MilesCranmer 7999158
Improve test coverage
MilesCranmer 50e3f2d
Refactor disambiguities
MilesCranmer 58e6bf3
Fix complex ambiguity
MilesCranmer 0609463
Avoid `rem` tests on earlier Julia versions
MilesCranmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,108 @@ | ||
Base.isless(::AbstractQuantity, ::Missing) = missing | ||
Base.isless(::Missing, ::AbstractQuantity) = missing | ||
Base.:(==)(::AbstractQuantity, ::Missing) = missing | ||
Base.:(==)(::Missing, ::AbstractQuantity) = missing | ||
Base.isapprox(::AbstractQuantity, ::Missing; kws...) = missing | ||
Base.isapprox(::Missing, ::AbstractQuantity; kws...) = missing | ||
for op in (:isless, :(==), :isequal, :(<)), (type, _, _) in ABSTRACT_QUANTITY_TYPES | ||
@eval begin | ||
Base.$(op)(::$type, ::Missing) = missing | ||
Base.$(op)(::Missing, ::$type) = missing | ||
end | ||
end | ||
for op in (:isapprox,), (type, _, _) in ABSTRACT_QUANTITY_TYPES | ||
@eval begin | ||
Base.$(op)(::$type, ::Missing; kws...) = missing | ||
Base.$(op)(::Missing, ::$type; kws...) = missing | ||
end | ||
end | ||
|
||
Base.:(==)(::AbstractQuantity, ::WeakRef) = error("Cannot compare a quantity to a weakref") | ||
Base.:(==)(::WeakRef, ::AbstractQuantity) = error("Cannot compare a weakref to a quantity") | ||
for (type, _, _) in ABSTRACT_QUANTITY_TYPES | ||
@eval begin | ||
Base.:(==)(::$type, ::WeakRef) = error("Cannot compare a quantity to a weakref") | ||
Base.:(==)(::WeakRef, ::$type) = error("Cannot compare a weakref to a quantity") | ||
end | ||
end | ||
|
||
Base.:*(l::AbstractDimensions, r::Number) = error("Please use an `UnionAbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).") | ||
Base.:*(l::Number, r::AbstractDimensions) = error("Please use an `UnionAbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).") | ||
Base.:/(l::AbstractDimensions, r::Number) = error("Please use an `UnionAbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).") | ||
Base.:/(l::Number, r::AbstractDimensions) = error("Please use an `UnionAbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).") | ||
|
||
# Promotion ambiguities | ||
function Base.promote_rule(::Type{F}, ::Type{Bool}) where {F<:FixedRational} | ||
return F | ||
end | ||
function Base.promote_rule(::Type{Bool}, ::Type{F}) where {F<:FixedRational} | ||
return F | ||
end | ||
function Base.promote_rule(::Type{F}, ::Type{BigFloat}) where {F<:FixedRational} | ||
return promote_type(Rational{eltype(F)}, BigFloat) | ||
end | ||
function Base.promote_rule(::Type{BigFloat}, ::Type{F}) where {F<:FixedRational} | ||
return promote_type(Rational{eltype(F)}, BigFloat) | ||
end | ||
function Base.promote_rule(::Type{F}, ::Type{T}) where {F<:FixedRational,T<:AbstractIrrational} | ||
return promote_type(Rational{eltype(F)}, T) | ||
end | ||
function Base.promote_rule(::Type{T}, ::Type{F}) where {F<:FixedRational,T<:AbstractIrrational} | ||
return promote_type(Rational{eltype(F)}, T) | ||
end | ||
|
||
################################################################################ | ||
# Assorted calls found by Aqua: ################################################ | ||
################################################################################ | ||
|
||
for type in (Signed, Float64, Float32, Rational), op in (:flipsign, :copysign) | ||
@eval function Base.$(op)(x::$type, y::AbstractRealQuantity) | ||
return $(op)(x, ustrip(y)) | ||
MilesCranmer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
for type in (:(Complex), :(Complex{Bool})) | ||
@eval begin | ||
function Base.:*(l::$type, r::AbstractRealQuantity) | ||
new_quantity(typeof(r), l * ustrip(r), dimension(r)) | ||
end | ||
function Base.:*(l::AbstractRealQuantity, r::$type) | ||
new_quantity(typeof(l), ustrip(l) * r, dimension(l)) | ||
end | ||
end | ||
end | ||
function Complex{T}(q::AbstractRealQuantity) where {T<:Real} | ||
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead." | ||
return Complex{T}(ustrip(q)) | ||
end | ||
for type in (:Bool, :Complex) | ||
@eval function $type(q::AbstractRealQuantity) | ||
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead." | ||
return $type(ustrip(q)) | ||
end | ||
end | ||
function Base.:/(l::Complex, r::AbstractRealQuantity) | ||
new_quantity(typeof(r), l / ustrip(r), inv(dimension(r))) | ||
end | ||
function Base.:/(l::AbstractRealQuantity, r::Complex) | ||
new_quantity(typeof(l), ustrip(l) / r, dimension(l)) | ||
end | ||
for op in (:(==), :isequal), base_type in (AbstractIrrational, AbstractFloat) | ||
@eval begin | ||
function Base.$(op)(l::AbstractRealQuantity, r::$base_type) | ||
return $(op)(ustrip(l), r) && iszero(dimension(l)) | ||
end | ||
function Base.$(op)(l::$base_type, r::AbstractRealQuantity) | ||
return $(op)(l, ustrip(r)) && iszero(dimension(r)) | ||
end | ||
end | ||
end | ||
function Base.isless(l::AbstractRealQuantity, r::AbstractFloat) | ||
iszero(dimension(l)) || throw(DimensionError(l, r)) | ||
return isless(ustrip(l), r) | ||
end | ||
function Base.isless(l::AbstractFloat, r::AbstractRealQuantity) | ||
iszero(dimension(r)) || throw(DimensionError(l, r)) | ||
return isless(l, ustrip(r)) | ||
end | ||
for (type, _, _) in ABSTRACT_QUANTITY_TYPES, numeric_type in (Bool, BigFloat) | ||
@eval begin | ||
function Base.promote_rule(::Type{Q}, ::Type{$numeric_type}) where {T,D,Q<:$type{T,D}} | ||
return with_type_parameters(promote_quantity_on_value(Q, $numeric_type), promote_type(T, $numeric_type), D) | ||
end | ||
function Base.promote_rule(::Type{$numeric_type}, ::Type{Q}) where {T,D,Q<:$type{T,D}} | ||
return with_type_parameters(promote_quantity_on_value(Q, $numeric_type), promote_type(T, $numeric_type), D) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.