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

Set default base for round to 10 #537

Merged
merged 4 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 29 additions & 17 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1594,10 +1594,10 @@ end

# https://github.com/JuliaLang/julia/pull/26670
@static if VERSION < v"0.7.0-DEV.4062"
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)
trunc(x; digits = 0, base = 10) = Base.trunc(x, digits, base)
floor(x; digits = 0, base = 10) = Base.floor(x, digits, base)
ceil(x; digits = 0, base = 10) = Base.ceil(x, digits, base)
function round(x; digits = nothing, sigdigits = nothing, 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.

Maybe these should be constants?

if digits === nothing
if sigdigits === nothing
Base.round(x, 0, base)
Expand All @@ -1610,10 +1610,10 @@ end
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)
trunc(x; digits = 0, base = 10) = Base.trunc(x, digits, base = base)
floor(x; digits = 0, base = 10) = Base.floor(x, digits, base = base)
ceil(x; digits = 0, base = 10) = Base.ceil(x, digits, base = base)
function round(x; digits = nothing, sigdigits = nothing, base = 10)
if digits === nothing
if sigdigits === nothing
Base.round(x, 0, base = base)
Expand All @@ -1625,19 +1625,31 @@ elseif VERSION < v"0.7.0-DEV.4804"
Base.round(x, digits, base = base)
end
end
elseif VERSION < v"0.7.0-beta2.86"
# https://github.com/JuliaLang/julia/pull/28199
trunc(x; digits = 0, base = 10) = Base.trunc(x, digits = digits, base = base)
floor(x; digits = 0, base = 10) = Base.floor(x, digits = digits, base = base)
ceil(x; digits = 0, base = 10) = Base.ceil(x, digits = digits, base = base)
function round(x; digits = nothing, sigdigits = nothing, base = 10)
if digits === nothing && sigdigits === nothing
Base.round(x, digits = 0, base = base)
else
Base.round(x, digits = digits, sigdigits = sigdigits, 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)
trunc(x; digits = 0, base = 10) = Base.trunc(x, digits = digits, base = base)
floor(x; digits = 0, base = 10) = Base.floor(x, digits = digits, base = base)
ceil(x; digits = 0, base = 10) = Base.ceil(x, digits = digits, base = base)
round(x; digits = nothing, sigdigits = nothing, base = 10) = Base.round(x, digits = digits, sigdigits = sigdigits, base = base)
end

# compatibiltiy with https://github.com/JuliaLang/julia/pull/26156
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)
trunc(x, digits; base = 10) = trunc(x, digits = digits, base = base)
floor(x, digits; base = 10) = floor(x, digits = digits, base = base)
ceil(x, digits; base = 10) = ceil(x, digits = digits, base = base)
round(x, digits; base = 10) = round(x, digits = digits, base = base)
signif(x, digits; base = 10) = round(x, sigdigits = digits, base = base)

# https://github.com/JuliaLang/julia/pull/25872
if VERSION < v"0.7.0-DEV.3734"
Expand Down
15 changes: 14 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1481,11 +1481,24 @@ end
@test Compat.signif(pi, 5, base = 10) == 3.1416

# 0.7.0-DEV.4804
@test Compat.trunc(pi) == 3.0
@test Compat.floor(pi) == 3.0
@test Compat.ceil(pi) == 4.0
@test Compat.round(pi) == 3.0
@test Compat.trunc(pi, digits = 3) == 3.141
@test Compat.floor(pi, digits = 3) == 3.141
@test Compat.ceil(pi, digits = 3) == 3.142
@test Compat.round(pi, digits = 3) == 3.142
@test Compat.round(pi, sigdigits = 5) == 3.1416
@test Compat.trunc(pi, base = 2) == 3.0
@test Compat.floor(pi, base = 2) == 3.0
@test Compat.ceil(pi, base = 2) == 4.0
@test Compat.round(pi, base = 2) == 3.0
@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
@test Compat.round(pi, sigdigits = 5, base = 2) == 3.125

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