fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
57e394e
v2.1.1a
1function math . gcd ( m , n ) 2-- greatest common divisor 3while m ~= 0 do 4m , n =n % m , m 5end 6return n 7end 8 9function math . clamp ( num , min , max ) 10assert ( min <= max , "invalid clamp range" ) 11if num < min then 12num =min 13elseif num > max then 14num =max 15end 16return num 17end 18 19do 20local pow =math . pow 21local floor =math . floor 22 23function math . round ( num ) 24return num + 0.5 - ( num + 0.5 ) % 1 25end 26 27function math . roundplaces ( num , palces ) 28local mult =pow ( 10 , ( palces or 0 )) 29return floor ( num * mult + 0.5 ) / mult 30end 31end 32 33do 34local random =math . random 35 36function math . randomFloat ( min , max ) 37return min + random () * ( max - min ) 38end 39 40require ( "extensions.table" ) 41 42local sum =table . sum 43 44function math . weightedRandom ( tbl ) 45local threshold =random ( 0 , sum ( tbl )) 46local last_choice 47for choice , weight in pairs ( tbl ) do 48threshold =threshold - weight 49if threshold <= 0 then return choice end 50last_choice =choice 51end 52return last_choice 53end 54end 55 56do 57local sin =math . sin 58 59function math . sinlerp ( min , max , t ) 60local h =( max - min ) / 2 61return min + h + sin ( t ) * h 62end 63 64local cos =math . cos 65 66function math . coslerp ( min , max , t ) 67local h =( max - min ) / 2 68return min + h + cos ( t ) * h 69end 70end 71 72function math . lerp ( a , b , t ) 73return ( 1 - t ) * a + t * b 74end 75 76function math . timeunits ( seconds ) 77return { 78years =seconds / 3.154e7 , 79months =seconds / 2.628e6 , 80weeks =seconds / 604800 , 81days =seconds / 86400 , 82hours =seconds / 3600 , 83minutes =seconds / 60 , 84seconds =seconds , 85milliseconds =seconds * 1000 , 86} 87end 88 89do 90local format =string . format 91 92function math . toMinutesSeconds ( num , fmt ) 93local m =num / 60 94local s =num % 60 95return format ( fmt or "%02d:%02d" , m , s ) 96end 97 98function math . toMinSecMS ( num , fmt ) 99local m =num / 60 100local s =num % 60 101local ms =( num % 1 ) * 100 102return format ( fmt or "%02d:%02d:%02d" , m , s , ms % 100 ) 103end 104end