-
Notifications
You must be signed in to change notification settings - Fork 25
Add Base.zero and one #6
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
Conversation
Benchmark Results
Benchmark PlotsA plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR. |
Are these right? I think zero should probably keep the dimension, and julia> x = Quantity(0.3, mass=1, length=0.5)
0.3 𝐋 ¹ᐟ² 𝐌 ¹
julia> x + zero(typeof(x)) # additive identity element
ERROR: DimensionError: 0.3 𝐋 ¹ᐟ² 𝐌 ¹ and 0.0 have incompatible dimensions
Stacktrace:
[1] +(l::Quantity{Float64, DynamicQuantities.FixedRational{…}}, r::Quantity{Float64, DynamicQuantities.FixedRational{…}})
@ DynamicQuantities ~/.julia/packages/DynamicQuantities/jl8WE/src/math.jl:19
[2] top-level scope
@ REPL[83]:1
Some type information was truncated. Use `show(err)` to see complete types.
julia> x * one(typeof(x)) # multiplicative identity for x
0.3 𝐋 ¹ᐟ² 𝐌 ¹
julia> x + oneunit(typeof(x)) # This differs from one for dimensionful quantities: one is dimensionless (a
multiplicative identity) while oneunit is dimensionful (of the same type as x, or of type T).
ERROR: MethodError: no method matching Quantity{Float64, DynamicQuantities.FixedRational{Int32, 25200}}(::Quantity{Float64, DynamicQuantities.FixedRational{Int32, 25200}}) I also wonder whether julia> one(typeof(x))
1.0
julia> typeof(ans)
Quantity{Float64, DynamicQuantities.FixedRational{Int32, 25200}} |
This is the intended behavior, although I'm happy to discuss whether it makes the most sense. I view it as adding a 2D array to a 3D array - even if the 2D array is all 0s, you would still want an error to be thrown. Other unit packages have similar behavior. e.g., astropy: from astropy import units as u
q = 10 * u.m # 10 meters
q2 = 10 * u.s # 10 seconds
q * 0 + q2
# ^ Raises UnitConversionsError and Unitful.jl julia> using Unitful
julia> 0.0u"m" + 10.0u"s"
ERROR: DimensionError: 0.0 m and 10.0 s are not dimensionally compatible.
This breaks because the
This is what Unitful.jl does, but from my brief experience, it can get very frustrating when you request a type and are given something else, or if it randomly converts to a Float64 in the middle of a calculation because it happened to be dimensionless. In the Unitful->DynamicQuantities conversion I need to construct a Note also that you can't add a julia> 0.5 + Quantity(0.5) (Right now this threads a |
Ah my complaint is not about the error, but about the zero. I agree that adding apples + oranges never makes sense. But perhaps I forgot about types & this is more complicated than I thought.
I do think |
Okay, I think I see what you mean. I'll put in a PR for this (and the |
No description provided.