Skip to content

Commit

Permalink
Merge pull request #6183 from JuliaLang/anj/zeroone
Browse files Browse the repository at this point in the history
Restrict zero and one to Number types. Default to floating points.
  • Loading branch information
JeffBezanson committed Apr 4, 2014
2 parents cbc9f40 + 764e11f commit bc1ea97
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions base/number.jl
Expand Up @@ -42,6 +42,11 @@ reinterpret{T<:Real}(::Type{T}, x::Real) = box(T,x)

map(f::Callable, x::Number) = f(x)

zero(x::Number) = oftype(x,0.0)
zero{T<:Number}(::Type{T}) = oftype(T,0.0)
one(x::Number) = oftype(x,1.0)
one{T<:Number}(::Type{T}) = oftype(T,1.0)

const _numeric_conversion_func_names =
(:int,:integer,:signed,:int8,:int16,:int32,:int64,:int128,
:uint,:unsigned,:uint8,:uint16,:uint32,:uint64,:uint128,
Expand Down
3 changes: 0 additions & 3 deletions base/operators.jl
Expand Up @@ -143,9 +143,6 @@ 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)

zero(x) = oftype(x,0)
one(x) = oftype(x,1)

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

sizeof(T::Type) = error(string("size of type ",T," unknown"))
Expand Down
5 changes: 5 additions & 0 deletions base/pointer.jl
Expand Up @@ -61,3 +61,8 @@ eltype{T}(::Ptr{T}) = T
+(x::Ptr, y::Integer) = oftype(x, uint(uint(x) + y))
-(x::Ptr, y::Integer) = oftype(x, uint(uint(x) - y))
+(x::Integer, y::Ptr) = y + x

zero{T}(::Type{Ptr{T}}) = convert(Ptr{T}, 0)
zero{T}(x::Ptr{T}) = convert(Ptr{T}, 0)
one{T}(::Type{Ptr{T}}) = convert(Ptr{T}, 1)
one{T}(x::Ptr{T}) = convert(Ptr{T}, 1)
5 changes: 5 additions & 0 deletions base/rational.jl
Expand Up @@ -110,6 +110,11 @@ typemax{T<:Integer}(::Type{Rational{T}}) = one(T)//zero(T)

isinteger(x::Rational) = x.den == 1

zero{T}(::Type{Rational{T}}) = zero(T)//one(T)
zero{T}(q::Rational{T}) = zero(T)//one(T)
one{T}(::Type{Rational{T}}) = one(T)//one(T)
one{T}(q::Rational{T}) = one(T)//one(T)

hash(x::Rational) = bitmix(hash(x.num), ~hash(x.den))

-(x::Rational) = (-x.num) // x.den
Expand Down
2 changes: 1 addition & 1 deletion test/numbers.jl
Expand Up @@ -1575,7 +1575,7 @@ approx_eq(a, b) = approx_eq(a, b, 1e-6)
for T in [Int,BigInt], n = [1:1000,1000000]
n = convert(T,n)
f = factor(n)
@test n == prod([p^k for (p,k)=f])
@test n == prod(T[p^k for (p,k)=f])
prime = n!=1 && length(f)==1 && get(f,n,0)==1
@test isprime(n) == prime

Expand Down
4 changes: 2 additions & 2 deletions test/sparse.jl
Expand Up @@ -209,8 +209,8 @@ for i=1:2, a={[1 2 3], [1 2 3]', speye(3)}
end

# test for "access to undefined error" types that initially allocate elements as #undef
@test all(sparse(1:2, 1:2, Any[1,2])^2 == sparse(1:2, 1:2, [1,4]))
sd1 = diff(sparse([1,1,1], [1,2,3], Any[1,2,3]), 1)
@test all(sparse(1:2, 1:2, Number[1,2])^2 == sparse(1:2, 1:2, [1,4]))
sd1 = diff(sparse([1,1,1], [1,2,3], Number[1,2,3]), 1)

# issue #6036
P = spzeros(Float64, 3, 3)
Expand Down

0 comments on commit bc1ea97

Please sign in to comment.