Skip to content

Commit

Permalink
make bool arithmetic consistently promote to Int.
Browse files Browse the repository at this point in the history
fixes Gunnar's issue 5, part of #2980
  • Loading branch information
JeffBezanson committed May 1, 2013
1 parent 6b53b1b commit 1f07511
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 9 additions & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -883,10 +883,18 @@ promote_array_type{S<:Integer}(::Type{S}, ::Type{Bool}) = S

.^(x::StridedArray, y::StridedArray) =
reshape([ x[i] ^ y[i] for i=1:length(x) ], promote_shape(size(x),size(y)))
.^(x::Number, y::StridedArray) =
.^{T<:Integer}(x::StridedArray{Bool}, y::StridedArray{T}) =
reshape([ bool(x[i] ^ y[i]) for i=1:length(x) ], promote_shape(size(x),size(y)))

.^(x::Number, y::StridedArray) =
reshape([ x ^ y[i] for i=1:length(y) ], size(y))
.^(x::Bool , y::StridedArray) =
reshape([ bool(x ^ y[i]) for i=1:length(y) ], size(y))

.^(x::StridedArray, y::Number ) =
reshape([ x[i] ^ y for i=1:length(x) ], size(x))
.^(x::StridedArray{Bool}, y::Integer) =
reshape([ bool(x[i] ^ y) for i=1:length(x) ], size(x))

for f in (:+, :-, :.*, :./, :div, :mod, :&, :|, :$)
@eval begin
Expand Down
8 changes: 4 additions & 4 deletions base/bool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ abs(x::Bool) = int(x)

+(x::Bool, y::Bool) = int(x)+int(y)
-(x::Bool, y::Bool) = int(x)-int(y)
*(x::Bool, y::Bool) = x&y
*(x::Bool, y::Bool) = int(x&y)
/(x::Bool, y::Bool) = int(x)/int(y)
^(x::Bool, y::Bool) = x|!y
^(x::Bool, y::Bool) = int(x|!y)
^(x::Integer, y::Bool) = y ? x : one(x)

div(x::Bool, y::Bool) = y ? x : throw(DivideError())
div(x::Bool, y::Bool) = y ? int(x) : throw(DivideError())
fld(x::Bool, y::Bool) = div(x,y)
rem(x::Bool, y::Bool) = y ? false : throw(DivideError())
rem(x::Bool, y::Bool) = y ? 0 : throw(DivideError())
mod(x::Bool, y::Bool) = rem(x,y)

0 comments on commit 1f07511

Please sign in to comment.