Math Functions
Math is easy, but you need to think
Last updated
Was this helpful?
Math is easy, but you need to think
Last updated
Was this helpful?
We use math almost everywhere, including coding. Unlike the normal way 1+1, you need to use function add 1 1
to get the result: 2; 2-1 will be sub 2 1
in YAGPDB coding system. There are also multiplication, division, log, mod, and other math functions.
Math functions will be used when you want the bot to calculate things such as your xp, your level, and your rank (example here). Sometimes we will use math functions with range
action to index specific objects or combine with the if-else
function (example here).
Math is hard for you? Don't worry because you will not need to calculate. All you have to do is write the codes, and then the bot will help you solve many math questions (example here).
To demonstrate math functions with range action and if-else
function.
$note
is a slice, and using range
to index the specific object is very common. We make it send a different message to the channel when the $index
is equal to 8; otherwise, the bot will send different words from a to j (except the "i") in order. The outputs will be like the following picture:
To demonstrate the way to use math functions to solve math questions. Also, you can use the YAGPDB built-in command (-calc
) to do the same thing.
Function
Description
add x y z ...
sub x y z ...
mult x y z ...
div x y z ...
fdiv x y z ...
Meant specifically for floating point numbers division.
log x base
mod x y
Mod returns the floating-point remainder of x/y. mod 17 3
returns 2
of type float64.
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
.
randInt (stop, or start stop)
Returns a random integer between 0 and stop, or start - stop if two args are provided.
round
roundCeil
roundFloor
roundEven
Returns the nearest integer, rounding ties to even.
{{roundEven 10.5}}
returns 10 {{roundEven 11.5}}
returns 12
.
sqrt
{{$d := randInt 10}}
Stores random int into variable $d
(a random number from 0-9). And
{{$d := randInt 3 10}}
Stores random int into variable $d
(a random number from 3-9).
To demonstrate round
, roundCeil
, and roundFloor
.
{{round 12.49}}
returns 12; {{round 12.50}}
returns 13;
{{roundCeil 12.000000000001}}
returns 13;
{{roundFloor 12.99999999999}}
returns 12.
To demonstrate log
and sqrt
.
{{sqrt 3}}
returns 1.7320508075688772;
{{sqrt 3 | printf "%.1f"}}
returns 1.7;
{{sqrt 3 | printf "%.6f"}}
returns 1.732051 (this means that it will round the number).
{{log "123" 2 | printf "%.4f"}}
returns 6.9425;
{{log "123" 2 | printf "%.10f"}}
returns 6.9425145053.
To demonstrate complex round
action.
round
can only returns the nearest integer, so how can we round other digits? Let's see.
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
. Example .
Returns x - y -z - ... Works like add, just subtracts. Example .
Multiplication, like add
or div
, detects first number's type. {{mult 3.14 2}}
returns 6.28
. Example .
Division, like add
or mult
, detects number's type first. {{div 11 3}}
returns 3
whereas {{div 11.1 3}}
returns 3.6999999999999997
. Example .
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, also is the default to change the base.{{log "123" 2}}
will return 6.94251450533924
. Example .
Result will be start <= random number < stop
. Example .
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 .
Returns the least integer value greater than or equal to input or rounds up. {{roundCeil 1.1}}
returns 2
. Example .
Returns the greatest integer value less than or equal to input or rounds down. {{roundFloor 1.9}}
returns 1
. Example .
Returns the square root of a number as type float64.
{{sqrt 49}}
returns 7
, {{sqrt 12.34 | printf "%.4f"}}
returns 3.5128
. Example .