Need help with math functions

As the title says can someone help me understand all the math functions in LuaU

1 Like

is there any in specific that you don’t understand? I think the explanations on the dev hub are pretty good but I am happy to try and help you understand any specific ones if needed.

Yea a couple
1st - math.acos
2nd - math.asin
3rd - math.atan
4th - math.atan2
5th - math.cosh
6th - math.exp
7th - math.log
8th - math.log10
9th - math.sin
10th - math.sinh
11th - math.mod
12th - math.tan
13th- math.tanh

Most of these are used in trigonometry and calculus. I recommend you to understand those first, using some online articles or videos! These things are explained well there! Example cosine, sine, tangent, cos inverse, log10, atan, atan2 etc

for the trigonomic functions like math.acos, math.asin and math.asin, I would recommend this video by Sebastian Lague. It is a generalized video and not in the form of roblox, but nonetheless still very good.
math.mod is the same as the modulus operator (%).

This post explains it well.

From my understanding of logarithms, the most simplest way i can put it is they can be used to solve say 2^x = 4. which becomes x = log base 2 (4)
I haven’t learnt math.exp (I know it has something to do with Euler’s number but don’t know the use case) or the trigonomic h operators as I’m only a term into calculus but I don’t think they will ever be useful unless in a very specific use case.

I wouldn’t fret about learning any of these apart from the basic trig functions as they will not make you a better developer.

I’m going to give a brief rundown of each function and depending on the complexity, explain how to use it. I know this topic/post has already been solved but it’s an opportunity to brush up on my own knowledge whilst hopefully helping someone else.

These functions are largely to do with trigonometry, and for that we need to know the names of the sides of the triangle. It is important to note that the hypotenuse is always the longest side, but that the opposite and adjacent sides depend on the angle being used in the calculation:

image

Image Credit

Next, a few definitions:

  • Tau: Mathematical constant equivalent to 2 * pi
  • Radian: A unit of measurement used for the circumference of a circle, it is equivalent to the radius wrapped around the circumference and is ~ 59.3 degrees. To convert from degrees to radians you multiply by pi and divide by 180. To convert radians into degrees you multiply by 180 and divide by pi. There are tau radians in the whole circumference of a circle, and therefore pi radians in half a circle.
  • Sign: “Direction” of a number i.e. positive or negative. This can be found by doing math.sign(x: number), it will return -1 for negative, 0 for 0 and 1 for positive.
  • Cosine: This is the ratio of hypotenuse to adjacent (hypotenuse divided by adjacent) (see triangle image).
  • Sine: This is the ratio of opposite to hypotenuse (again see triangle image).
  • Tangent: This is the ratio of opposite to adjacent (again see triangle image).
  • E: Euler’s number is a mathematical constant roughly equal to 2.71828 and is used in many things such as the natural logarithm (more detail under math.log())

math.acos(x: number)
This provides the arc cosine of the number x, the inverse of the cosine.

math.asin(x: number)
This provides the arc sine of the number x, the inverse of the sine. I.e. It takes the ratio of opposite to hypotenuse and returns the angle.

math.atan(x: number)
This provides the arc tangent of the number x, the inverse of the tangent in radians. I.e. It takes the ratio of opposite to adjacent and returns the angle

math.atan2(x: number)
This provides the arc tangent of y/x, the inverse of the tangent but uses the signs to determine the quadrant in radians. Because of this, it can handle x being 0.

math.cosh(x: number)
This provides the hyperbolic cosine of x. This is a complex one so I won’t go into detail, but it is the cosine relative to a hyperbola as opposed to a circle. This is equal to:

cosh(x) = (e^x + e^(-x)) / 2

math.exp(x: number)
This gives you e^x, and as such can be used to find e by doing math.exp(1).

math.log(x: number, base: number = e)
This gives you the logarithm of x using the base, the base defaults to e for the natural logarithm. A logarithm is a the inverse of an indices (aka exponent). This means that you can use it to find what power was used:

math.log(8, 2) -- This is 3, because 2^3 is 8

math.log10(x: number)
This provides the base-10 (decimal aka “normal” number base) logarithm of x. Therefore, the following lines are the same:

math.log(x, 10)
math.log10(x)

math.sin(x: number)
This provides the sine of x (see definitions).

math.sinh(x: number)
This provides the hyperbolic sine of x. This is a complex one so I won’t go into detail, but it is the cosine relative to a hyperbola as opposed to a circle. This is equal to:

sinh(x) = (e^x - e^(-x)) / 2

math.modf(x: number)
There is no math.mod() function so I assume this is what you mean. It returns the integral and fractional parts of x. I.e. It splits a number into a whole and a decimal. For example, 2.3 → 2, 0.3. For whole numbers it will return the number and 0 e.g. 2 → 2, 0

math.tan(x: number)
This provides the tangent of x (see definitions).

math.tanh(x: number)
This provides the hyperbolic tangent of x. This is a complex one so I won’t go into detail, but it is the cosine relative to a hyperbola as opposed to a circle. This is equal to:

tanh(x) = sinh(x) / cosh(x)
3 Likes