Skip to content

Commit

Permalink
Rename num/den to numerator/denominator (#19246)
Browse files Browse the repository at this point in the history
* rename num/den -> numerator/denominator

* added news entry [ci skip]

* grammar [ci skip]
  • Loading branch information
fredrikekre authored and KristofferC committed Nov 10, 2016
1 parent c263a6e commit 5fa05dc
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 58 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
==========================

Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1098,4 +1098,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

0 comments on commit 5fa05dc

Please sign in to comment.