Help with magnitudes / metaTables

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
image
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

1 Like

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?

image
Yours is actually more accurate.

1 Like

The error is very insignificant (10^ -9), I personally wouldn’t worry about it too much.

1 Like

yeah you are right thanks for that

yes ik amnot worry about it i just need an explaination of why that happend

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.

1 Like

that may be the reason i will look into that algorithm thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.