math.pi equals to 180° in degrees, commonly used for circles
Good to know, but why do I get an error when I try to print it? attempt to call a number value
Maybe, it’s because it’s constant?
Yes, it’s a constant meaning no changes could be made
Ah yes, just found that out. Silly me
There are 2 functions called math.sin and math.cos, you can make a circle out of these 2 functions. (known as polar coordinate)
local radius = 50
local increment = 0.1
for i = 0, math.pi*2, increment do
local x = radius * math.cos(i)
local y = radius * math.sin(i)
print(x, y)
end
So, this is the result of math.pi?
Yes, its an floating point, pi decimals is actually infinite, but we’ll crash if we load all the numbers of pi.
So, this is basically an approx. result? (I’m sorry for my unlimited questions)
I don’t know what do you mean by approximate result (i have limited phrases knowledge in english)
Heres a updated script that demonstrates how math.pi can used for
local radius = 50
local increment = 0.1
for i = 0, math.pi*2, increment do -- math.pi * 2 is equals to 360 degrees
local x = radius * math.cos(i)
local y = radius * math.sin(i)
local part = Instance.new('Part', workspace)
part.Position = Vector3.new(x, 5, y)
part.Anchored = true
end
It’s alright. I understand what pi returns now. Thanks!
Let me know if you have any questions about it
Basically, math.floor()
returns the lowest whole number for a decimal, with math.ceil()
doing the opposite; rounding the number up to the highest whole number in a decimal.
Examples:
print(math.floor(1.99)) - will always return 1.
print(math.ceil(1.01)) - will always return 2.
math.abs()
is a function that returns the absolute value of a number (converts negative to positive number)
For instance, math.abs(-1)
returns 1
math.abs(0)
returns 0 because its a positive number
math.abs(1)
returns 1
now, lets move onto math.pow()
, math.pow()
is equilevant to x^y
Example: math.pow(5, 2)
is 25 (5*5)
5^2
is also 25
math.pow(9, 1)
is 9
Just to summarize what has already been said, you can think of math.floor
and math.ceil
like this:
math.floor:
When running a number through this function, think about it as cutting off the numbers after the decimal. For example:
math.floor(5.7) == 5
.7 == 5
See how we “chopped off” the numbers after the decimal?
math.ceil:
When running a number through this function, it’s best to think of it as math.floor
with an extra step. In this case, that extra step is to just add 1. So math.ceil(x) == math.floor(x) + 1
. For example:
math.ceil(5.7) == (5
.7 + 1) == (5 + 1) == 6
For other functions in the math library, you will either have to understand the underlying concept already or be ready to learn it.
Just a fair warning, this reply is sort-of long-winded, so if you don’t want to read all of it, there’s a short explanation at the bottom for those who already understand exponents and the square root of a number.
Just to provide an extension of this, math.abs
can be thought of as a result of multiplying a number by itself. What I’m alluding to here are the rules for multiplying negative and positive numbers. When you multiply a negative number by another negative number you get a positive number. When you multiply a positive number by a positive number you get a positive number. For example:
1 * 2 = 2
and -1 * -2 = 2
But what does this have to do with math.abs
you’re asking? Hold on, I’m getting there.
As @Artzified mentioned, math.power
multiplies a number by itself the specified amount of times. What he didn’t mention is the inverse of this, finding the nth root. This requires a bit more explanation however, so bear with me if you can. Also, if you already understand the square root of a number or the nth term, you can skip this part.
Finding The nth Root:
First, lets discuss a term you might be more familiar with, the square root of a number. But what is this? Glad you asked! The square root of a number is a number that when multiplied by itself once, gives you the number you found the square root of. This can be hard to explain through words, so I’ll provide a more visual example.
For example:
5 * 5 = 25
In this case, 5 is being multiplied by itself once. This also ties into the math.pow
function mentioned, as it is the same as math.pow(5, 2)
This makes 5 the square root of 25, because when multiplied by itself once, it gives us 25.
Another example would be 2.
2 * 2 = 4
In this case, 2 is the square root of 4 because when multiplied by itself, it gives us 4. Roblox has built in support for getting the square root of a number with the math.sqrt
function.
If you understand this and just want to understand how this is related to math.abs
, move on past the following paragraph. If you want to learn about roots in general, carry on.
Roots in General
Alright, first things first, what does it mean to find the nth root? What does nth even mean? n
is just a variable meant as a stand-in for any (positive) number. For example, the square root of a number is the 2nd root because it is when there are two numbers multiplied to get the result. Again, a visual example:
2 * 2 = 4
see how there are two numbers multiplied to get the four?
Now, with this in mind, say we wanted to find the 3rd root, also called the cube root, of a number. In this example, let us find the 3rd root of 27. This means we want to find the number that gives us 27 when multiplied three times. In this case, that number is 3. Visual example:
3 * 3 * 3 = 27
This remains true no matter what value of nth root you want to find.
Great. Now that you understand roots (or at the very least, the square root of a number) and exponents (the math.pow
function), you should be able to understand and appreciate what math.abs
is doing. Essentially, it uses the exponent of the value to get rid of negatives, and the square root of the exponent to get the absolute value. Like I mentioned at the beginning, this is possible because a negative times a negative equals a positive.
As always, here’s a visual example:
math.abs(x) == math.sqrt(math.pow(x, 2))
This is actually the official definition of the absolute value.
What part is? The equation? Because the official definition is the amount a value is displaced from zero on the number line. However, the equation is the way we get this in coding.
The absolute value part.
It looks like this:
|x| = √x^2
math.pi
is 180 degrees in form of radians, which can be converted to degrees using math.deg
and back to radians by math.rad
.