forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool.jl
59 lines (45 loc) · 1.61 KB
/
bool.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
## boolean conversions ##
convert(::Type{Bool}, x::Bool) = x
convert(::Type{Bool}, x::Real) = (x!=0)
# promote Bool to any other numeric type
promote_rule{T<:Number}(::Type{Bool}, ::Type{T}) = T
typemin(::Type{Bool}) = false
typemax(::Type{Bool}) = true
## boolean operations ##
!(x::Bool) = box(Bool,not_int(unbox(Bool,x)))
(~)(x::Bool) = !x
(&)(x::Bool, y::Bool) = box(Bool,and_int(unbox(Bool,x),unbox(Bool,y)))
(|)(x::Bool, y::Bool) = box(Bool,or_int(unbox(Bool,x),unbox(Bool,y)))
($)(x::Bool, y::Bool) = (x!=y)
signbit(x::Bool) = false
sign(x::Bool) = x
abs(x::Bool) = x
abs2(x::Bool) = x
<(x::Bool, y::Bool) = y&!x
<=(x::Bool, y::Bool) = y|!x
## do arithmetic as Int ##
+(x::Bool) = Int(x)
-(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) = x | !y
^(x::Integer, y::Bool) = ifelse(y, x, one(x))
function +{T<:FloatingPoint}(x::Bool, y::T)
ifelse(x, one(promote_type(Bool,T)) + convert(promote_type(Bool,T),y),
convert(promote_type(Bool,T),y))
end
+(y::FloatingPoint, x::Bool) = x + y
function *{T<:Number}(x::Bool, y::T)
ifelse(x, convert(promote_type(Bool,T),y),
ifelse(signbit(y), -zero(promote_type(Bool,T)), zero(promote_type(Bool,T))))
end
function *{T<:Unsigned}(x::Bool, y::T)
ifelse(x, convert(promote_type(Bool,T),y), zero(promote_type(Bool,T)))
end
*(y::Number, x::Bool) = x * y
div(x::Bool, y::Bool) = y ? x : throw(DivideError())
fld(x::Bool, y::Bool) = div(x,y)
cld(x::Bool, y::Bool) = div(x,y)
rem(x::Bool, y::Bool) = y ? false : throw(DivideError())
mod(x::Bool, y::Bool) = rem(x,y)