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

Make base a keyword for trunc, floor, ceil, round, signif #26156

Merged
merged 3 commits into from
Feb 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@ Library improvements
* `IOBuffer` can take the `sizehint` keyword argument to suggest a capacity of
the buffer ([#25944]).

* `trunc`, `floor`, `ceil`, `round`, and `signif` specify `base` using a
keyword argument. ([#26156])

Compiler/Runtime improvements
-----------------------------

Expand Down
6 changes: 3 additions & 3 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -960,9 +960,9 @@ function round(z::Complex{<:AbstractFloat}, ::RoundingMode{MR}, ::RoundingMode{M
end
round(z::Complex) = Complex(round(real(z)), round(imag(z)))

function round(z::Complex, digits::Integer, base::Integer=10)
Complex(round(real(z), digits, base),
round(imag(z), digits, base))
function round(z::Complex, digits::Integer; base::Integer = 10)
Complex(round(real(z), digits, base = base),
round(imag(z), digits, base = base))
end

float(z::Complex{<:AbstractFloat}) = z
Expand Down
7 changes: 7 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,13 @@ end
link_pipe!(pipe, reader_supports_async = julia_only_read, writer_supports_async = julia_only_write),
false)

# PR 26156
@deprecate trunc(x, digits, base) trunc(x, digits, base = base)
@deprecate floor(x, digits, base) floor(x, digits, base = base)
@deprecate ceil(x, digits, base) ceil(x, digits, base = base)
@deprecate round(x, digits, base) round(x, digits, base = base)
@deprecate signif(x, digits, base) signif(x, digits, base = base)

# Remember to delete the module when removing this
@eval Base.Math module JuliaLibm
Base.@deprecate log Base.log
Expand Down
15 changes: 8 additions & 7 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ maxintfloat() = maxintfloat(Float64)
isinteger(x::AbstractFloat) = (x - trunc(x) == 0)

"""
round([T,] x, [digits, [base]], [r::RoundingMode])
round([T,] x, [r::RoundingMode])
round(x, [digits; base = 10])

Rounds `x` to an integer value according to the provided
[`RoundingMode`](@ref), returning a value of the same type as `x`. When not
Expand Down Expand Up @@ -71,14 +72,14 @@ rounded.
[`InexactError`](@ref) if the value is not representable.

`round(x, digits)` rounds to the specified number of digits after the decimal place (or
before if negative). `round(x, digits, base)` rounds using a base other than 10.
before if negative). `round(x, digits, base = base)` rounds using a base other than 10.

# Examples
```jldoctest
julia> round(pi, 2)
3.14

julia> round(pi, 3, 2)
julia> round(pi, 3, base = 2)
3.125
```

Expand Down Expand Up @@ -141,7 +142,7 @@ function _signif_og(x, digits, base)
end

"""
signif(x, digits, [base])
signif(x, digits; base = 10)

Rounds (in the sense of [`round`](@ref)) `x` so that there are `digits` significant digits, under a
base `base` representation, default 10.
Expand All @@ -151,11 +152,11 @@ base `base` representation, default 10.
julia> signif(123.456, 2)
120.0

julia> signif(357.913, 4, 2)
julia> signif(357.913, 4, base = 2)
352.0
```
"""
function signif(x::Real, digits::Integer, base::Integer=10)
function signif(x::Real, digits::Integer; base::Integer = 10)
digits < 1 && throw(DomainError(digits, "`digits` cannot be less than 1."))

x = float(x)
Expand All @@ -171,7 +172,7 @@ end

for f in (:round, :ceil, :floor, :trunc)
@eval begin
function ($f)(x::Real, digits::Integer, base::Integer=10)
function ($f)(x::Real, digits::Integer; base::Integer = 10)
x = float(x)
og = convert(eltype(x),base)^digits
r = ($f)(x * og) / og
Expand Down
6 changes: 3 additions & 3 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ mod(x::Integer, ::Type{T}) where {T<:Integer} = rem(x, T)
unsafe_trunc(::Type{T}, x::Integer) where {T<:Integer} = rem(x, T)

"""
trunc([T,] x, [digits, [base]])
trunc([T,] x, [digits; base = 10])

`trunc(x)` returns the nearest integral value of the same type as `x` whose absolute value
is less than or equal to `x`.
Expand All @@ -491,7 +491,7 @@ not representable.
function trunc end

"""
floor([T,] x, [digits, [base]])
floor([T,] x, [digits; base = 10])

`floor(x)` returns the nearest integral value of the same type as `x` that is less than or
equal to `x`.
Expand All @@ -504,7 +504,7 @@ not representable.
function floor end

"""
ceil([T,] x, [digits, [base]])
ceil([T,] x, [digits; base = 10])

`ceil(x)` returns the nearest integral value of the same type as `x` that is greater than or
equal to `x`.
Expand Down
10 changes: 5 additions & 5 deletions test/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ end
@testset "significant digits" begin
# (would be nice to have a smart vectorized
# version of signif)
@test signif(123.456,1) ≈ 100.
@test signif(123.456,3) ≈ 123.
@test signif(123.456,5) ≈ 123.46
@test signif(123.456,8,2) ≈ 123.5
@test signif(123.456,2,4) ≈ 128.0
@test signif(123.456, 1) ≈ 100.
@test signif(123.456, 3) ≈ 123.
@test signif(123.456, 5) ≈ 123.46
@test signif(123.456, 8, base = 2) ≈ 123.5
@test signif(123.456, 2, base = 4) ≈ 128.0
@test signif(0.0, 1) === 0.0
@test signif(-0.0, 1) === -0.0
@test signif(1.2, 2) === 1.2
Expand Down
14 changes: 7 additions & 7 deletions test/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,18 @@ end
end
end
@testset "rounding in other bases" begin
@test round(pi,2,2) ≈ 3.25
@test round(pi,3,2) ≈ 3.125
@test round(pi,3,5) ≈ 3.144
@test round(pi, 2, base = 2) ≈ 3.25
@test round(pi, 3, base = 2) ≈ 3.125
@test round(pi, 3, base = 5) ≈ 3.144
end
@testset "vectorized trunc/round/floor/ceil with digits/base argument" begin
a = rand(2, 2, 2)
for f in (round, trunc, floor, ceil)
@test f.(a[:, 1, 1], 2) == map(x->f(x, 2), a[:, 1, 1])
@test f.(a[:, :, 1], 2) == map(x->f(x, 2), a[:, :, 1])
@test f.(a, 9, 2) == map(x->f(x, 9, 2), a)
@test f.(a[:, 1, 1], 9, 2) == map(x->f(x, 9, 2), a[:, 1, 1])
@test f.(a[:, :, 1], 9, 2) == map(x->f(x, 9, 2), a[:, :, 1])
@test f.(a, 9, 2) == map(x->f(x, 9, 2), a)
@test f.(a, 9, base = 2) == map(x->f(x, 9, base = 2), a)
@test f.(a[:, 1, 1], 9, base = 2) == map(x->f(x, 9, base = 2), a[:, 1, 1])
@test f.(a[:, :, 1], 9, base = 2) == map(x->f(x, 9, base = 2), a[:, :, 1])
@test f.(a, 9, base = 2) == map(x->f(x, 9, base = 2), a)
end
end