z_mirror/flib
git clone https://git.raiguard.me/z_mirror/flib
d80f6ce
trunk
1--- Additional mathematical utilities. 2--- ```lua 3--- local flib_math = require("__flib__.math") 4--- ``` 5--- @class flib_math 6local flib_math ={} 7 8--- Multiply by degrees to convert to radians. 9--- ```lua 10--- local rad = 1 x flib_math.deg_to_rad -- 0.0174533 11--- ``` 12flib_math . deg_to_rad =math . pi / 180 --- @type number 13 14--- Multiply by radians to convert to degrees. 15--- 16--- ```lua 17--- local deg = 1 x flib_math.rad_to_deg -- 57.2958 18--- ``` 19flib_math . rad_to_deg =180 / math . pi --- @type number 20 21--- One radian. 22flib_math . radian =2 * math . pi 23 24flib_math . max_double =0X1.FFFFFFFFFFFFFP+1023 25flib_math . min_double =- 0X1.FFFFFFFFFFFFFP+1023 26flib_math . max_int8 =127 --- 127 27flib_math . min_int8 =- 128 --- -128 28flib_math . max_uint8 =255 --- 255 29flib_math . max_int16 =32767 --- 32,767 30flib_math . min_int16 =- 32768 --- -32,768 31flib_math . max_uint16 =65535 --- 65,535 32flib_math . max_int =2147483647 --- 2,147,483,647 33flib_math . min_int =- 2147483648 --- -2,147,483,648 34flib_math . max_uint =4294967295 --- 4,294,967,295 35flib_math . max_int53 =0x1FFFFFFFFFFFFF --- 9,007,199,254,740,991 36flib_math . min_int53 =- 0x20000000000000 --- -9,007,199,254,740,992 37 38--- Round a number to the nearest multiple of divisor. 39--- Defaults to nearest integer if divisor is not provided. 40--- 41--- From [lua-users.org](http://lua-users.org/wiki/SimpleRound). 42--- @param num number 43--- @param divisor? number `num` will be rounded to the nearest multiple of `divisor` (default: 1). 44--- @return number 45function flib_math . round ( num , divisor ) 46divisor =divisor or 1 47if num >= 0 then 48return math . floor (( num / divisor ) + 0.5 ) * divisor 49else 50return math . ceil (( num / divisor ) - 0.5 ) * divisor 51end 52end 53 54--- Ceil a number to the nearest multiple of divisor. 55--- @param num number 56--- @param divisor? number `num` will be ceiled to the nearest multiple of `divisor` (default: 1). 57function flib_math . ceiled ( num , divisor ) 58if divisor then 59return math . ceil ( num / divisor ) * divisor 60end 61return math . ceil ( num ) 62end 63 64--- Floor a number to the nearest multiple of divisor. 65--- @param num number 66--- @param divisor? number `num` will be floored to the nearest multiple of `divisor` (default: 1). 67function flib_math . floored ( num , divisor ) 68if divisor then 69return math . floor ( num / divisor ) * divisor 70end 71return math . floor ( num ) 72end 73 74--- Returns the argument with the maximum value from a set. 75--- @param set number[] 76--- @return number 77function flib_math . maximum ( set ) 78return math . max ( table . unpack ( set )) 79end 80 81--- Returns the argument with the minimum value from a set. 82--- @param set number[] 83--- @return number 84function flib_math . minimum ( set ) 85return math . min ( table . unpack ( set )) 86end 87 88--- Calculate the sum of a set of numbers. 89--- @param set number[] 90--- @return number 91function flib_math . sum ( set ) 92local sum =set [ 1 ] or 0 93for i =2 , # set do 94sum =sum + set [ i ] 95end 96return sum 97end 98 99--- Calculate the mean (average) of a set of numbers. 100--- @param set number[] 101--- @return number 102function flib_math . mean ( set ) 103return flib_math . sum ( set ) / # set 104end 105 106--- Calculate the mean of the largest and the smallest values in a set of numbers. 107--- @param set number[] 108--- @return number 109function flib_math . midrange ( set ) 110return 0.5 * ( flib_math . minimum ( set ) + flib_math . maximum ( set )) 111end 112 113--- Calculate the range in a set of numbers. 114--- @param set number[] 115--- @return number 116function flib_math . range ( set ) 117return flib_math . maximum ( set ) - flib_math . minimum ( set ) 118end 119 120--- Clamp a number between minimum and maximum values. 121--- @param x number 122--- @param min? number default 0 123--- @param max? number default 1 124--- @return number 125function flib_math . clamp ( x , min , max ) 126x =x == 0 and 0 or x -- Treat -0 as 0 127min , max =min or 0 , max or 1 128return x < min and min or ( x > max and max or x ) 129end 130 131--- Return the signedness of a number as a multiplier. 132--- @param x number 133--- @return number 134function flib_math . sign ( x ) 135return ( x >= 0 and 1 ) or - 1 136end 137 138--- Linearly interpolate between `num1` and `num2` by `amount`. 139--- 140--- The parameter `amount` is clamped between `0` and `1`. 141--- 142--- When `amount = 0`, returns `num1`. 143--- 144--- When `amount = 1`, returns `num2`. 145--- 146--- When `amount = 0.5`, returns the midpoint of `num1` and `num2`. 147--- @param num1 number 148--- @param num2 number 149--- @param amount number 150--- @return number 151function flib_math . lerp ( num1 , num2 , amount ) 152return num1 + ( num2 - num1 ) * flib_math . clamp ( amount , 0 , 1 ) 153end 154 155return flib_math