Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does false ÷ true evaluate to false?

Tags:

boolean

julia

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.

like image 427
sj95126 Avatar asked May 28 '26 21:05

sj95126


1 Answers

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.

like image 129
Oscar Smith Avatar answered Jun 01 '26 20:06

Oscar Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!