website: update docs for precedence rules
This commit is contained in:
parent
9daa9942b3
commit
8fab86a9d4
|
@ -381,19 +381,21 @@ The supported operations are:
|
|||
- *Add* (`+`), *Subtract* (`-`), *Multiply* (`*`), and *Divide* (`/`) for **float** types
|
||||
- *Add* (`+`), *Subtract* (`-`), *Multiply* (`*`), *Divide* (`/`), and *Modulo* (`%`) for **integer** types
|
||||
|
||||
Operator precedences is the standard mathematical order of operations:
|
||||
*Multiply* (`*`), *Divide* (`/`), and *Modulo* (`%`) have precedence over
|
||||
*Add* (`+`) and *Subtract* (`-`). Parenthesis can be used to force ordering.
|
||||
|
||||
```
|
||||
"${2 * 4 + 3 * 3}" # computes to 17
|
||||
"${3 * 3 + 2 * 4}" # computes to 17
|
||||
"${2 * (4 + 3) * 3}" # computes to 42
|
||||
```
|
||||
|
||||
You can use the [terraform console](/docs/commands/console.html) command to
|
||||
try the math operations.
|
||||
|
||||
-> **Note:** Since Terraform allows hyphens in resource and variable names,
|
||||
it's best to use spaces between math operators to prevent confusion or unexpected
|
||||
behavior. For example, `${var.instance-count - 1}` will subtract **1** from the
|
||||
`instance-count` variable value, while `${var.instance-count-1}` will interpolate
|
||||
the `instance-count-1` variable value.
|
||||
|
||||
|
||||
-> **Note:** Operator precedence is not the usual one where *Multiply* (`*`),
|
||||
*Divide* (`/`), and *Modulo* (`%`) have precedence over *Add* (`+`) and *Subtract* (`-`).
|
||||
The operations are made in the order they appear. Parenthesis can be used to force ordering :
|
||||
```
|
||||
"${2 * 4 + 3 * 3}" # computes to 33
|
||||
"${3 * 3 + 2 * 4}" # computes to 44
|
||||
"${(2 * 4) + (3 * 3)}" # computes to 17
|
||||
"${(3 * 3) + (2 * 4)}" # computes to 17
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue