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

digits as keyword argument for round and friends #530

Merged
merged 2 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `Compat.range` supporting keyword arguments ([#25896]).

* `Compat.trunc`, `Compat.floor`, `Compat.ceil`, `Compat.round`, `Compat.signif` take a keyword argument for `base` ([#26156]).
* `Compat.trunc`, `Compat.floor`, `Compat.ceil`, `Compat.round`, take a keyword argument
for `base` and `digits`, `Compat.round` also takes `sigdigits` ([#26156], [#26670]).

* `Compat.mv` and `Compat.cp` with `force` keyword argument ([#26069]).

Expand Down Expand Up @@ -618,3 +619,4 @@ includes this fix. Find the minimum version from there.
[#26369]: https://github.com/JuliaLang/julia/issues/26369
[#26436]: https://github.com/JuliaLang/julia/issues/26436
[#26442]: https://github.com/JuliaLang/julia/issues/26442
[#26670]: https://github.com/JuliaLang/julia/issues/26670
52 changes: 46 additions & 6 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1592,13 +1592,53 @@ else
end
end

# https://github.com/JuliaLang/julia/pull/26156
# https://github.com/JuliaLang/julia/pull/26670
@static if VERSION < v"0.7.0-DEV.4062"
trunc(x, digits; base = base) = Base.trunc(x, digits, base)
floor(x, digits; base = base) = Base.floor(x, digits, base)
ceil(x, digits; base = base) = Base.ceil(x, digits, base)
round(x, digits; base = base) = Base.round(x, digits, base)
signif(x, digits; base = base) = Base.signif(x, digits, base)
trunc(x; digits = digits, base = base) = Base.trunc(x, digits, base)
floor(x; digits = digits, base = base) = Base.floor(x, digits, base)
ceil(x; digits = digits, base = base) = Base.ceil(x, digits, base)
function round(x; digits = nothing, sigdigits = nothing, base = base)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't base have a default value here?

Copy link
Member Author

Choose a reason for hiding this comment

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

See #567/#537.

if digits === nothing
if sigdigits === nothing
Base.round(x, 0, base)
else
Base.signif(x, sigdigits, base)
end
else
sigdigits === nothing || throw(AgrumentError("`round` cannot use both `digits` and `sigdigits` arguments"))
Base.round(x, digits, base)
end
end
elseif VERSION < v"0.7.0-DEV.4804"
trunc(x; digits = digits, base = base) = Base.trunc(x, digits, base = base)
floor(x; digits = digits, base = base) = Base.floor(x, digits, base = base)
ceil(x; digits = digits, base = base) = Base.ceil(x, digits, base = base)
function round(x; digits = nothing, sigdigits = nothing, base = base)
if digits === nothing
if sigdigits === nothing
Base.round(x, 0, base = base)
else
Base.signif(x, sigdigits, base = base)
end
else
sigdigits === nothing || throw(AgrumentError("`round` cannot use both `digits` and `sigdigits` arguments"))
Base.round(x, digits, base = base)
end
end
else
trunc(x; digits = digits, base = base) = Base.trunc(x, digits = digits, base = base)
floor(x; digits = digits, base = base) = Base.floor(x, digits = digits, base = base)
ceil(x; digits = digits, base = base) = Base.ceil(x, digits = digits, base = base)
round(x; digits = nothing, sigdigits = nothing, base = base) = Base.round(x, digits = digits, sigdigits = sigdigits, base = base)
end

# compatibiltiy with https://github.com/JuliaLang/julia/pull/26156
if VERSION < v"0.7.0-DEV.4062" || VERSION >= v"0.7.0-DEV.4804"
trunc(x, digits; base = base) = trunc(x, digits = digits, base = base)
floor(x, digits; base = base) = floor(x, digits = digits, base = base)
ceil(x, digits; base = base) = ceil(x, digits = digits, base = base)
round(x, digits; base = base) = round(x, digits = digits, base = base)
signif(x, digits; base = base) = round(x, sigdigits = digits, base = base)
end

# https://github.com/JuliaLang/julia/pull/25872
Expand Down
11 changes: 9 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1472,12 +1472,19 @@ end
@test :foo in Compat.names(TestNames)
@test :bar in Compat.names(TestNames, all=true)

# 0.7.0-DEV.4062
# 0.7.0-DEV.4062, but dropped in 0.7.0-DEV.4804
@test Compat.trunc(pi, 3, base = 2) == 3.125
@test Compat.floor(pi, 3, base = 2) == 3.125
@test Compat.ceil(pi, 3, base = 2) == 3.25
@test Compat.round(pi, 3, base = 2) == 3.125
@test Compat.signif(pi, 5, base = 2) == 3.125
@test Compat.signif(pi, 5, base = 10) == 3.1416
Copy link
Member Author

Choose a reason for hiding this comment

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

Change base to 10 to circumvent JuliaLang/julia#26670 (comment).


# 0.7.0-DEV.4804
@test Compat.trunc(pi, digits = 3, base = 2) == 3.125
@test Compat.floor(pi, digits = 3, base = 2) == 3.125
@test Compat.ceil(pi, digits = 3, base = 2) == 3.25
@test Compat.round(pi, digits = 3, base = 2) == 3.125
@test Compat.round(pi, sigdigits = 5, base = 10) == 3.1416

# 0.7.0-DEV.3734
let buf = Compat.IOBuffer(read=true, write=false, maxsize=25)
Expand Down