From 317b59f79ed2194bdb85ad74fe72ccb3b593390f Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 7 Nov 2016 09:41:56 +0100 Subject: [PATCH 1/3] rename num/den -> numerator/denominator --- base/deprecated.jl | 4 ++ base/docs/helpdb/Base.jl | 14 ----- base/exports.jl | 4 +- base/hashing2.jl | 4 +- base/mpfr.jl | 2 +- base/rational.jl | 65 ++++++++++++--------- base/sysimg.jl | 2 +- doc/manual/complex-and-rational-numbers.rst | 6 +- doc/stdlib/math.rst | 4 +- test/hashing.jl | 2 +- test/numbers.jl | 10 ++-- 11 files changed, 59 insertions(+), 58 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index b99709c5f8021..61d44915cd287 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1087,4 +1087,8 @@ eval(Base.LinAlg, quote end end) +# #19246 +@deprecate den denominator +@deprecate num numerator + # End deprecations scheduled for 0.6 diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 2b75cedb08b38..aa9cb83ba5630 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -999,13 +999,6 @@ In-place version of [`reverse`](:func:`reverse`). """ reverse! -""" - num(x) - -Numerator of the rational representation of `x`. -""" -num - """ .<(x, y) @@ -2617,13 +2610,6 @@ The process was stopped by a terminal interrupt (CTRL+C). """ InterruptException -""" - den(x) - -Denominator of the rational representation of `x`. -""" -den - """ issubnormal(f) -> Bool diff --git a/base/exports.jl b/base/exports.jl index 6cdaf50c3bf3c..5b7757236ef9a 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -332,7 +332,7 @@ export csch, dawson, deg2rad, - den, + denominator, digamma, div, divrem, @@ -400,7 +400,7 @@ export nextpow, nextpow2, nextprod, - num, + numerator, num2hex, one, powermod, diff --git a/base/hashing2.jl b/base/hashing2.jl index 3d29fcef204c5..403c5a62e1e93 100644 --- a/base/hashing2.jl +++ b/base/hashing2.jl @@ -94,7 +94,7 @@ Special values: =# decompose(x::Integer) = x, 0, 1 -decompose(x::Rational) = num(x), 0, den(x) +decompose(x::Rational) = numerator(x), 0, denominator(x) function decompose(x::Float16)::NTuple{3,Int} isnan(x) && return 0, 0, 0 @@ -144,7 +144,7 @@ end ## streamlined hashing for smallish rational types ## function hash{T<:BitInteger64}(x::Rational{T}, h::UInt) - num, den = Base.num(x), Base.den(x) + num, den = Base.numerator(x), Base.denominator(x) den == 1 && return hash(num, h) den == 0 && return hash(ifelse(num > 0, Inf, -Inf), h) if isodd(den) diff --git a/base/mpfr.jl b/base/mpfr.jl index 25c92fa3c6ee8..1ac8dadb57d3b 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -107,7 +107,7 @@ convert(::Type{BigFloat}, x::Union{Bool,Int8,Int16,Int32}) = BigFloat(convert(Cl convert(::Type{BigFloat}, x::Union{UInt8,UInt16,UInt32}) = BigFloat(convert(Culong,x)) convert(::Type{BigFloat}, x::Union{Float16,Float32}) = BigFloat(Float64(x)) -convert(::Type{BigFloat}, x::Rational) = BigFloat(num(x)) / BigFloat(den(x)) +convert(::Type{BigFloat}, x::Rational) = BigFloat(numerator(x)) / BigFloat(denominator(x)) function tryparse(::Type{BigFloat}, s::AbstractString, base::Int=0) z = BigFloat() diff --git a/base/rational.jl b/base/rational.jl index e3d7bb4911e98..f65397c064788 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -44,9 +44,9 @@ end .//(y::Number, X::AbstractArray) = reshape([ y // x for x in X ], size(X)) function show(io::IO, x::Rational) - show(io, num(x)) + show(io, numerator(x)) print(io, "//") - show(io, den(x)) + show(io, denominator(x)) end function read{T<:Integer}(s::IO, ::Type{Rational{T}}) @@ -55,7 +55,7 @@ function read{T<:Integer}(s::IO, ::Type{Rational{T}}) r//i end function write(s::IO, z::Rational) - write(s,num(z),den(z)) + write(s,numerator(z),denominator(z)) end convert{T<:Integer}(::Type{Rational{T}}, x::Rational) = Rational{T}(convert(T,x.num),convert(T,x.den)) @@ -104,7 +104,7 @@ julia> rationalize(5.6) julia> a = rationalize(BigInt, 10.3) 103//10 -julia> typeof(num(a)) +julia> typeof(numerator(a)) BigInt ``` """ @@ -170,10 +170,21 @@ end rationalize{T<:Integer}(::Type{T}, x::AbstractFloat; tol::Real=eps(x)) = rationalize(T, x, tol)::Rational{T} rationalize(x::AbstractFloat; kvs...) = rationalize(Int, x; kvs...) -num(x::Integer) = x -den(x::Integer) = one(x) -num(x::Rational) = x.num -den(x::Rational) = x.den +""" + numerator(x) + +Numerator of the rational representation of `x`. +""" +numerator(x::Integer) = x +numerator(x::Rational) = x.num + +""" + denominator(x) + +Denominator of the rational representation of `x`. +""" +denominator(x::Integer) = one(x) +denominator(x::Rational) = x.den sign(x::Rational) = oftype(x, sign(x.num)) signbit(x::Rational) = signbit(x.num) @@ -313,15 +324,15 @@ ceil{ T}(::Type{T}, x::Rational) = convert(T,cld(x.num,x.den)) function round{T, Tr}(::Type{T}, x::Rational{Tr}, ::RoundingMode{:Nearest}) - if den(x) == zero(Tr) && T <: Integer + if denominator(x) == zero(Tr) && T <: Integer throw(DivideError()) - elseif den(x) == zero(Tr) - return convert(T, copysign(one(Tr)//zero(Tr), num(x))) + elseif denominator(x) == zero(Tr) + return convert(T, copysign(one(Tr)//zero(Tr), numerator(x))) end - q,r = divrem(num(x), den(x)) + q,r = divrem(numerator(x), denominator(x)) s = q - if abs(r) >= abs((den(x)-copysign(Tr(4), num(x))+one(Tr)+iseven(q))>>1 + copysign(Tr(2), num(x))) - s += copysign(one(Tr),num(x)) + if abs(r) >= abs((denominator(x)-copysign(Tr(4), numerator(x))+one(Tr)+iseven(q))>>1 + copysign(Tr(2), numerator(x))) + s += copysign(one(Tr),numerator(x)) end convert(T, s) end @@ -329,35 +340,35 @@ end round{T}(::Type{T}, x::Rational) = round(T, x, RoundNearest) function round{T, Tr}(::Type{T}, x::Rational{Tr}, ::RoundingMode{:NearestTiesAway}) - if den(x) == zero(Tr) && T <: Integer + if denominator(x) == zero(Tr) && T <: Integer throw(DivideError()) - elseif den(x) == zero(Tr) - return convert(T, copysign(one(Tr)//zero(Tr), num(x))) + elseif denominator(x) == zero(Tr) + return convert(T, copysign(one(Tr)//zero(Tr), numerator(x))) end - q,r = divrem(num(x), den(x)) + q,r = divrem(numerator(x), denominator(x)) s = q - if abs(r) >= abs((den(x)-copysign(Tr(4), num(x))+one(Tr))>>1 + copysign(Tr(2), num(x))) - s += copysign(one(Tr),num(x)) + if abs(r) >= abs((denominator(x)-copysign(Tr(4), numerator(x))+one(Tr))>>1 + copysign(Tr(2), numerator(x))) + s += copysign(one(Tr),numerator(x)) end convert(T, s) end function round{T, Tr}(::Type{T}, x::Rational{Tr}, ::RoundingMode{:NearestTiesUp}) - if den(x) == zero(Tr) && T <: Integer + if denominator(x) == zero(Tr) && T <: Integer throw(DivideError()) - elseif den(x) == zero(Tr) - return convert(T, copysign(one(Tr)//zero(Tr), num(x))) + elseif denominator(x) == zero(Tr) + return convert(T, copysign(one(Tr)//zero(Tr), numerator(x))) end - q,r = divrem(num(x), den(x)) + q,r = divrem(numerator(x), denominator(x)) s = q - if abs(r) >= abs((den(x)-copysign(Tr(4), num(x))+one(Tr)+(num(x)<0))>>1 + copysign(Tr(2), num(x))) - s += copysign(one(Tr),num(x)) + if abs(r) >= abs((denominator(x)-copysign(Tr(4), numerator(x))+one(Tr)+(numerator(x)<0))>>1 + copysign(Tr(2), numerator(x))) + s += copysign(one(Tr),numerator(x)) end convert(T, s) end function round{T}(::Type{T}, x::Rational{Bool}) - if den(x) == false && issubtype(T, Union{Integer, Bool}) + if denominator(x) == false && issubtype(T, Union{Integer, Bool}) throw(DivideError()) end convert(T, x) diff --git a/base/sysimg.jl b/base/sysimg.jl index 82b4d45cc0a90..8e5dd922b6594 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -247,7 +247,7 @@ include("mpfr.jl") importall .MPFR big(n::Integer) = convert(BigInt,n) big(x::AbstractFloat) = convert(BigFloat,x) -big(q::Rational) = big(num(q))//big(den(q)) +big(q::Rational) = big(numerator(q))//big(denominator(q)) include("combinatorics.jl") diff --git a/doc/manual/complex-and-rational-numbers.rst b/doc/manual/complex-and-rational-numbers.rst index c23b5bdd53277..c0da0998bb1f7 100644 --- a/doc/manual/complex-and-rational-numbers.rst +++ b/doc/manual/complex-and-rational-numbers.rst @@ -229,14 +229,14 @@ are reduced to lowest terms such that the denominator is non-negative: This normalized form for a ratio of integers is unique, so equality of rational values can be tested by checking for equality of the numerator and denominator. The standardized numerator and denominator of a -rational value can be extracted using the :func:`num` and :func:`den` functions: +rational value can be extracted using the :func:`numerator` and :func:`denominator` functions: .. doctest:: - julia> num(2//3) + julia> numerator(2//3) 2 - julia> den(2//3) + julia> denominator(2//3) 3 Direct comparison of the numerator and denominator is generally not diff --git a/doc/stdlib/math.rst b/doc/stdlib/math.rst index e034c02b2726a..148965f19374d 100644 --- a/doc/stdlib/math.rst +++ b/doc/stdlib/math.rst @@ -324,13 +324,13 @@ Mathematical Operators julia> typeof(num(a)) BigInt -.. function:: num(x) +.. function:: numerator(x) .. Docstring generated from Julia source Numerator of the rational representation of ``x``\ . -.. function:: den(x) +.. function:: denominator(x) .. Docstring generated from Julia source diff --git a/test/hashing.jl b/test/hashing.jl index 272c20a1d6172..60da16201cfe9 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -20,7 +20,7 @@ vals = vcat( function coerce(T::Type, x) if T<:Rational - convert(T, coerce(typeof(num(zero(T))), x)) + convert(T, coerce(typeof(numerator(zero(T))), x)) elseif !(T<:Integer) convert(T, x) else diff --git a/test/numbers.jl b/test/numbers.jl index d08c6b7a3ba97..cb28f874c7793 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1299,9 +1299,9 @@ for yr = Any[ f2, m2 = fldmod(x,y) t1 = isa(x,Rational) && isa(y,Rational) ? - promote_type(typeof(num(x)),typeof(num(y))) : - isa(x,Rational) ? promote_type(typeof(num(x)),typeof(y)) : - isa(y,Rational) ? promote_type(typeof(x),typeof(num(y))) : + promote_type(typeof(numerator(x)),typeof(numerator(y))) : + isa(x,Rational) ? promote_type(typeof(numerator(x)),typeof(y)) : + isa(y,Rational) ? promote_type(typeof(x),typeof(numerator(y))) : promote_type(typeof(x),typeof(y)) t2 = promote_type(typeof(x),typeof(y)) @@ -2630,8 +2630,8 @@ end rand_int = rand(Int8) for T in [Int8, Int16, Int32, Int128, BigInt] - @test num(convert(T, rand_int)) == rand_int - @test den(convert(T, rand_int)) == 1 + @test numerator(convert(T, rand_int)) == rand_int + @test denominator(convert(T, rand_int)) == 1 @test typemin(Rational{T}) == -one(T)//zero(T) @test typemax(Rational{T}) == one(T)//zero(T) From 73590f113cb0f4e3fd311abe48caf6c3c3ff21b3 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 7 Nov 2016 16:27:12 +0100 Subject: [PATCH 2/3] added news entry [ci skip] --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 998a191a16767..d6f3446c961e2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -66,6 +66,8 @@ Deprecated or removed * `is` has been deprecated in favor of `===` (which used to be an alias for `is`) ([#17758]). + * `num` and `den` has been deprecated in favor of `numerator` and `denominator` respectively ([#19233]). + Julia v0.5.0 Release Notes ========================== @@ -693,3 +695,4 @@ Language tooling improvements [#18473]: https://github.com/JuliaLang/julia/issues/18473 [#18839]: https://github.com/JuliaLang/julia/issues/18839 [#19018]: https://github.com/JuliaLang/julia/issues/19018 +[#19233]: https://github.com/JuliaLang/julia/issues/19233 From 3df47363331beec965e448b51f8c3d297ce3ed56 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Wed, 9 Nov 2016 10:02:04 +0100 Subject: [PATCH 3/3] grammar [ci skip] --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index d6f3446c961e2..baa5f0ee72e17 100644 --- a/NEWS.md +++ b/NEWS.md @@ -66,7 +66,7 @@ Deprecated or removed * `is` has been deprecated in favor of `===` (which used to be an alias for `is`) ([#17758]). - * `num` and `den` has been deprecated in favor of `numerator` and `denominator` respectively ([#19233]). + * `num` and `den` have been deprecated in favor of `numerator` and `denominator` respectively ([#19233]). Julia v0.5.0 Release Notes ==========================