Well, almost... When `Inf` or `NaN` are involved, this may not hold, for example `1 + Inf < 2 + Inf` is actually `false`. As would be `1 + (typemax(1)-1) < 2 + (typemax(1)-1)`.
So adding or subtracting most any finite value from an inequality will preserve the inequality, just as it does for equations.
What about addition and multiplication?
Consider the case $a < b$ and $c > 0$. Then $ca < cb$. Here we investigate using ``3`` random values (which will be positive):
```julia;hold=true
a,b,c = rand(3) # 3 random numbers in [0,1)
a < b, c*a < c*b
```
Whenever these two commands are run, the two logical values should be identical, even though the specific values of `a`, `b`, and `c` will vary.
The restriction that $c > 0$ is needed. For example, if $c = -1$, then we have $a < b$ if and only if $-a > -b$. That is the inequality is "flipped."
```julia;hold=true
a,b = rand(2)
a < b, -a > -b
```
Again, whenever this is run, the two logical values should be the same.
The values $a$ and $-a$ are the same distance from $0$, but on opposite sides. Hence if $0 < a < b$, then $b$ is farther from $0$ than $a$, so $-b$ will be farther from $0$ than $-a$, which in this case says $-b < -a$, as expected.
Finally, we have the case of division. The relation of $x$ and $1/x$
(for $x > 0$) is that the farther $x$ is from $0$, the closer $1/x$ is
to $0$. So large values of $x$ make small values of $1/x$. This leads
to this fact for $a,b > 0$: $a < b$ if and only if $1/a > 1/b$.
We can check with random values again:
```julia;hold=true
a,b = rand(2)
a < b, 1/a > 1/b
```
In summary we investigated numerically that the following hold:
- `a < b` if and only if `a + c < b + c` for all finite `a`, `b`, and `c`.
- `a < b` if and only if `c*a < c*b` for all finite `a` and `b`, and finite, positive `c`.
- `a < b` if and only if `-a > -b` for all finite `a` and `b`.
- `a < b` if and only if `1/a > 1/b` for all finite, positive `a` and `b`.
The absolute value notation can be defined through cases:
```math
\lvert x\rvert = \begin{cases}
x & x \geq 0\\
-x & \text{otherwise}.
\end{cases}
```
The interpretation of $\lvert x\rvert$, as the distance on the number line of $x$
from $0$, means that many relationships are naturally expressed in
terms of absolute values. For example, a simple shift: $\lvert x -c\rvert$ is
related to the distance $x$ is from the number $c$. As common as they are, the concept can still be confusing when inequalities are involved.
For example, the expression $\lvert x - 5\rvert < 7$ has solutions which are all
values of $x$ within $7$ units of $5$. This would be the values $-2< x < 12$.
If this isn't immediately intuited, then formally $\lvert x - 5\rvert <7$
is a compact representation of a chain of inequalities: $-7 < x-5 < 7$.
(Which is really two combined inequalities: $-7 < x-5$ *and* $x-5 < 7$.)
We can "add" ``5`` to each side to get $-2 < x < 12$, using the
fact that adding by a finite number does not change the inequality
sign.
Julia's precedence for logical
expressions, allows such statements to mirror the mathematical
notation:
```julia;
x = 18
abs(x - 5) < 7
```
This is to be expected, but we could also have written:
```julia;
-7 < x - 5 < 7
```
Read aloud this would be "minus ``7`` is less than ``x`` minus ``5``
**and** ``x`` minus ``5`` is less than ``7``".
The "and" equations can be combined as above with a natural notation. However,
an equation like $\lvert x - 5\rvert > 7$ would emphasize
an **or** and be "``x`` minus ``5`` less than minus ``7`` **or** ``x`` minus ``5``
greater than ``7``". Expressing this requires some new notation.
The *boolean shortcut operators* `&&` and `||` implement "and" and "or." (There are also *bitwise* boolean operators `&` and `|`, but we only describe the former.)
Thus we could write $-7 < x-5 < 7$ as
```julia;
(-7 < x - 5) && (x - 5 < 7)
```
and could write $\lvert x-5\rvert > 7$ as
```julia;
(x - 5 < -7) || (x - 5 > 7)
```
(The first expression is false for $x=18$ and the second expression true, so the "or"ed result is `true` and the "and" result if `false`.)
##### Example
One of [DeMorgan's Laws](http://en.wikipedia.org/wiki/De_Morgan%27s_laws) states that
"not (A and B)" is the same as "(not A) or (not B)". This is a kind of
distributive law for "not", but note how the "and" changes to
"or". We can verify this law systematically. For example, the
following shows it true for ``1`` of the ``4`` possible cases for the pair
`A`, `B` to take:
```julia;
A,B = true, false ## also true, true; false, true; and false, false
!(A && B) == !A || !B
```
## Precedence
The question of when parentheses are needed and when they are not is answered by the
To represent $\lvert x-5\rvert > 7$. Were the parentheses necessary? Let's just check.
```julia;
x - 5 < -7 || x - 5 > 7
```
So no, they were not in this case.
An operator (such as `<`, `>`, `||` above) has an associated associativity and precedence. The associativity is whether an expression like `a - b - c` is `(a-b) - c` or `a - (b-c)`. The former being left associative, the latter right. Of issue here is *precedence*, as in with two or more different operations, which happens first, second, ``\dots``.
The table in the manual on [operator precedence and associativity](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity) shows that for these operations "control flow" (the `&&` above) is lower than "comparisons" (the `<`, `>`), which are lower than "Addition" (the `-` above). So the expression without parentheses would be equivalent to:
```julia
((x-5) < -7) && ((x-5) > 7)
```
(This is different than the precedence of the bitwise boolean operators, which have `&` with "Multiplication" and `|` with "Addition", so `x-5 < 7 | x - 5 > 7` would need parentheses.)
A thorough understanding of the precedence rules can help eliminate
unnecessary parentheses, but in most cases it is easier just to put
them in.
## Arithmetic with
For convenience, basic arithmetic can be performed with Boolean
values, `false` becomes $0$ and true $1$. For example, both these
expressions make sense:
```julia;
true + true + false, false * 1000
```
The first example shows a common means used to count the number of
`true` values in a collection of Boolean values - just add them.
This can be cleverly exploited. For example, the following expression returns `x` when it is positive and $0$ otherwise:
```julia;
(x > 0) * x
```
There is a built in function, `max` that can be used for this: `max(0, x)`.
This expression returns `x` if it is between $-10$ and $10$ and otherwise $-10$ or $10$ depending on whether $x$ is negative or positive.
The IEEE 754 standard is about floating point numbers, for which there are the special values `Inf`, `-Inf`, `NaN`, and, surprisingly, `-0.0` (as a floating point number and not `-0`, an integer). Here are 4 facts that seem reasonable:
- Positive zero is equal but not greater than negative zero.
- `Inf` is equal to itself and greater than everything else except `NaN`.
- `-Inf` is equal to itself and less then everything else except `NaN`.
- `NaN` is not equal to, not less than, and not greater than anything, including itself.
Do all four seem to be the case within `Julia`? Find your answer by trial and error.
```julia; hold=true; echo=false;
yesnoq(true)
```
###### Question
The `NaN` value is meant to signal an error in computation. `Julia` has value to indicate some data is missing or unavailable. This is `missing`. For `missing` values we have these computations:
```julia;
true && missing, true || missing
```
We see the value of `true || missing` is `true`. Why?
```julia; hold=true; echo=false;
choices = ["""
In the manual we can read that "In the expression `a || b`, the subexpression `b` is only evaluated if `a` evaluates to false." In this case `a` is `true` and so `a` is returned.
""",
"Since the second value is \"`missing`\", only the first is used. So `false || missing` would also be `false`"]
The value for `true && missing` is `missing`, not a boolean value. What happens?
```julia; hold=true; echo=false;
choices = ["""
In the manual we can read that "In the expression `a && b`, the subexpression `b` is only evaluated if `a` evaluates to true." In this case, `a` is `false` so `b` is evaluated and returned. As `b` is just `missing` that is the return value.
""",
"Since the second value is \"`missing`\" all such answers would be missing."]