Skip to content

Commit

Permalink
deprecate oftype(type,x), which is identical to convert
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Oct 16, 2014
1 parent 56f0820 commit 5039cb1
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ angle(z::Complex) = atan2(imag(z), real(z))
function log{T<:FloatingPoint}(z::Complex{T})
const T1::T = 1.25
const T2::T = 3
const ln2::T = log(oftype(T,2)) #0.6931471805599453
const ln2::T = log(convert(T,2)) #0.6931471805599453
x, y = reim(z)
ρ, k = ssqs(x,y)
ax = abs(x)
Expand Down
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,5 @@ const None = Union()
@deprecate Dict{K}(ks::(K...), vs::Tuple) Dict{K,Any}(zip(ks, vs))
@deprecate Dict{V}(ks::Tuple, vs::(V...)) Dict{Any,V}(zip(ks, vs))
@deprecate Dict(ks, vs) Dict{Any,Any}(zip(ks, vs))

@deprecate oftype{T}(::Type{T},c) convert(T,c)
2 changes: 1 addition & 1 deletion base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ for f in (:round, :ceil, :floor, :trunc)
@eval begin
function ($f)(x, digits::Integer, base::Integer=10)
x = float(x)
og = oftype(eltype(x),base)^digits
og = convert(eltype(x),base)^digits
($f)(x * og) / og
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function expm!{T<:BlasFloat}(A::StridedMatrix{T})
s = log2(nA/5.4) # power of 2 later reversed by squaring
if s > 0
si = iceil(s)
A /= oftype(T,2^si)
A /= convert(T,2^si)
end
CC = T[64764752532480000.,32382376266240000.,7771770303897600.,
1187353796428800., 129060195264000., 10559470521600.,
Expand Down
4 changes: 2 additions & 2 deletions base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ reinterpret{T,S}(::Type{T}, x::S) = box(T,unbox(S,x))
map(f::Callable, x::Number) = f(x)

zero(x::Number) = oftype(x,0)
zero{T<:Number}(::Type{T}) = oftype(T,0)
zero{T<:Number}(::Type{T}) = convert(T,0)
one(x::Number) = oftype(x,1)
one{T<:Number}(::Type{T}) = oftype(T,1)
one{T<:Number}(::Type{T}) = convert(T,1)

const _numeric_conversion_func_names =
(:int,:integer,:signed,:int8,:int16,:int32,:int64,:int128,
Expand Down
3 changes: 1 addition & 2 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ A_ldiv_Bt (a,b) = a\transpose(b)
At_ldiv_Bt(a,b) = transpose(a)\transpose(b)


oftype{T}(::Type{T},c) = convert(T,c)
oftype{T}(x::T,c) = convert(T,c)
oftype(x,c) = convert(typeof(x),c)

widen{T<:Number}(x::T) = convert(widen(T), x)

Expand Down
22 changes: 11 additions & 11 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ immutable StepRange{T,S} <: OrdinalRange{T,S}
if T<:Signed && (diff > zero(diff)) != (stop > start)
# handle overflowed subtraction with unsigned rem
if diff > zero(diff)
remain = -oftype(T, unsigned(-diff) % step)
remain = -convert(T, unsigned(-diff) % step)
else
remain = oftype(T, unsigned(diff) % step)
remain = convert(T, unsigned(diff) % step)
end
else
remain = steprem(start,stop,step)
Expand Down Expand Up @@ -188,9 +188,9 @@ length(r::FloatRange) = integer(r.len)
function length{T<:Union(Int,Uint,Int64,Uint64)}(r::StepRange{T})
isempty(r) && return zero(T)
if r.step > 1
return checked_add(oftype(T, div(unsigned(r.stop - r.start), r.step)), one(T))
return checked_add(convert(T, div(unsigned(r.stop - r.start), r.step)), one(T))
elseif r.step < -1
return checked_add(oftype(T, div(unsigned(r.start - r.stop), -r.step)), one(T))
return checked_add(convert(T, div(unsigned(r.start - r.stop), -r.step)), one(T))
else
checked_add(div(checked_sub(r.stop, r.start), r.step), one(T))
end
Expand All @@ -213,12 +213,12 @@ let smallint = (Int === Int64 ?
length{T <: smallint}(r::UnitRange{T}) = int(r.stop) - int(r.start) + 1
end

first{T}(r::OrdinalRange{T}) = oftype(T, r.start)
first{T}(r::OrdinalRange{T}) = convert(T, r.start)
first(r::FloatRange) = r.start/r.divisor

last{T}(r::StepRange{T}) = r.stop
last(r::UnitRange) = r.stop
last{T}(r::FloatRange{T}) = oftype(T, (r.start + (r.len-1)*r.step)/r.divisor)
last{T}(r::FloatRange{T}) = convert(T, (r.start + (r.len-1)*r.step)/r.divisor)

minimum(r::UnitRange) = isempty(r) ? error("range must be non-empty") : first(r)
maximum(r::UnitRange) = isempty(r) ? error("range must be non-empty") : last(r)
Expand All @@ -235,18 +235,18 @@ copy(r::Range) = r
## iteration

start(r::FloatRange) = 0
next{T}(r::FloatRange{T}, i) = (oftype(T, (r.start + i*r.step)/r.divisor), i+1)
next{T}(r::FloatRange{T}, i) = (convert(T, (r.start + i*r.step)/r.divisor), i+1)
done(r::FloatRange, i) = (length(r) <= i)

# NOTE: For ordinal ranges, we assume start+step might be from a
# lifted domain (e.g. Int8+Int8 => Int); use that for iterating.
start(r::StepRange) = convert(typeof(r.start+r.step), r.start)
next{T}(r::StepRange{T}, i) = (oftype(T,i), i+r.step)
next{T}(r::StepRange{T}, i) = (convert(T,i), i+r.step)
done{T,S}(r::StepRange{T,S}, i) = isempty(r) | (i < min(r.start, r.stop)) | (i > max(r.start, r.stop))
done{T,S}(r::StepRange{T,S}, i::Integer) = isempty(r) | (i == r.stop+r.step)

start(r::UnitRange) = oftype(r.start+1, r.start)
next{T}(r::UnitRange{T}, i) = (oftype(T,i), i+1)
next{T}(r::UnitRange{T}, i) = (convert(T,i), i+1)
done(r::UnitRange, i) = i==oftype(i,r.stop)+1


Expand All @@ -256,11 +256,11 @@ getindex(r::Range, i::Real) = getindex(r, to_index(i))

function getindex{T}(r::Range{T}, i::Integer)
1 <= i <= length(r) || error(BoundsError)
oftype(T, first(r) + (i-1)*step(r))
convert(T, first(r) + (i-1)*step(r))
end
function getindex{T}(r::FloatRange{T}, i::Integer)
1 <= i <= length(r) || error(BoundsError)
oftype(T, (r.start + (i-1)*r.step)/r.divisor)
convert(T, (r.start + (i-1)*r.step)/r.divisor)
end

function check_indexingrange(s, r)
Expand Down
3 changes: 1 addition & 2 deletions doc/manual/performance-tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ some cases. But it can easily be fixed as follows::
pos(x) = x < 0 ? zero(x) : x

There is also a ``one`` function, and a more general ``oftype(x,y)``
function, which returns ``y`` converted to the type of ``x``. The first
argument to any of these functions can be either a value or a type.
function, which returns ``y`` converted to the type of ``x``.

Avoid changing the type of a variable
-------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ All Objects

.. function:: oftype(x, y)

Convert ``y`` to the type of ``x``.
Convert ``y`` to the type of ``x`` (``convert(typeof(x), y)``).

.. function:: widen(type | x)

Expand Down

0 comments on commit 5039cb1

Please sign in to comment.