-
Notifications
You must be signed in to change notification settings - Fork 113
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(n; sigdigits) without explicit units #326
Comments
They only agree because you are rounding in base 10 and s/ns = 10^9 is a power of 10 as well. For other units, they don’t agree: julia> time_hr = 1.23456*u"hr"
1.23456 hr
julia> time_s = time_hr |> u"s"
4444.416 s
julia> round(ustrip(time_s), sigdigits=3) * unit(time_s)
4440.0 s
julia> round(ustrip(time_hr), sigdigits=3) * unit(time_hr)
1.23 hr
julia> ans |> u"s" # 4440 s != 4428 s
4428.0 s |
Sure. The more refined version of this is #328 (comment) : After declaring that you care about 3 digits, how surprising is it really that things don't agree beyond that? ("3 digits" is, of course, a crude way of saying "between 1 part in 10^2 and 1 in 10^3". It's specifying a logarithmic cutoff in linear space, sort-of. The error bar on the number of digits is about 1.) |
I would be surprised if However, I understand the desire for “unitless rounding” for the purpose of printing less digits (cf. #474), because we don’t have a nice way to do that (like |
But when you say "same value, just different types", what does "same" mean? If we are talking about physical quantities, with units, then it does not mean infinite precision, surely. Even if we just have floating point numbers, we also have finite precision, and thus cannot trust that rounding commutes with changes of type:
However, these will "almost always" agree, where the meaning of "almost" depends on the precision we specified. |
Yes, floating-point numbers have finite precision and can introduce arbitrarily large errors. However, I don’t think we should give up unit-invariant arithmetic just because floating-point numbers can break it.
I think we fundamentally disagree on that. I think physical quantities can have values with infinite precision. Measurements cannot, of course, but for example the speed of light is defined as exactly 299,792,458 m/s, with infinite precision. In the example above, the numbers Generally, I would want exact unit-invariance when using exact arithmetic (i.e., when using an exact type like
That’s where our disagreement may stem from. I don’t think of specifying Footnotes
|
I feel like perfect is the enemy of the good here. When I want to round a The issues that arise with inequality when changing the order of rounding and unit conversion is a bummer, but there is no way around it - the information is lost during rounding. I honestly think that you are crazy if you expect strict commutativity between rounding and unit conversion, and that you would be foolish to use rounding and unit conversion without extreme care if you are doing anything important. And in the extreme care case, simply specify the output unit! But please allow the quick-and-dirty "let me remove some visual noise" rounding to be done without specifying the output unit, when such a sensible default exists. |
As for requiring the keyword argument |
I'm writing a Plots recipe and would like to support user specified rounding for tick labels, but because of this issue it ends up being somewhat complicated: julia> x = 1.2345678
1.2345678
julia> xs = x * 1u"s"
1.2345678 s
julia> round(x, digits=2)
1.23
julia> round(xs, digits=2)
ERROR: specify the type of the quantity to convert to when rounding quantities. Example: round(typeof(1u"m"), 137u"cm").
julia> round(typeof(xs), xs, digits=2)
1.23 s
julia> round(typeof(x), x, digits=2)
ERROR: MethodError: no method matching round(::Type{Float64}, ::Float64; digits=2) Because of this issue, I have to create my own rounding function that has different methods for |
I just realized that to create my own rounding function with a method for values of type |
I strongly agree with this opinion. |
neat solution:
It should be added inside |
While being able to round in a unit-controlled way is neat (and should perhaps be encouraged n the docs), what's the reason to disallow simple rounding? I expected the second of these to work, and if it did, then code with & without units would behave the same:
Edit: for
sigdigits
in particular, the objection that the rounding depends on your units does not apply, these agree:The text was updated successfully, but these errors were encountered: