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

Restrict zero and one to Number types. Default to floating points. #6183

Merged
merged 1 commit into from
Apr 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions base/number.jl
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have one(Ptr{Int})? If I need a zero pointer I use C_NULL, but I can't see any reason to have a one pointer.

5 changes: 5 additions & 0 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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