soupwat
(soupwat)
March 6, 2022, 8:53pm
#1
Hiya!
I was recently playing around with vectors and I decided to make my own custom magnitude function as shown below:
local function magnitude(vector)
return math.sqrt(vector.x^2 + vector.y^2 + vector.z^2)
end
It works just fine, however, when comparing it to the normal .Magnitude method there seems to be a very small difference.
My question is simple: why? Is it an inaccuracy with math.sqrt?
I am merely curious, as I have no use for my own magnitude function.
IDoLua
(CantBeBothered)
March 6, 2022, 8:55pm
#2
Accord to the output, your function is actually more accurate. Note the extra decimal precision.
There’s such negligible difference that it’s practically ==. I wouldn’t sweat over it too much.
soupwat
(soupwat)
March 6, 2022, 9:02pm
#3
Oh lol, awesome
It doesn’t really matter to me anyway, I was just curious as to why there was a small difference.
Cheers
IDoLua
(CantBeBothered)
March 6, 2022, 9:03pm
#4
Here’s the results of a unit test:
Sample size : 2^20
Forummer
(Forummer)
March 6, 2022, 9:21pm
#5
Double floating point precision errors, in other words there aren’t enough available bits to accurately represent your number.
Double-precision floating-point format (sometimes called FP64 or float64) is a computer number format, usually occupying 64 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point.
Floating point is used to represent fractional values, or when a wider range is needed than is provided by fixed point (of the same bit width), even if at the cost of precision. Double precision may be chosen when the range or precision of single precision would ...