hello
so i was learning metaTables and made a custom vector 2 with them but my custom vector2 have a Magnitude variable when i print it it gives differant value from the original Vector2
the script
local Vec2 = require(script.Vec2)
print(Vec2.new(5,5).Magnitude) -- my custom vector2
print(Vector2.new(5,5).Magnitude)
what it prints
the 2 values are differant even tho its small differance operations on floating point numbers should always return the same value … itsnot random and both of them are using 64bit/double floating point numbers so there shouldnot be a differance | am i doing the magnitude formula differant from how roblox made it? is there another reason why that is happening?
any help is appreciated and thanks
TheModule
local Vec2 = {}
local MT = {}
MT.__index = MT
Vec2.new = function(x, y)
local vector = setmetatable({}, MT)
vector.X = x
vector.Y = y
vector.Magnitude = math.sqrt(x^2 + y^2) -- the Magnitude is here
return vector
end
function MT:Dot(v)
return self.X * v.X + self.Y * v.Y
end
return Vec2
Floating point inaccuracy. The formula is correct, everything you’re doing is correct, it’s just that the compiler is not able to represent the irrational number accurately. Floating-point error mitigation - Wikipedia
yes ik about that what i mean is
print(0.3 - 0.2)
print(0.3 - 0.2)
both of them should print the same value whatever what since they are both the same dataType is that right or i am missing something?
I think Roblox uses a faster algorithm (similar to Quake III inverse square root) for square roots as they’re extremely slow. And that causes very small inaccuracies.