The internal representation of a vector is x,y. It will assign the x,y that approximates length 1.
Note that it won’t find the closest possible float representation of that magnitude – only the closest possible one along the current line.
If we don’t care as much about the heading as about the length, it is often easy to find neighboring x and y values that are closer to length 1.0. Here is a very simple example that just checks if a slightly higher x value gives a magnitude that is closer to 1.0:
void setup() {
frameRate(1);
}
void draw() {
PVector vec = new PVector(random(10), random(10));
println();
println(vec, vec.mag());
vec.normalize();
println(vec, vec.mag(), "normed");
float nudged = Math.nextUp(vec.x);
if (abs(1.0 - nudged) < abs(1.0 - vec.x)) {
vec.x = nudged;
println(vec, vec.mag(), "nudged");
} else {
println("not nudged");
}
}
So, we can often nudge a normalized vector to make its .mag() display even closer to 1.0 length – HOWEVER, in doing so, we slightly changed its angle clockwise or counterclockwise. What normalize does instead is prioritizes preserving angle.
Keep in mind that these are very small values – and they almost are all approximations whether or not they appear to be .9999994 or 1.0 etc. It isn’t that one is imprecise and the other isn’t. They are both / all imprecise.
For example, if you look closely at the output, you will see that all combinations of nudging are “closer” or “not closer”:
// nudged, displays as 1.0
[ 8.462109, 5.3119826, 0.0 ] 9.991219
[ 0.8469546, 0.53166515, 0.0 ] 0.99999994 normed
[ 0.84695464, 0.53166515, 0.0 ] 1.0 nudged
// nudged, still displays as .99
[ 0.36812425, 7.538138, 0.0 ] 7.5471215
[ 0.048776776, 0.99880964, 0.0 ] 0.99999994 normed
[ 0.04877678, 0.99880964, 0.0 ] 0.99999994 nudged
// did display as 1.0, still nudged, still displays as
[ 1.9437051, 9.681125, 0.0 ] 9.874318
[ 0.19684449, 0.9804348, 0.0 ] 1.0 normed
[ 0.1968445, 0.9804348, 0.0 ] 1.0 nudged
// nudged past 1.0, still closer than "1.0" !
[ 9.634562, 5.9459205, 0.0 ] 11.321605
[ 0.85098904, 0.52518356, 0.0 ] 1.0 normed
[ 0.8509891, 0.52518356, 0.0 ] 1.0000001 nudged
// displays .99, nudging doesn't make it closer
[ 0.1304692, 8.879499, 0.0 ] 8.880458
[ 0.014691721, 0.99989206, 0.0 ] 0.99999994 normed
not nudged
// etc.