Why does false ÷ true evaluate to false?
Asked Answered
O

2

9

Booleans in Julia, when used in calculations, have numeric values (false = 0, true = 1).

All of the following have expected results:

true + false        #  1
false - true        #  -1
false / true        # 0.0
true ÷ false        # DivideError: integer division error
0 ÷ 1               # 0

Except for:

false ÷ true        # false

So why does false ÷ true evaluate to false, instead of 0 like 0 ÷ 1 does?


Update: it appears multiplication has the same behavior:

false * true        # false

I can understand that there may different behavior for type Bool than other numeric types, but it's strange that addition and subtraction behave differently than multiplication and division.

The Mathematical Operations documentation states:

Note that Bool is an integer type and all the usual promotion rules and numeric operators are also defined on it.

so it's a little surprising that it doesn't treat booleans as integers in all arithmetic contexts.

Occidentalize answered 12/12, 2022 at 17:33 Comment(4)
Iirc the ÷ operator is an alias for the Base.div() function. I am not sure and have never used Julia, but I guess that the div function always tries to return something of the same type as the operands, therefore a boolean. Guess: div internally does bool->int conversion, does the int division and then casts back to boolean. For false÷true that works, for true÷false the integer division step fails.Blob
Or the Julia compiler wants to answer math problems in as near types as possible, and therefore answers here in boolean values. Technically in a way 0 ÷ 1 is the same as false ÷ true.Tarpeia
Yes, it's the same as calling div(false, true) docs.julialang.org/en/v1/manual/mathematical-operations/…Ambur
@TorgeRosendahl: Your guess is not quite right, the code works entirely in Boolean. Here's the definition: div(x::Bool, y::Bool) = y ? x : throw(DivideError())Starlike
O
2

As @DNF pointed out, the div() decision is implemented in Boolean logic.

The specific method definitions are in the base code of the Julia source tree (julia/base/bool.jl).

Addition and subtraction convert Bool to Int:

+(x::Bool, y::Bool) = Int(x) + Int(y)
-(x::Bool, y::Bool) = Int(x) - Int(y)

but division does not:

div(x::Bool, y::Bool) = y ? x : throw(DivideError())

I do still find it a bit odd that not all Bool operations are treated as numeric types but clearly it's intended to be that way.

Occidentalize answered 13/12, 2022 at 0:32 Comment(1)
Quite counter-intuitive. In C that would have been UB, meaning printing 'rabbits' is quite valid.Sphagnum
A
2

The reasoning is that the division of 2 bools is always a bool or error, while other arithmetic on bools can produce numbers outside the range of a bool.

Affidavit answered 12/12, 2022 at 17:41 Comment(4)
I've revised the question - multiplication has the same behavior, so it's not just specific to division.Occidentalize
multiplication has the same answer. all outputs of Bool*Bool fit into a BoolAffidavit
OK, I can see the logic there - division, multiplication, and even exponentiation would result in 0 or 1, so the result can fix in a Bool. Is this behavior documented anywhere? I couldn't find it.Occidentalize
I'm not sure this is documented, and it's a little ad-hoc given that (e.g.) Int8+Int8=Int8, but I think the decision was just that with Bool, if you want the version with overflow, it's just xor anyway.Affidavit
O
2

As @DNF pointed out, the div() decision is implemented in Boolean logic.

The specific method definitions are in the base code of the Julia source tree (julia/base/bool.jl).

Addition and subtraction convert Bool to Int:

+(x::Bool, y::Bool) = Int(x) + Int(y)
-(x::Bool, y::Bool) = Int(x) - Int(y)

but division does not:

div(x::Bool, y::Bool) = y ? x : throw(DivideError())

I do still find it a bit odd that not all Bool operations are treated as numeric types but clearly it's intended to be that way.

Occidentalize answered 13/12, 2022 at 0:32 Comment(1)
Quite counter-intuitive. In C that would have been UB, meaning printing 'rabbits' is quite valid.Sphagnum

© 2022 - 2024 — McMap. All rights reserved.