Skip to content

Commit

Permalink
Merge pull request #436 from Orden4/add-math-mathf
Browse files Browse the repository at this point in the history
Expand Math, add MathF
  • Loading branch information
yanghuan authored Oct 7, 2023
2 parents 2edfc2e + ac3628f commit ca2cb31
Show file tree
Hide file tree
Showing 2 changed files with 365 additions and 6 deletions.
116 changes: 110 additions & 6 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,93 @@ local trunc = System.trunc

local math = math
local floor = math.floor
local ceil = math.ceil
local min = math.min
local max = math.max
local abs = math.abs
local log = math.log
local sqrt = math.sqrt
local ln2 = log(2)

local function acosh(a)
return log(a + sqrt(a ^ 2 - 1))
end

local function asinh(a)
return log(a + sqrt(a ^ 2 + 1))
end

local function atanh(a)
return 0.5 * log((1 + a) / (1 - a))
end

local function cbrt(a)
if a >= 0 then
return a ^ (1 / 3)
else
return -abs(a) ^ (1 / 3)
end
end

local function copySign(a, b)
if b >= 0 then
return a >= 0 and a or -a
else
return a >= 0 and -a or a
end
end

local function fusedMultiplyAdd(a, b, c)
return a * b + c
end

local function ilogB(a)
return a == 0 and -2147483648 or floor(log(abs(a)) / ln2)
end

local function log2(a)
return log(a) / ln2
end

local function maxMagnitude(a, b)
local x = abs(a)
local y = abs(b)
if x > y then
return a
elseif x < y then
return b
else
return a > b and a or b
end
end

local function minMagnitude(a, b)
local x = abs(a)
local y = abs(b)
if x < y then
return a
elseif x > y then
return b
else
return a < b and a or b
end
end

local function reciprocalEstimate(a)
return 1 / a
end

local function reciprocalSqrtEstimate(a)
return sqrt(1 / a)
end

local function scaleB(a, b)
return a * 2 ^ b
end

local function sinCos(a)
return System.ValueTuple(math.sin(a), math.cos(a))
end

local function bigMul(a, b)
return a * b
Expand All @@ -38,11 +121,17 @@ local function round(value, digits, mode)
local i = value * mult
if mode == 1 then
value = trunc(i + (value >= 0 and 0.5 or -0.5))
elseif mode == 2 then
value = i >= 0 and floor(i) or ceil(i)
elseif mode == 3 then
value = floor(i)
elseif mode == 4 then
value = ceil(i)
else
value = trunc(i)
if value ~= i then
local dif = i - value
if value >= 0 then
if i >= 0 then
if dif > 0.5 or (dif == 0.5 and value % 2 ~= 0) then
value = value + 1
end
Expand Down Expand Up @@ -115,30 +204,45 @@ local tanh = math.tanh or function (x) return sinh(x) / cosh(x) end
local Math = math
Math.Abs = abs
Math.Acos = math.acos
Math.Acosh = acosh
Math.Asin = math.asin
Math.Asinh = asinh
Math.Atan = math.atan
Math.Atanh = atanh
Math.Atan2 = math.atan2 or math.atan
Math.BigMul = bigMul
Math.Ceiling = math.ceil
Math.Cbrt = cbrt
Math.Ceiling = ceil
Math.Clamp = clamp
Math.CopySign = copySign
Math.Cos = math.cos
Math.Cosh = cosh
Math.DivRem = divRem
Math.Exp = exp
Math.Floor = math.floor
Math.Floor = floor
Math.FusedMultiplyAdd = fusedMultiplyAdd
Math.IEEERemainder = IEEERemainder
Math.ILogB = ilogB
Math.Log = log
Math.Log10 = log10
Math.Max = math.max
Math.Min = math.min
Math.Log2 = log2
Math.Max = max
Math.MaxMagnitude = maxMagnitude
Math.Min = min
Math.MinMagnitude = minMagnitude
Math.Pow = pow
Math.ReciprocalEstimate = reciprocalEstimate
Math.ReciprocalSqrtEstimate = reciprocalSqrtEstimate
Math.Round = round
Math.ScaleB = scaleB
Math.Sign = sign
Math.Sin = math.sin
Math.SinCos = sinCos
Math.Sinh = sinh
Math.Sqrt = math.sqrt
Math.Sqrt = sqrt
Math.Tan = math.tan
Math.Tanh = tanh
Math.Truncate = truncate

System.define("System.Math", Math)
System.define("System.MathF", Math)
Loading

0 comments on commit ca2cb31

Please sign in to comment.