-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Combine signif
into round, use keyword args for digits/sigdigits
#26670
Conversation
base/floatfuncs.jl
Outdated
@@ -44,43 +44,51 @@ isinteger(x::AbstractFloat) = (x - trunc(x) == 0) | |||
|
|||
""" | |||
round([T,] x, [r::RoundingMode]) | |||
round(x, [digits; base = 10]) | |||
round(x, [r::RoundingMode]; digits::Integer= [, base = 10]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
digits::Integer=0, base::Integer=10
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that, but the default digits = 0
is covered by the above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then just omit =
. Though I'm not sure it's a problem to repeat the default. Also brackets are never used for keyword arguments.
BTW, below there's a typo in "provied".
@KristofferC Have you seen this error before? |
@simonbyrne #26705 maybe |
@@ -662,7 +660,7 @@ for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UIn | |||
end | |||
end | |||
function (::Type{$Ti})(x::$Tf) | |||
if ($(Tf(typemin(Ti))) <= x <= $(Tf(typemax(Ti)))) && (trunc(x) == x) | |||
if ($(Tf(typemin(Ti))) <= x <= $(Tf(typemax(Ti)))) && (_round(x, RoundToZero) == x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious: why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
problems with bootstrapping: trunc(x)
had to be defined later.
Triage accepts. |
I think this broke julia> round(pi, sigdigits=3, base=2)
ERROR: MethodError: no method matching exponent(::Irrational{:π})
Closest candidates are:
exponent(::Missing) at missing.jl:75
exponent(::BigFloat) at mpfr.jl:803
exponent(::T<:Union{Float16, Float32, Float64}) where T<:Union{Float16, Float32, Float64} at math.jl:590
Stacktrace:
[1] hidigit(::Irrational{:π}, ::Int64) at ./floatfuncs.jl:179
[2] _round at ./floatfuncs.jl:186 [inlined]
[3] #round#557 at ./floatfuncs.jl:127 [inlined]
[4] #round at ./<missing>:0 [inlined] (repeats 2 times)
[5] top-level scope (Other bases work.) |
Fixes BigFloat digit rounding (JuliaIO/Formatting.jl#56). I've also tweaked the definitions to make it easier to extend to new number formats.
Fixes BigFloat digit rounding (JuliaIO/Formatting.jl#56). I've also tweaked the definitions to make it easier to extend to new number formats.
* origin/master: A few more #26670 fixes (#26773) Revert "deprecate using the value of `.=`. fixes #25954" (#26754) change dim arguments for `diff` and `unique` to keyword args (#26776) reorder pmap arguments to allow do-block syntax (#26783) correct deprecated parametric method syntax (#26789) [NewOptimizer] handle new IR nodes correctly in binary format [NewOptimizer] support line number emission from new IR format fix #26453, require obviously-concrete lower bound for a var to be diagonal (#26567) fix #26743, spurious `return` path in try-finally in tail position (#26753) Also lift SelectInst addrspaces
This is my attempt at fixing #26663.
Internally, this ended up being a bit cleaner, as they share a common codebase. The only downside is that dispatch is slightly more complicated (e.g. to define
round
for a new numeric type, you need to define a method forBase._round
, which is a bit cumbersome).Looking at this a bit more, we could actually generalise this a bit further, by adding 2 more:
step
orincrement
keyword, which would be equivalent toround(x, digits=-1, base=step)
. This would correspond to howround
currently behaves with dates (for dates we could also add an extraepoch
keyword).nth
orinversestep
keyword (there is probably a better name), which would be equivalent toround(x, digits=1, base=nth)
.In this way,
round(x, digits=..)
could in turn call these.