What is math.abs exactly doing?

What is math.abs exactly doing?

It does return the absolute value then 1.33333 might be 1 and 1.99999 might be 2. Or am I wrong.

but when does it round the absolute value?

4 Likes

It’s usually to convert a negative number to a positive one

print(math.abs(1)) --Prints 1
print(math.abs(-1)) -- Prints 1

The thign you’re mentioning is math.round

17 Likes

math.abs() is a built-in lua method (function), that enables you to find an absolute value.

What is an absolute value? It is a aboslute, non-negative value of any real number.

x = -5   ;   |x| = 5
y = 5    ;   |y| = 5

Read more about absolute values here: Absolute value - Wikipedia.

EDIT @Iwantbebetter

Rounding is a different operation.
It is basically shortening of a number to approximate value that is closer to integer values, e.g. 1.6 is close to 2; 1.4 is close to 1.

Positive values:

math.round(1.4) --> 1
math.round(1.5) --> 2

Negative values:

math.round(-2.4) --> -2
math.round(-2.5) --> -3

( -2 > -3)

Read more about various built-in mathematical operations in lua by following this link:

5 Likes

No, you’re thinking of rounding there.

Absolute value is how far a number is from 0. For example, -2 and 2 are both 2 units away from 0, so they both return 2.

5 Likes

Use math.round() to achieve that

math.abs(x) returns back the positive number of x, math.round(x) however returns back an integer of x (Or whole number) when rounding it to the nearest whole number

local Number = -2.48
Number = math.abs(Number)
print(Number)
--2.48

Number = math.round(Number)
print(Number)
--2
1 Like

lua doesn’t support variable pointers last time i checked so your usage of math.abs and math.round is incorrect; this is how you’d actually do it:

local Number = -2.48
Number = math.abs(Number)
print(Number)
-- 2.48

Number = math.round(Number)
print(Number)
-- 2
1 Like

From what people are saying, if you wanna know how it would look like in code, it would be something like this:

function abs(n)
    if n < 0 then
         return n * -1
    end
    return n
end
2 Likes
 print(math.abs(-1))-- 1
 print(math.abs(1))--  1

math.abs always gives you a positive
heres the tutorial from where i learned it

1 Like

i don’t think math.round is a function

Really? I thought you were able to use math functions without equaling them as values, guess I’ll need to double-check then

@ZINTICK

All of these are functions used by math, math.round isn’t in the documentation but it is usable isn’t auto-filled in Studio but its function is still usable

2 Likes

It is, it was added back in August.

1 Like

oh, I did not know probably bc there weren’t documented

It is in the documentation.

2 Likes

Ok what last time I checked it wasn’t in there maybe I’m just blind

Oh, maybe I was thinking of when using it in Studio, it wasn’t there to auto-fill it (Fixed it hopefully)

1 Like