Skip to content
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

round(x::Quantity) = round(typeof(x), x) #732

Closed
jariji opened this issue Jun 27, 2024 · 1 comment
Closed

round(x::Quantity) = round(typeof(x), x) #732

jariji opened this issue Jun 27, 2024 · 1 comment

Comments

@jariji
Copy link

jariji commented Jun 27, 2024

julia> round(1.2u"s")
ERROR: specify the type of the quantity to convert to when rounding quantities. Example: round(typeof(1u"m"), 137u"cm").
Stacktrace:
      internal @ Base, Unitful
 [3] round(x::Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, r::RoundingMode{:Nearest}; kwargs::@Kwargs{})
   @ Unitful ~/.julia/packages/Unitful/6r8Hq/src/quantities.jl:327
Use `err` to retrieve the full stack trace.

julia> let x = 1.2u"s"; round(typeof(x), x; digits=2) end
1.2 s

What about defining round(x::Quantity; kw...) = round(typeof(x), x; kw...) so I don't have to pass it myself? Would that work?

@sostock
Copy link
Collaborator

sostock commented Jun 28, 2024

It would “work”, but then rounding wouldn’t be an operation that is invariant under unit conversion anymore. That’s why it’s forbidden. Consider

julia> round(x::Quantity; kw...) = round(typeof(x), x; kw...)
round (generic function with 89 methods)

julia> 1.2m == 120cm
true

julia> round(1.2m) == round(120cm)
false

Previous discussion: #326, #328

@jariji jariji closed this as completed Jun 28, 2024
@giordano giordano closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants