Surviving Mars

Surviving Mars

Not enough ratings
math. Functions
   
Award
Favorite
Favorited
Unfavorite
File Size
Posted
Updated
10.126 KB
16 Jul, 2018 @ 7:17pm
22 Sep, 2022 @ 4:57pm
16 Change Notes ( view )

Subscribe to download
math. Functions

In 1 collection by ChoGGi
ChoGGi's Mods: Modding
17 items
Description
Modders only (unless it's a mod that uses these functions).

For some reason SM doesn't have any of lua "math." functions.
Functions are either from SM (math.min = Min()), or pure lua.

SM and certain SM functions have issues with floats: 5 == 5.5 returns true for one (though 5.0 == 5.5 returns false).
Which is why I'm not just using math.cos = cos... (you can access them as math.sm_cos)
I'm going for accuracy not speed: Bench with GetPreciseTicks()

Implemented:
abs, ceil, deg, exp, floor, fmod, huge, log, max, maxinteger, min, mininteger, modf, pi, rad, random, randomseed, sqrt, tointeger, type, ult

Not implemented yet:
cos, sin, tan, acos, asin, atan


I'm only implementing the ones listed in the manual, if you have some to add: feel free to send them to me.

Functions should return values according to:
https://www.lua.org/manual/5.3/manual.html#6.7
You can test results with a copy of lua.exe: https://github.com/HazeProductions/Lua-x64.
If the result it gives is different then my func: Please let me know.
8 Comments
Unichi 21 Aug, 2018 @ 12:23pm 
I looked here:
https://www.gammon.com.au/scripts/doc.php?lua=math.random

It states "That is, zero up to but excluding 1", which makes sense, it's like that in most random() functions :)
ChoGGi  [author] 21 Aug, 2018 @ 12:19pm 
Oh, I was just going by https://www.lua.org/manual/5.3/manual.html#pdf-math.random

When called without arguments, returns a pseudo-random float with uniform distribution in the range [0,1).[/i]
I suppose that wouldn't be a 1 ever... whoops
Unichi 21 Aug, 2018 @ 12:02pm 
Hey, based on the documentation of math.random for Lua it never includes 1 by design. I found this because it works the same way in JavaScript (and in ANSI-C, afaik, for that matter)

It's basically to make

SomeTable[math.floor(math.random() * #SomeTable)]

work as expected without subtracting 1 from the count (it will never yield an invalid index, as you never reach #SomeTable, only just _before_ #SomeTable which will floor to #SomeTable - 1

I wouldn't include this as a "Known Issue", this is actually correct (and right/good) behavior :)
ChoGGi  [author] 29 Jul, 2018 @ 10:23pm 
v0.4
Changed:
.log(number,NaN) would assume NaN was Napier's constant, now it errors out.
.maxinteger/.mininteger now use max_int/min_int instead of the values from lua console.

Fixed:
.ceil() was off for negative numbers with a few decimal place digits.
ChoGGi  [author] 18 Jul, 2018 @ 2:07pm 
v0.3
Fixed: math.fmod, math.modf, math.log, math.random(n).
More unit testing.
Removed lua.exe, use https://github.com/HazeProductions/Lua-x64 .
Dash 18 Jul, 2018 @ 2:11am 
Thank you for this library!
ChoGGi  [author] 17 Jul, 2018 @ 6:14pm 
Aww, thanks

I didn't really expect this to be popular, but I'm sure some people will enjoy it :)
SkiRich 17 Jul, 2018 @ 5:51pm 
Subscriber #1. Because I know.