- Home /
Error calculating dot product (?!)
I expect I'll feel embarrassed by the answer to this question, but I gotta ask anyway...
Say we've got 2 Vector3s: (0.6, 0.0, 0.8) and (0.3, 0.0, 0.9)
I'm pretty sure their dot product should be: (0.6 x 0.3) + (0.0 x 0.0) + (0.8 x 0.9) = 0.9
Asking Unity this question:
> vects.dirAgentToGoal
"(0.6, 0.0, 0.8)"
magnitude: 0.9999999
normalized: "(0.6, 0.0, 0.8)"
sqrMagnitude: 0.9999999
x: 0.5608401
y: -0.005341334
z: 0.8279068
Static members:
> vects.dirAgentToTarget
"(0.3, 0.0, 0.9)"
magnitude: 1
normalized: "(0.3, 0.0, 0.9)"
sqrMagnitude: 0.9999999
x: 0.3438614
y: 0
z: 0.9390204
Static members:
ok, so far so good.
Unity, please do tell, what is their dot product?
> Vector3.Dot(vects.dirAgentToTarget, vects.dirAgentToGoal)
0.9702727
Ack! But...Unity, 0.9702727 != 0.9
How can this be? Any help is appreciated!
Answer by Eno-Khaon · Oct 10, 2020 at 03:25 AM
All of that checks out, and here's why:
Vector3 values only display a single decimal point when printed by default.
Yep, that's all. Each of the full values (listed individual float values with 7-digit decimal places), rounded to the nearest 0.1, results in what you're seeing there.
So, how do we see the values with more detail?
You can change the string formatting to accommodate your needs in ToString(). For example:
string dATG = vects.dirAgentToGoal.ToString("F7");
Debug.Log("vects.dirAgentToGoal = " + dATG);
This would use the "fixed-point" string formatting, with 7-digits displayed.
Right, in addition to that I would recommend watching this video on floating point numbers. Because you never want to compare floating point numbers that you calculated against some fix constant as the chance that they match is quite random / low.
Answer by DivGradCurl · Oct 10, 2020 at 03:19 PM
oh jeez, and the actual values were staring me straight in the face. Thank you very much @Eno-Khaon