From 6b7ede3c82e13f4cdb18e6dcc6be1699f846ad24 Mon Sep 17 00:00:00 2001 From: Stefan Karpinski Date: Sun, 25 Dec 2011 14:07:23 -0500 Subject: [PATCH] Compare with Uint64 values correctly. Previously `-1 < typemax(Uint64)` was false. --- j/int.j | 21 ++++++++++++++++++++- j/operators.j | 16 ++++++++-------- j/promotion.j | 4 ++-- test/core.j | 8 ++++++++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/j/int.j b/j/int.j index 7bc3346a10eb6..81d56b51a949b 100644 --- a/j/int.j +++ b/j/int.j @@ -104,7 +104,7 @@ convert(::Type{Unsigned}, x::Bool ) = convert(Uint8, x) convert(::Type{Unsigned}, x::Int8 ) = convert(Uint8, x) convert(::Type{Unsigned}, x::Int16 ) = convert(Uint16, x) convert(::Type{Unsigned}, x::Int32 ) = convert(Uint32, x) -convert(::Type{Unsigned}, x::Int64 ) = convert(Uint64, x) # LOSSY +convert(::Type{Unsigned}, x::Int64 ) = convert(Uint64, x) convert(::Type{Unsigned}, x::Float32) = convert(Uint32, x) convert(::Type{Unsigned}, x::Float64) = convert(Uint64, x) @@ -392,6 +392,25 @@ trailing_zeros(x::Uint64) = boxui64(cttz_int(unbox64(x))) <=(x::Uint32, y::Uint32) = ule_int(unbox32(x),unbox32(y)) <=(x::Uint64, y::Uint64) = ule_int(unbox64(x),unbox64(y)) +# this would be unncessary if we had Signed: +==(x::Unsigned, y::Uint64) = uint64(x) == y +==(x::Uint64, y::Unsigned) = x == uint64(y) +!=(x::Unsigned, y::Uint64) = uint64(x) != y +!=(x::Uint64, y::Unsigned) = x != uint64(y) +< (x::Unsigned, y::Uint64) = uint64(x) < y +< (x::Uint64, y::Unsigned) = x < uint64(y) +<=(x::Unsigned, y::Uint64) = uint64(x) <= y +<=(x::Uint64, y::Unsigned) = x <= uint64(y) + +==(x::Integer, y::Uint64) = x < 0 ? false : uint64(x) == y +==(x::Uint64, y::Integer) = y < 0 ? false : x == uint64(y) +!=(x::Integer, y::Uint64) = x < 0 ? true : uint64(x) != y +!=(x::Uint64, y::Integer) = y < 0 ? true : x != uint64(y) +< (x::Integer, y::Uint64) = x < 0 ? true : uint64(x) < y +< (x::Uint64, y::Integer) = y <= 0 ? false : x < uint64(y) +<=(x::Integer, y::Uint64) = x <= 0 ? true : uint64(x) <= y +<=(x::Uint64, y::Integer) = y < 0 ? false : x <= uint64(y) + ## traits ## typemin(::Type{Int8 }) = int8(-128) diff --git a/j/operators.j b/j/operators.j index a663ba1629516..c1755d435d5be 100644 --- a/j/operators.j +++ b/j/operators.j @@ -20,8 +20,8 @@ isequal(x::Number, y::Number) = hash(x)==hash(y) && x==y <=(x,y) = !(y < x) >=(x,y) = y <= x -max(x, y) = x > y ? x : y -min(x, y) = x < y ? x : y +max(x,y) = x > y ? x : y +min(x,y) = x < y ? x : y ## definitions providing basic traits of arithmetic operators ## @@ -84,12 +84,12 @@ mod1{T<:Real}(x::T, y::T) = y-mod(y-x,y) cmp{T<:Real}(x::T, y::T) = sign(x-y) # transposed multiply -aCb(a, b) = ctranspose(a)*b -abC(a, b) = a*ctranspose(b) -aCbC(a, b) = ctranspose(a)*ctranspose(b) -aTb(a, b) = transpose(a)*b -abT(a, b) = a*transpose(b) -aTbT(a, b) = transpose(a)*transpose(b) +aCb (a,b) = ctranspose(a)*b +abC (a,b) = a*ctranspose(b) +aCbC(a,b) = ctranspose(a)*ctranspose(b) +aTb (a,b) = transpose(a)*b +abT (a,b) = a*transpose(b) +aTbT(a,b) = transpose(a)*transpose(b) oftype{T}(::Type{T},c) = convert(T,c) oftype{T}(x::T,c) = convert(T,c) diff --git a/j/promotion.j b/j/promotion.j index 0b7d9042f2a3f..6016e37e04f0c 100644 --- a/j/promotion.j +++ b/j/promotion.j @@ -47,9 +47,9 @@ end |(x::Integer, y::Integer) = |(promote(x,y)...) ($)(x::Integer, y::Integer) = ($)(promote(x,y)...) +==(x::Number, y::Number) = (==)(promote(x,y)...) < (x::Real, y::Real) = (< )(promote(x,y)...) <=(x::Real, y::Real) = (<=)(promote(x,y)...) -==(x::Number, y::Number) = (==)(promote(x,y)...) div(x::Real, y::Real) = div(promote(x,y)...) fld(x::Real, y::Real) = fld(promote(x,y)...) @@ -72,5 +72,5 @@ no_op_err(name, T) = error(name," not defined for ",T) |{T<:Integer}(x::T, y::T) = no_op_err("|", T) ($){T<:Integer}(x::T, y::T) = no_op_err("\$", T) -<{T<:Real}(x::T, y::T) = no_op_err("<", T) =={T<:Number}(x::T, y::T) = no_op_err("==", T) +<{T<:Real}(x::T, y::T) = no_op_err("<", T) diff --git a/test/core.j b/test/core.j index 7c530bf45e2b0..4a9624d3bddaa 100644 --- a/test/core.j +++ b/test/core.j @@ -256,6 +256,14 @@ end @assert (ComplexPair(1,2)/ComplexPair(2.5,3.0))*ComplexPair(2.5,3.0) == ComplexPair(1,2) @assert 0.7 < real(sqrt(ComplexPair(0,1))) < 0.707107 +for S = {Int8, Int16, Int32, Int64}, + U = {Uint8, Uint16, Uint32, Uint64} + @assert !(-one(S) == typemax(U)) + @assert -one(S) != typemax(U) + @assert -one(S) < typemax(U) + @assert !(typemax(U) <= -one(S)) +end + # check type of constructed rationals int_types = {Int8, Uint8, Int16, Uint16, Int32, Uint32, Int64, Uint64} for N = int_types, D = int_types