SymbolicML / DynamicQuantities.jl

Efficient and type-stable physical quantities in Julia
https://symbolicml.org/DynamicQuantities.jl/dev/
Apache License 2.0
120 stars 15 forks source link

Why `one(1u"kg")` no dimension? #130

Closed liuyxpp closed 2 months ago

liuyxpp commented 3 months ago

Example below:

julia> using DynamicQuantities
julia> zero(1u"kg")
0.0 kg
julia> one(1u"kg")
1.0
julia> zero(1u"kg") |> dimension
kg
julia> one(1u"kg") |> dimension
# nothing displayed

I wonder why dimensions for the results of zero and one functions are different. Is it intentional or a bug?

MilesCranmer commented 2 months ago

This is the difference between one and oneunit. See https://docs.julialang.org/en/v1/base/numbers/#Base.oneunit

If you want a quantity that is of the same type as x, or of type T, even if x is dimensionful, use oneunit instead.

The way these are defined is that one is a multiplicative identity and zero is an additive identity. This is why one does not have units.

MilesCranmer commented 1 month ago

@liuyxpp on #136 you can find an alternative dimensions type that permits zero(::Type)