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

Combine signif into round, use keyword args for digits/sigdigits #26670

Merged
merged 20 commits into from Apr 6, 2018

Conversation

simonbyrne
Copy link
Contributor

@simonbyrne simonbyrne commented Mar 31, 2018

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 for Base._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 or increment keyword, which would be equivalent to round(x, digits=-1, base=step). This would correspond to how round currently behaves with dates (for dates we could also add an extra epoch keyword).
  • an nth or inversestep keyword (there is probably a better name), which would be equivalent to round(x, digits=1, base=nth).

In this way, round(x, digits=..) could in turn call these.

@@ -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])
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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".

@JeffBezanson JeffBezanson added triage This should be discussed on a triage call deprecation This change introduces or involves a deprecation labels Apr 3, 2018
@simonbyrne
Copy link
Contributor Author

@KristofferC
Copy link
Sponsor Member

KristofferC commented Apr 5, 2018

@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)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: why this change?

Copy link
Contributor Author

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.

@JeffBezanson
Copy link
Sponsor Member

Triage accepts.

@JeffBezanson JeffBezanson removed the triage This should be discussed on a triage call label Apr 5, 2018
@simonbyrne simonbyrne merged commit 59eae7d into master Apr 6, 2018
@simonbyrne simonbyrne deleted the sb/signif branch April 6, 2018 18:20
@martinholters
Copy link
Member

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.)

simonbyrne added a commit that referenced this pull request Apr 9, 2018
JeffBezanson pushed a commit that referenced this pull request Apr 10, 2018
simonbyrne added a commit that referenced this pull request Apr 10, 2018
Fixes BigFloat digit rounding (JuliaIO/Formatting.jl#56). I've also tweaked the definitions to make it easier to extend to new number formats.
@jmkuhn jmkuhn mentioned this pull request Apr 11, 2018
simonbyrne added a commit that referenced this pull request Apr 12, 2018
Fixes BigFloat digit rounding (JuliaIO/Formatting.jl#56). I've also tweaked the definitions to make it easier to extend to new number formats.
mbauman added a commit that referenced this pull request Apr 12, 2018
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deprecation This change introduces or involves a deprecation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants