Skip to content

Commit 8016f1e

Browse files
committed
functions: migrate math section
Signed-off-by: Luca Zeuch <[email protected]>
1 parent 8ee3a5e commit 8016f1e

File tree

1 file changed

+199
-38
lines changed

1 file changed

+199
-38
lines changed

content/docs/reference/templates/functions.md

Lines changed: 199 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -634,47 +634,208 @@ object](/docs/reference/templates/syntax-and-data#message). Is also valid for ep
634634

635635
## Math
636636

637-
{{< callout context="note" title="Note" icon="outline/info-circle" >}}
637+
### add
638638

639-
Boolean logic (and, not, or) and comparison operators (eq, gt, lt, etc.) are covered in [conditional
640-
branching](/docs/reference/templates/syntax-and-data#if-conditional-branching).
639+
```yag
640+
{{ $sum := add x y [...] }}
641+
```
641642

642-
{{< /callout >}}
643+
Returns the sum of the provided numbers. Detects the first number's type and performs the operation accordingly.
644+
645+
### bitwiseAnd
646+
647+
```yag
648+
{{ $result := bitwiseAnd x y }}
649+
```
650+
651+
Performs a bitwise AND operation on the two provided numbers and returns the result.
652+
653+
### bitwiseAndNot
654+
655+
```yag
656+
{{ $result := bitwiseAndNot x y }}
657+
```
658+
659+
Performs a bitwise AND NOT operation on the two provided numbers and returns the result.
660+
661+
### bitwiseNot
662+
663+
```yag
664+
{{ $result := bitwiseNot x }}
665+
```
666+
667+
Performs a bitwise NOT operation on the provided number and returns the result.
668+
669+
### bitwiseOr
670+
671+
```yag
672+
{{ $result := bitwiseOr x y [...] }}
673+
```
674+
675+
Performs a bitwise OR operation on the provided numbers and returns the result.
676+
677+
### bitwiseXor
678+
679+
```yag
680+
{{ $result := bitwiseXor x y }}
681+
```
682+
683+
Performs a bitwise XOR operation on the two provided numbers and returns the result.
684+
685+
### bitwiseLeftShift
686+
687+
```yag
688+
{{ $result := bitwiseLeftShift x y }}
689+
```
690+
691+
Shifts X left by Y bits and returns the result.
692+
693+
### bitwiseRightShift
694+
695+
```yag
696+
{{ $result := bitwiseRightShift x y }}
697+
```
698+
699+
Shifts X right by Y bits and returns the result.
700+
701+
### cbrt
702+
703+
```yag
704+
{{ $result := cbrt x }}
705+
```
706+
707+
Returns the cube root of the provided number.
708+
709+
### div
710+
711+
```yag
712+
{{ $result := div x y [...] }}
713+
```
714+
715+
Performs division on the provided numbers. Detects the first number's type and performs the operation accordingly.
716+
If you need a floating-point number as a result of integer division, use [fdiv](#fdiv).
717+
718+
### fdiv
719+
720+
```yag
721+
{{ $result := fdiv x y [...] }}
722+
```
723+
724+
Special case of [div](#div); always returns a floating-point number as result.
725+
726+
### log
727+
728+
```yag
729+
{{ $result := log x [base] }}
730+
```
731+
732+
Returns the logarithm of X with the given base. If no base is provided, the natural logarithm is used.
733+
734+
### mathConst
735+
736+
```yag
737+
{{ $result := mathConst "constant" }}
738+
```
739+
740+
Returns the value of the specified math constant. See the [math constants list](https://pkg.go.dev/math#pkg-constants).
741+
742+
### max
743+
744+
```yag
745+
{{ $result := max x y }}
746+
```
747+
748+
Returns the larger of the two provided numbers.
749+
750+
### min
751+
752+
```yag
753+
{{ $result := min x y }}
754+
```
755+
756+
Returns the smaller of the two provided numbers.
757+
758+
### mod
759+
760+
```yag
761+
{{ $result := mod x y }}
762+
```
763+
764+
Returns the floating-point remainder of the division of X by Y.
765+
Takes the sign of X, so `mod -5 3` results in `-2`, not `1`. To ensure a non-negative result, use `mod` twice.
766+
767+
### mult
768+
769+
```yag
770+
{{ $result := mult x y [...] }}
771+
```
772+
773+
Performs multiplication on the provided numbers. Detects the first number's type and returns the result accordingly.
774+
775+
### pow
776+
777+
```yag
778+
{{ $result := pow x y }}
779+
```
780+
781+
Returns X raised to the power of Y as a floating-point number.
782+
783+
### randInt
784+
785+
```yag
786+
{{ $result := randInt [start] stop }}
787+
```
788+
789+
Returns a random integer in the right-closed interval of `[0, stop)` or `[start, stop)` if two arguments are provided.
790+
That is, the result is always greater than or equal to `start` and strictly less than `stop`.
791+
792+
### round
793+
794+
```yag
795+
{{ $result := round x }}
796+
```
797+
798+
Returns the nearest integer to X as float. Normal rounding rules apply.
799+
800+
### roundCeil
801+
802+
```yag
803+
{{ $result := roundCeil x }}
804+
```
805+
806+
Returns the smallest integer greater than or equal to X. Put simply, always round up.
807+
808+
### roundEven
809+
810+
```yag
811+
{{ $result := roundEven x }}
812+
```
813+
814+
Returns the nearest integer to X, rounding ties (x.5) to the nearest even integer.
815+
816+
### roundFloor
817+
818+
```yag
819+
{{ $result := roundFloor x }}
820+
```
821+
822+
Returns the largest integer less than or equal to X. Put simply, always round down.
823+
824+
### sqrt
825+
826+
```yag
827+
{{ $result := sqrt x }}
828+
```
829+
830+
Returns the square root of X as a floating-point number.
831+
832+
### sub
833+
834+
```yag
835+
{{ $result := sub x y [...] }}
836+
```
643837

644-
| **Function** | **Description** |
645-
| ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
646-
| `add` x y z ... | Returns x + y + z + ..., detects first number's type - is it _int_ or _float_ and based on that adds. (use `toFloat` on the first argument to force floating point math.)`{{add 5 4 3 2 -1}}` sums all these numbers and returns `13`. |
647-
| `bitwiseAnd` x y | The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0. Example: `{{bitwiseAnd 12 25}}` returns `8`, that in binary 00001100 AND 00011001 is 00001000. |
648-
| `bitwiseAndNot` x y | This function is called bit clear because of AND NOT. For example in the expression z = x AND NOT y, each bit of z is 0 if the corresponding bit of y is 1; otherwise it equals to the corresponding bit of x. `{{bitwiseAndNot 7 12}}` returns `3`, that is 0111 AND NOT 1100 is 11. |
649-
| `bitwiseNot` x | The bitwise NOT operator inverts the bits of the argument. Example: `{{bitwiseNot 7}}` returns `-8`. that in binary 0111 to 1000 |
650-
| `bitwiseOr` x y z... | The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. Example: `{{bitwiseOr 12 25}}` returns `29`, that in binary 00001100 OR 00011001 is 00011101. |
651-
| `bitwiseXor` x y | The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. Example: `{{bitwiseXor 12 25}}` returns `21`, that in binary 00001100 OR 00011001 is 00010101. |
652-
| `bitwiseLeftShift` x y | Left shift operator shifts all bits towards left by a certain number of specified bits. The bit positions that have been vacated by the left shift operator are filled with 0. Example: `{{range seq 0 3}} {{bitwiseLeftShift 212 .}} {{end}}` returns `212 424 848` |
653-
| `bitwiseRightShift` x y | Right shift operator shifts all bits towards right by certain number of specified bits. Example: `{{range seq 0 3}} {{bitwiseRightShift 212 .}} {{end}}` returns `212 106 53`. |
654-
| `cbrt` x | Returns the cube root of given argument in type _float64_ e.g. `{{cbrt 64}}` returns `4`. |
655-
| `div` x y z ... | Division, like `add` or `mult`, detects first number's type first. `{{div 11 3}}` returns `3` whereas `{{div 11.1 3}}` returns `3.6999999999999997` |
656-
| `fdiv` x y z ... | Meant specifically for floating point numbers division. |
657-
| `log` x (base) | Log is a logarithm function using (log base of x). Arguments can be any type of numbers, as long as they follow logarithm logic. Return value is of type _float64_. If base argument is not given It is using natural logarithm (base e - The Euler's constant) as default.`{{ log "123" 2 }}` will return `6.94251450533924`. |
658-
| `mathConst` "arg" | Function returns all constants available in go's math package as _float64_. `"arg"` has to be a case-insensitive _string_ from [math constants list](https://pkg.go.dev/[email protected]#pkg-constants). For example `{{mathConst "sqrtphi"}}` would return `1.272019649514069`. |
659-
| `max` x y | Returns the larger of x or y as type _float64_. |
660-
| `min` x y | Returns the smaller of x or y as type _float64_. |
661-
| `mod` x y | Mod returns the floating-point remainder of the division of x by y. For example, `mod 17 3` returns `2` as type _float64_.<br><br>Note that like Go's `[math.Mod](https://pkg.go.dev/math#Mod)` function, `mod` takes the sign of `x`, so `mod -5 3` results in `-2`, not `1`. To ensure a non-negative result, use `mod` twice, like such: `mod (add (mod x y) y) y`. |
662-
| `mult` x y z ... | Multiplication, like `add` or `div`, detects first number's type. `{{mult 3.14 2}}` returns `6.28` |
663-
| `pow` x y | Pow returns x\*\*y, the base-x exponential of y which have to be both numbers. Type is returned as _float64_. `{{ pow 2 3 }}` returns `8`. |
664-
| `randInt` (stop, or start stop) | Returns a random integer between 0 and stop, or start - stop if two args are provided.Result will be `start &#x3C;= random number &#x3C; stop`. Without arguments, range is 0..10. Example in section's [Snippets](#math-sections-snippets). |
665-
| `round` x | Returns the nearest integer, rounding half away from zero. Regular rounding > 10.4 is `10` and 10.5 is `11`. All round functions return type _float64_, so use conversion functions to get integers. For more complex rounding, example in section's [Snippets](#math-sections-snippets). |
666-
| `roundCeil` x | Returns the least integer value greater than or equal to input or rounds up. `{{roundCeil 1.1}}` returns `2`. |
667-
| `roundEven` x | Returns the nearest integer, rounding ties to even.<br>`{{roundEven 10.5}}` returns `10 {{roundEven 11.5}}` returns `12`. |
668-
| `roundFloor` x | Returns the greatest integer value less than or equal to input or rounds down. `{{roundFloor 1.9}}` returns `1`. |
669-
| `sqrt` x | Returns the square root of a number as type _float64_.<br>`{{sqrt 49}}` returns `7, {{sqrt 12.34 \|printf "%.4f"}} returns 3.5128` |
670-
| `sub` x y z ... | Returns x - y -z - ... Works like add, just subtracts. |
671-
672-
### Math section's snippets
673-
674-
- `{{$d := randInt 10}}` Stores random _int_ into variable `$d` (a random number from 0-9).
675-
- To demonstrate rounding float to 2 decimal places.\
676-
`{{div (round (mult 12.3456 100)) 100}}` returns 12.35\
677-
`{{div (roundFloor (mult 12.3456 100)) 100}}` returns 12.34
838+
Subtracts the provided numbers from each other. Detects the first number's type and returns the result accordingly.
678839

679840
## Member
680841

0 commit comments

Comments
 (0)