Help with math library

I’m interested into what all math functions do, ive seen different systems use them and different types of scripts and I’m really interested to what all their uses are,
I know math.random etc it’s the math.abs , and math.log,10

I develop quite numbery games and i was wondering if these math operations would make developing the games easier, and I’m interested just for knowledge :slight_smile:

There is no such thing as “Lua math” or “RLua math”. Math is just math. It doesn’t necessarily change.

math.abs(x) just returns the absolute value of x.

Let’s use math.abs(-6) as an example, and let me draw an ugly-as-hell number line.

image

x is always 6 numbers away from 0, no matter the sign.

math.log(x[, base = e]) simply returns the logarithm of x, and base defaults to Euler’s number (e) if not provided. I don’t know too much about logarithms so that is as helpful as I can get.

math.log10(x) returns the power you need to take 10 to, to get x. In other words

math.log10(10^x) == x
4 Likes

Thank you! Is there any examples out there of this in use?

Well you can force a number to be positive (exception is 0, it has no sign) with math.abs, and i’m not a big math nerd so I don’t know of any use cases for the last two, sorry :frowning:

No worries, neither aha. So its for making sure that the number output is always positive?

1 Like

Math.log(n) can be rephrased as “How much would I need to raise e (approximately 2.718) to get n”. Math.log also has a second argument where you can specify a base for the logarithm. If I were to do math.log(8,2) then the logarithm’s base would be 2, and from that it will return 3 as 2 raised to the power 3 will get us back to 8. If I have not explained something correctly let me know.

Tl;dr math.log(n,base) will return what number you need to raise the base to, to get n.

2 Likes

Basic algebra, trigonometry, and linear algebra are the most useful math skills for game development IMO. I’m sure there’s tons of good courses on Khan Academy on these subjects.

7 Likes

you can use math.log to essentially compress very large numbers while saving to DataStores.

  local BigNumber = 1000000000000000000000000000000
  local base = 10^5  

  local compressed = math.log(BigNumber, base) 
  local uncompressed = (base^compressed) 

  print(compressed,uncompressed, BigNumber)

  6,  1e+30, 1e+30

you could save these decimal numbers (turned to strings internally while saving) which are much smaller than the original ones.

123213123123123123123123123123123123123123123
= 8.8181313931833

2 Likes

Honestly, if you are thinking to add in 3D programming aspects into your game, I would brush up on Vector and CFrame maths and how it works. Roblox Developer Hub and the DevForum can help you out with this.

Here are some awesome resources:

And personally, I would dive into physics a little because personally my knowledge of physics really helped me conceptualize my ideas during game development and helped me understand what is going on with my code/ in general. As @Crazyman32 had stated before there are a lot of information on math and even physics in Khan Academy, so I second his idea.

1 Like

Thank you , ill take a look on the website

@AbiZinho , thank you I’ll take a look into vector3 and cframe in studio to practise roblox math,

Hard agree. Don’t bother going out of your way to learn what math.atan2 or math.log does, just go learn some math, then figure out which are the appropriate math functions to help you to achieve what you want to achieve. It’ll make a lot of sense!

Okay thank you, do you know if there is anywhere I can see examples of these in use?