|
1 | 1 | --- |
2 | 2 | id: math |
| 3 | +sidebar_label: "$(math)" |
| 4 | +description: "Learn how to use the $(math) variable in StreamElements Chatbot for performing mathematical calculations in chat messages." |
3 | 5 | tags: |
4 | 6 | - chatbot |
5 | 7 | - variable |
| 8 | + - math |
| 9 | +keywords: |
| 10 | + - StreamElements |
| 11 | + - chatbot |
| 12 | + - math |
| 13 | + - calculations |
| 14 | + - expressions |
6 | 15 | --- |
7 | 16 |
|
8 | | -# $(math) |
| 17 | +# $(math) Variable |
9 | 18 |
|
10 | | -Outputs the result of a math.js expression. |
| 19 | +## Overview |
11 | 20 |
|
12 | | -#### Parameters |
| 21 | +The `$(math)` variable allows you to perform mathematical calculations and operations directly in your StreamElements Chatbot messages. It utilizes the math.js library, providing a wide range of mathematical functions and capabilities. |
13 | 22 |
|
14 | | -This variable takes a math.js expression as a parameter. You can also use math.js functions like `round()`, `floor()`, `log()`, `random()`, etc. |
| 23 | +## Usage |
15 | 24 |
|
16 | | -#### Example Input |
| 25 | +To use the `$(math)` variable, include it in your message followed by a valid math.js expression: |
17 | 26 |
|
18 | 27 | ``` |
19 | | -${math 2 + 2} |
20 | | -${math round(10/3)} |
21 | | -${math random(1,10)} |
| 28 | +$(math *expression*) |
22 | 29 | ``` |
23 | 30 |
|
24 | | -#### Example Output |
| 31 | +Replace *expression* with the mathematical operation you want to perform. |
25 | 32 |
|
26 | | -``` |
27 | | -4 |
28 | | -3 |
29 | | -7.525986609745358 |
30 | | -``` |
| 33 | +## Examples |
| 34 | + |
| 35 | +Here are some practical examples of how to use the `$(math)` variable: |
| 36 | + |
| 37 | +1. Basic arithmetic: |
| 38 | + ``` |
| 39 | + Bot: 2 + 2 equals $(math "2 + 2") |
| 40 | + Output: 2 + 2 equals 4 |
| 41 | + ``` |
| 42 | + |
| 43 | +2. Rounding a division result: |
| 44 | + ``` |
| 45 | + Bot: 10 divided by 3, rounded to the nearest integer, is $(math "round(10/3)") |
| 46 | + Output: 10 divided by 3, rounded to the nearest integer, is 3 |
| 47 | + ``` |
| 48 | + |
| 49 | +3. Generating a random number: |
| 50 | + ``` |
| 51 | + Bot: Your random number between 1 and 10 is $(math "random(1,10)") |
| 52 | + Output: Your random number between 1 and 10 is 7.525986609745358 |
| 53 | + ``` |
| 54 | + |
| 55 | +## Parameters |
| 56 | + |
| 57 | +The `$(math)` variable takes a single parameter: a valid math.js expression. This can include: |
| 58 | + |
| 59 | +- Basic arithmetic operations (`+`, `-`, `*`, `/`) |
| 60 | +- Mathematical functions (e.g., `round()`, `floor()`, `ceil()`, `log()`, `sin()`, `cos()`) |
| 61 | +- Constants (e.g., `pi`, `e`) |
| 62 | +- Logical operators (e.g., `>`, `<`, `==`, `!=`) |
| 63 | + |
| 64 | +For a complete list of supported operations and functions, refer to the [math.js documentation](https://mathjs.org/docs/index.html). |
| 65 | + |
| 66 | +## Related Variables |
| 67 | + |
| 68 | +- [`$(customapi)`](customapi.md): Can be used to fetch and process numerical data from external sources |
| 69 | + |
| 70 | +## FAQ |
| 71 | + |
| 72 | +**Q: Can I use variables in my math expressions?** |
| 73 | + |
| 74 | +A: Yes, you can use other StreamElements variables within your `$(math)` expression. For example: `$(math "{points} * 2")` would double the user's points. |
| 75 | + |
| 76 | +**Q: Is there a limit to the complexity of expressions I can use?** |
| 77 | + |
| 78 | +A: While there's no strict limit, very complex expressions may take longer to process. It's best to keep expressions relatively simple for optimal performance. |
0 commit comments