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

Rename num/den to numerator/denominator #19246

Merged
merged 3 commits into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1087,4 +1087,8 @@ eval(Base.LinAlg, quote
end
end)

# #19246
@deprecate den denominator
@deprecate num numerator

# End deprecations scheduled for 0.6
14 changes: 0 additions & 14 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -999,13 +999,6 @@ In-place version of [`reverse`](:func:`reverse`).
"""
reverse!

"""
num(x)

Numerator of the rational representation of `x`.
"""
num

"""
.<(x, y)

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export
csch,
dawson,
deg2rad,
den,
denominator,
digamma,
div,
divrem,
Expand Down Expand Up @@ -400,7 +400,7 @@ export
nextpow,
nextpow2,
nextprod,
num,
numerator,
num2hex,
one,
powermod,
Expand Down
4 changes: 2 additions & 2 deletions base/hashing2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
65 changes: 38 additions & 27 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
Expand All @@ -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))
Expand Down Expand Up @@ -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
```
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -313,51 +324,51 @@ 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

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)
Expand Down
2 changes: 1 addition & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
6 changes: 3 additions & 3 deletions doc/manual/complex-and-rational-numbers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down