Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created January 16, 2021 00:10
Show Gist options
  • Save howmanysmall/2a1caf4ebd669e72e921970e1f5e7395 to your computer and use it in GitHub Desktop.
Save howmanysmall/2a1caf4ebd669e72e921970e1f5e7395 to your computer and use it in GitHub Desktop.
local Math = {}
function Math:__index(Index)
if Math[Index] then
return Math[Index]
end
end
function Math.new()
return setmetatable({}, Math)
end
function Math:_FindFirstChildWhichIsAWithName(Parent, ClassName, Name)
for Index = 1, #Parent:GetDescendants() do
if Parent:GetDescendants()[Index].Parent == Parent and Parent:GetDescendants()[Index].Name == Name and Parent:GetDescendants()[Index]:IsA(ClassName) then
return Parent:GetDescendants()[Index]
end
end
end
function Math:_IsNear(Value1, Value2, Epsilon)
Epsilon = Epsilon or 0.0001
return Value2 >= Value1 - Epsilon and Value2 <= Value1 + Epsilon
end
function Math:Floor(Number)
local FloorValue = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "FloorValue")
local FinalValue = self:_FindFirstChildWhichIsAWithName(script, "IntValue", "FinalValue")
local OneValue = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "OneValue")
if not FloorValue then
FloorValue = Instance.new("NumberValue", script)
FloorValue.Name = "FloorValue"
end
if not FinalValue then
FinalValue = Instance.new("IntValue", script)
FinalValue.Name = "FinalValue"
end
if not OneValue then
OneValue = Instance.new("NumberValue", script)
OneValue.Name = "OneValue"
end
FloorValue.Value = Number
OneValue.Value = 1
FinalValue.Value = FloorValue.Value - FloorValue.Value % OneValue.Value
local NewValue = FinalValue.Value
FloorValue:Destroy()
FinalValue:Destroy()
OneValue:Destroy()
return NewValue
end
function Math:Sqrt(Number)
local SqrtValue = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "SqrtValue")
local X = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "X")
local Y = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "Y")
local E = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "E")
if not SqrtValue then
SqrtValue = Instance.new("NumberValue", script)
SqrtValue.Name = "SqrtValue"
end
if not X then
X = Instance.new("NumberValue", script)
X.Name = "X"
end
if not Y then
Y = Instance.new("NumberValue", script)
Y.Name = "Y"
end
if not E then
E = Instance.new("NumberValue", script)
E.Name = "E"
end
SqrtValue.Value = Number
X.Value = Number
Y.Value = 1
E.Value = 0.000001
while X.Value - Y.Value > E.Value do
X.Value = (X.Value + Y.Value) / 2
Y.Value = SqrtValue.Value / X.Value
end
E.Value = 0.0000001
if self:_IsNear(X.Value, self:Floor(X.Value), E.Value) then
local NewValue = X.Value
SqrtValue:Destroy()
X:Destroy()
Y:Destroy()
E:Destroy()
return self:Floor(NewValue)
else
local NewValue = X.Value
SqrtValue:Destroy()
X:Destroy()
Y:Destroy()
E:Destroy()
return NewValue
end
end
function Math:Pow(Number, Power)
local PowValue1 = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "PowValue1")
local PowValue2 = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "PowValue2")
local PowValue3 = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "PowValue3")
local IndexValue = self:_FindFirstChildWhichIsAWithName(script, "IntValue", "IndexValue")
if not PowValue1 then
PowValue1 = Instance.new("NumberValue", script)
PowValue1.Name = "PowValue1"
end
if not PowValue2 then
PowValue2 = Instance.new("NumberValue", script)
PowValue2.Name = "PowValue2"
end
if not PowValue3 then
PowValue3 = Instance.new("NumberValue", script)
PowValue3.Name = "PowValue3"
end
if not IndexValue then
IndexValue = Instance.new("IntValue", script)
IndexValue.Name = "IndexValue"
end
PowValue1.Value = Number
PowValue2.Value = Power
PowValue3.Value = 1
IndexValue.Value = 1
while true do
if IndexValue.Value == self:Floor(PowValue2.Value) + 1 then
break
end
IndexValue.Value = IndexValue.Value + 1
PowValue3.Value = PowValue3.Value * PowValue1.Value
end
local NewValue = PowValue3.Value
PowValue1:Destroy()
PowValue2:Destroy()
PowValue3:Destroy()
IndexValue:Destroy()
return NewValue
end
function Math:Abs(Number)
local AbsValue = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "AbsValue")
local SquareValue = self:_FindFirstChildWhichIsAWithName(script, "NumberValue", "SquareValue")
local TwoValue = self:_FindFirstChildWhichIsAWithName(script, "IntValue", "TwoValue")
if not AbsValue then
AbsValue = Instance.new("NumberValue", script)
AbsValue.Name = "AbsValue"
end
if not SquareValue then
SquareValue = Instance.new("NumberValue", script)
SquareValue.Name = "SquareValue"
end
if not TwoValue then
TwoValue = Instance.new("IntValue", script)
TwoValue.Name = "TwoValue"
end
AbsValue.Value = Number
TwoValue.Value = 2
SquareValue.Value = self:Pow(AbsValue.Value, TwoValue.Value)
AbsValue.Value = self:Sqrt(SquareValue.Value)
local NewValue = AbsValue.Value
AbsValue:Destroy()
SquareValue:Destroy()
TwoValue:Destroy()
return NewValue
end
print(Math.new():Abs(-5))
return Math
@TreehouseFalcon
Copy link

you have 2 seconds to explain why you wrote lines 1-194

@howmanysmall
Copy link
Author

you have 2 seconds to explain why you wrote lines 1-194

because i am the best programmer in history and you cannot deny it

@y3th
Copy link

y3th commented Jan 27, 2021

but what if i want to garbage collect my math!!!!!???

@howmanysmall
Copy link
Author

neve

@howmanysmall
Copy link
Author

r

@y3th
Copy link

y3th commented Jan 28, 2021

No!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment