hello guys. I spent literally week to find, why my besiers aren’t correctly calculated. And today, FINALLY!!! I found 1 of the issues. And it’s floating point in my for loop. In short, I add 0.01 to i each time, and finish value is 1. But due to floating point, i slowly turns into 0.9900000007, and +0.01 results in 1.00000000007, and this leads to loop end. How I can fix this inside for loop?
Can you provide us with a script?
Problematic piece of code:
local BezierCurve = {}
Time = Time or 1
warn(Time)
if Time <= 0 then error("INVALID TIME") end
local Inc = Time/(Params or 100)
for i = 0, Time, Inc do
local Lerps = table.clone(Dots)
if Time == math.huge then error("Inf asquared") end
if i == Time then warn("TIME MATCHED") end
warn(i)
while #Lerps > 1 do
for a = 1, #Lerps-1, 1 do
Lerps[a] = Vector3.new(Lerps[a].X * (1 - i) + Lerps[a+1].X * i, 0, Lerps[a].Z * (1 - i) + Lerps[a+1].Z * i)
end
Lerps[#Lerps] = nil
end
table.insert(BezierCurve, Lerps[1])
end
local BezierLength = 0
for i = 1, #BezierCurve - 1, 1 do
print(tostring(i) .. ": " .. tostring((BezierCurve[i + 1] - BezierCurve[i]).Magnitude))
BezierLength += (BezierCurve[i + 1] - BezierCurve[i]).Magnitude
end
return BezierLength
As you can see, there’s 2 for loops, and first one is problematic one.