diff --git a/NEWS.md b/NEWS.md index 998a191a16767..baa5f0ee72e17 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` have 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 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)