How are you making the initial random vectors?
for i=1,1e2 do
local pitch=(math.random()-.5)*math.pi
local yaw=math.pi*2
local co=math.cos(pitch)
local si=math.sin(pitch)
local vec=Vector3.new(math.cos(yaw)*co,si,math.sin(yaw)*co)
print(vec.Magnitude)--and if you don't trust .Magnitude, vec:Dot(vec) is also imperfect
end
My output
1 (x2)
0.99999994039536 (x2)
1 (x3)
0.99999994039536
1 (x2)
0.99999994039536
1 (x11)
0.99999994039536
1 (x7)
0.99999994039536
1 (x3)
0.99999994039536
1 (x6)
0.99999994039536
1
0.99999994039536
1
0.99999994039536
1 (x2)
0.99999994039536
1
0.99999994039536 (x2)
1 (x8)
0.99999994039536
1
0.99999994039536
1 (x5)
0.99999994039536
1 (x9)
0.99999994039536
1 (x19)
0.99999994039536 (x2)
As you can see, just generating the random unit vectors has floating point error on the order of 10^-8
edit: I guess this isn’t necessarily too relevant to the dot product floating point but I’m still curious
edit2: how did you get 10^-13 for cross product?
I get 10^-23
local function GetOrthogonalVector(vec)
if vec.Magnitude == 0 then error("Vector has no magnitude") end
if vec == Vector3.new(0, 1, 0) or vec == Vector3.new(0, -1, 0) then
return Vector3.new(1, 0, 0)
else
return Vector3.new(0, 1, 0):Cross(vec).Unit
end
end
local worst=0
for i=1,1e5 do
local pitch=(math.random()-.5)*math.pi
local yaw=math.pi*2
local co=math.cos(pitch)
local si=math.sin(pitch)
local vec=Vector3.new(math.cos(yaw)*co,si,math.sin(yaw)*co)
worst=math.max(worst,math.abs(GetOrthogonalVector(vec):Dot(vec)))
end
print(worst)
edit 3:
local function GetOrthogonalVector(vec)
local x=vec.x
local y=vec.y
local z=vec.z
if (y==1 or y==-1)and x==0 and z==0 then
return Vector3.new(1,0,0)
end
return Vector3.new(z,0,-x)
end
local worst=0
for i=1,1e5 do
local pitch=(math.random()-.5)*math.pi
local yaw=math.pi*2
local co=math.cos(pitch)
local si=math.sin(pitch)
local vec=Vector3.new(math.cos(yaw)*co,si,math.sin(yaw)*co)
worst=math.max(worst,math.abs(GetOrthogonalVector(vec):Dot(vec)))
end
print(worst)
@AstroCode this has a 0 precision error but the orthogonal vector is not necessarily unit
When its unit it has 10^-23 precision (I just expanded out the cross product)
Also I never knew Vector3s overloaded the equal operator, thats pretty cool print(Vector3.new(0,1,0)==Vector3.new(0,1,0))--true
edit:: oh no it turns out I was doing local yaw=math.pi*2
instead of local yaw=math.pi*2*math.random()
doing random yaw makes me get 10^-8
error for cross product (still 10^-8
for dot)
So still very curious about how you make random unit vecs:P