Unitful one(T) = 1.0 (without units). What is the alternative?

Basically:

julia> using Unitful

julia> x = 1.0u"nm"
1.0 nm

julia> zero(typeof(x))
0.0 nm

julia> one(typeof(x))
1.0

julia>

Why one(typeof(x)) is not 1.0 nm? And which is the alternative to using one for unit propagation if one has to do something like

julia> f(x::T) = x + one(T)/2
f (generic function with 1 method)

julia> f(1.0u"nm")
ERROR: DimensionError: 1.0 nm and 0.5 are not dimensionally compatible.
Stacktrace:

edit:

T(0.5) is the alternative.

If anyone has further comments, please let me know.

2 Likes
julia> oneunit(typeof(x))
1.0 nm

one is defined as the multiplicative identity which wouldn’t be true if it was unitful.

11 Likes

2 posts were split to a new topic: Zero and zerounit, analogous to one and oneunit?