From b889040fe088ed430ed9bf646cfbcb28cab432a7 Mon Sep 17 00:00:00 2001 From: John M Kuhn Date: Thu, 22 Feb 2018 06:32:27 -0500 Subject: [PATCH 1/3] Make base a keyword for trunc, floor, ceil, round, signif --- NEWS.md | 3 +++ base/complex.jl | 6 +++--- base/deprecated.jl | 7 +++++++ base/floatfuncs.jl | 14 +++++++------- base/int.jl | 6 +++--- test/floatfuncs.jl | 10 +++++----- test/rounding.jl | 14 +++++++------- 7 files changed, 35 insertions(+), 25 deletions(-) diff --git a/NEWS.md b/NEWS.md index b821906be8639..233da8824c476 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. ([#xxxxx]) + Compiler/Runtime improvements ----------------------------- diff --git a/base/complex.jl b/base/complex.jl index dbeb47bf55f90..4b53923dbf3a2 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -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 diff --git a/base/deprecated.jl b/base/deprecated.jl index 576369a175812..bc679da0c5e52 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1400,6 +1400,13 @@ end link_pipe!(pipe, reader_supports_async = julia_only_read, writer_supports_async = julia_only_write), false) +# PR xxxxx +@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 diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index a68d9d33abbbc..b5e269b9f8c3e 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -43,7 +43,7 @@ maxintfloat() = maxintfloat(Float64) isinteger(x::AbstractFloat) = (x - trunc(x) == 0) """ - round([T,] x, [digits, [base]], [r::RoundingMode]) + round([T,] x, [digits,] [r::RoundingMode;] 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 @@ -71,14 +71,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 ``` @@ -141,7 +141,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. @@ -151,11 +151,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) @@ -171,7 +171,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 diff --git a/base/int.jl b/base/int.jl index 7acb3fef01ad8..c6909db5d2fcc 100644 --- a/base/int.jl +++ b/base/int.jl @@ -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`. @@ -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`. @@ -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`. diff --git a/test/floatfuncs.jl b/test/floatfuncs.jl index c6ca32200279e..00f1b25794154 100644 --- a/test/floatfuncs.jl +++ b/test/floatfuncs.jl @@ -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 diff --git a/test/rounding.jl b/test/rounding.jl index 57872c617175d..fe6779e382cc2 100644 --- a/test/rounding.jl +++ b/test/rounding.jl @@ -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 From b3e9545c97d5fa09f014c08d5923d5a7d7c81ce7 Mon Sep 17 00:00:00 2001 From: John M Kuhn Date: Thu, 22 Feb 2018 06:39:25 -0500 Subject: [PATCH 2/3] Update PR # --- NEWS.md | 2 +- base/complex.jl | 2 +- base/deprecated.jl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 233da8824c476..3de7e11515903 100644 --- a/NEWS.md +++ b/NEWS.md @@ -600,7 +600,7 @@ Library improvements the buffer ([#25944]). * `trunc`, `floor`, `ceil`, `round`, and `signif` specify `base` using a - keyword argument. ([#xxxxx]) + keyword argument. ([#26156]) Compiler/Runtime improvements ----------------------------- diff --git a/base/complex.jl b/base/complex.jl index 4b53923dbf3a2..80ef9edf7e803 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -960,7 +960,7 @@ 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) +function round(z::Complex, digits::Integer; base::Integer = 10) Complex(round(real(z), digits, base = base), round(imag(z), digits, base = base)) end diff --git a/base/deprecated.jl b/base/deprecated.jl index bc679da0c5e52..1f98294e9b37e 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1400,7 +1400,7 @@ end link_pipe!(pipe, reader_supports_async = julia_only_read, writer_supports_async = julia_only_write), false) -# PR xxxxx +# 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) From 242c59b69f50097ba07fb7795040024af402cc75 Mon Sep 17 00:00:00 2001 From: John M Kuhn Date: Thu, 22 Feb 2018 07:40:27 -0500 Subject: [PATCH 3/3] Update documented signatures --- base/floatfuncs.jl | 5 +++-- base/int.jl | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index b5e269b9f8c3e..09c70c7fe27e5 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -43,7 +43,8 @@ maxintfloat() = maxintfloat(Float64) isinteger(x::AbstractFloat) = (x - trunc(x) == 0) """ - round([T,] x, [digits,] [r::RoundingMode;] base = 10) + 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 @@ -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 diff --git a/base/int.jl b/base/int.jl index c6909db5d2fcc..99a65e69cdd55 100644 --- a/base/int.jl +++ b/base/int.jl @@ -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 = 10) + 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`. @@ -491,7 +491,7 @@ not representable. function trunc end """ - floor([T,] x, [digits;] base = 10) + 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`. @@ -504,7 +504,7 @@ not representable. function floor end """ - ceil([T,] x, [digits;] base = 10) + 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`.