Skip to content

Commit 252c65e

Browse files
authored
Add explanation on arithmetic operations with Bool values in the manual (#58281)
1 parent fc8e6e7 commit 252c65e

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

doc/src/manual/mathematical-operations.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ julia> 3*2/12
4747
operators. For instance, we would generally write `-x + 2` to reflect that first `x` gets negated,
4848
and then `2` is added to that result.)
4949

50-
When used in multiplication, `false` acts as a *strong zero*:
51-
52-
```jldoctest
53-
julia> NaN * false
54-
0.0
55-
56-
julia> false * Inf
57-
0.0
58-
```
59-
60-
This is useful for preventing the propagation of `NaN` values in quantities that are known to be zero. See [Knuth (1992)](https://arxiv.org/abs/math/9205211) for motivation.
61-
6250
## Boolean Operators
6351

6452
The following [Boolean operators](https://en.wikipedia.org/wiki/Boolean_algebra#Operations) are supported on [`Bool`](@ref) types:
@@ -71,7 +59,29 @@ The following [Boolean operators](https://en.wikipedia.org/wiki/Boolean_algebra#
7159

7260
Negation changes `true` to `false` and vice versa. The short-circuiting operations are explained on the linked page.
7361

74-
Note that `Bool` is an integer type and all the usual promotion rules and numeric operators are also defined on it.
62+
## Arithmetic operations with `Bool` values
63+
64+
Note that `Bool` is an integer type, such that `false` is numerically equal to `0` and `true` is numerically equal to `1`. All the usual promotion rules and numeric operators are also defined on it, with a special behavior of arithmetic (non-Boolean) operations when all the arguments are `Bool`: in those cases, the arguments are promoted to `Int` instead of keeping their type. Compare e.g. the following equivalent operations with `Bool` and with a different numeric type (`UInt8`):
65+
66+
```jldoctest
67+
julia> true - true
68+
0
69+
70+
julia> 0x01 - 0x01
71+
0x00
72+
```
73+
74+
Also, when used in multiplication, `false` acts as a *strong zero*:
75+
76+
```jldoctest
77+
julia> NaN * false
78+
0.0
79+
80+
julia> false * Inf
81+
0.0
82+
```
83+
84+
This is useful for preventing the propagation of `NaN` values in quantities that are known to be zero. See [Knuth (1992)](https://arxiv.org/abs/math/9205211) for motivation.
7585

7686
## Bitwise Operators
7787

0 commit comments

Comments
 (0)