-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unitless typeof and eltype #22216
Comments
Even julia> using Unitful
julia> x = 3u"m"
3 m
julia> x/x
1.0 which is Unitful has ustrip(x::Number) = x and then each package could specialize it for the types defined in the package. |
Wouldn't this be useful in Base for the same reason as |
A kind of funny workaround for this is |
Yes, if If you want to go the I agree these are conceptually relevant for dates, but AFAICT all dates objects are fundamentally |
Well if that's the case then I'm fine with using it. Would a simple unitless_eltype(x) = typeof(one(eltype(x))) be worthy of inclusion into Base? |
I think |
Would changing |
It might well do so. I guess as long as uniful quantities behave as expected wrt |
Unitful actually does handle this: using Unitful
1u"m"/1u"m"
# 1.00
I agree with this because while Unitful special-cased it to work, there's no reason to assume each other units package does. A generic fallback is inherently brittle for this reason, so I agree that it should probably be left up to the units packages to handle the overload. |
The usual nomenclature (e.g. mathematica) is that quantity refers to the unitful quantity, not the unitless one. Nevertheless it's not strictly always possible to strip a unit, because that may be ambiguous or not make sense... taking |
I think having
My gut feeling is that these equalities should also hold when taking |
If |
How often does one need to get the value without the unit? The original question was about getting the unitless type, where I don't think we should have |
|
Now there is Unitless.jl which can do this: julia> using Unitful, Unitless
julia> baretype(u"3km/s")
Int64
julia> baretype(u"3.2km/s")
Float64 |
A very common operation when trying to write package which works with unitful quantities is getting the unitless element-type. This is required to find the right
eps
or set the tolerance to the correctNumber
type. However, I am not sure of a completely generic way to handle this.typeof(first(one(u)))
works well in a pinch, but I am noticing an issue withGPUArray
s which do not have indexing and thus don't havefirst
defined. So the only way I know of to always get the unitless element type iseltype(u./u)
, which has an extra operation just to grab the type.Is there a generic function to strip the units off of a type? Some
unitless(T)
to get whatever the type of the non-unit part is? That would be a clear way to implement this.The text was updated successfully, but these errors were encountered: