Basically for things like this (not real code):
for(int i = 0; i < 1000000000; ++i)
{
Vert v0 = mesh.polygons[i].edges[0].vert0;
Vert v1 = mesh.polygons[i].edges[0].vert1;
Vector3 center = (v0.pos + v1.pos) / 2;
}
v0.pos is of type Vector3<float, float, float>, but could be Vector3<double, double, double>.
Is it safe to just do?:
Vector3 center = (v0.pos + v1.pos) * 0.5;
This might seem like premature optimization but this has to be as fast as possible that will be called on billions of points (point clouds).
Not a C++ expert by any means, but in C#, doing this:
generates IL that in the former:
and the latter
It strikes me that the former will likely have higher precision because the multiplication is performed on 2 doubles, resulting in double precision, whereas in the latter the division is performed on a single which is upcast to a double (but still only will have the precision of the single).
This bears me out:
produces:
So, ‘safe’, maybe, if
losing precisiongaining extra precision is ok.