📚
YAGPDB
  • YAGPDB Coding System - Documentation
  • Getting Started
  • Useful Functions
  • Primary Level
    • The Dot and Variables
      • User
      • Guild / Server
      • Member
      • Channel
      • Message
      • Reaction
      • Time
    • Custom Types
    • Functions
      • Type Conversion
      • String Manipulation
      • Math Functions
      • Message Functions
      • Mentions
      • Role Functions
      • Current User
      • Miscellaneous
      • ExecCC
    • Conditional Branching
  • Intermediate Level
  • Database
  • Range Action
  • With Action
  • Custom Embeds
  • Advanced Level
    • Using RegEx
  • Custom Commands Examples
    • Texas Hold'em
Powered by GitBook
On this page
  • Preface
  • Function

Was this helpful?

  1. Primary Level
  2. Functions

Math Functions

Math is easy, but you need to think

PreviousString ManipulationNextMessage Functions

Last updated 5 years ago

Was this helpful?

Preface

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 ). Sometimes we will use math functions with range action to index specific objects or combine with the if-else function (example ).

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 ).

Example:

  • To demonstrate math functions with range action and if-else function.

{{$note := cslice "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"}}
{{$index := 0}}
{{range (seq 1 11)}}
{{if eq $index 8}}
{{sendMessage nil "will not output **i** when the $index is equal to 8"}}
{{else}}
{{sendMessage nil (index $note $index)}}
{{end}}
{{$index = (add $index 1)}}
{{end}}

$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.

{{$x := (index .CmdArgs 0)}}
{{$y := (index .CmdArgs 2)}}
{{if reFind "add" (index .CmdArgs 1)}}
{{(add $x $y)}}
{{else if reFind "sub" (index .CmdArgs 1)}}
{{(sub $x $y)}}
{{else if reFind "mult" (index .CmdArgs 1)}}
{{(mult $x $y)}}
{{else if reFind "div" (index .CmdArgs 1)}}
{{(div $x $y)}}
{{end}}

Function

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

Example:

  • {{$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.

To round to the first digit after decimal point:
{{$x := 12.356}} {{div (round (mult $x 10)) 10}}
------------------------------------------------
To round to the third digit before decimal point:
{{$x := 12375.964}} {{mult (round (div $x 100)) 100}}
-----------------------------------------------------
To test your skills. Try to round the third digit before and after decimal point, like 12376.48978 >>> 12400.49:
{{$x := 12376.48978}} {{add (mult (round (div $x 100)) 100) (div (round (mult (sub $x (roundFloor $x)) 1000)) 1000)}}

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 .

here
here
here
here
here
here
here
here
here
here
here
here
here