# Does a point have a length?

I am going though a functional source that has the following class:

 class Point3D
{
int x ,y,z;

int vSize2() const
{
return x*x +
y*y +
z*z;
}

int vSize() const
{
return sqrt(vSize2());
}
};


It is hard to understand the functionalities of the above two functions (vSize2() and vSize()). It seems to me that it is calculating the length between the point and the origin.

Any thoughts?

You are correct in guessing that the function vSize() returns the distance from the origin to the point. The other function, vSize2() returns the square of that distance. This is used in calculating the distance, and in some cases it may be useful to work directly with the square of the distance instead of the distance. This avoids calculating square roots, so can lead to improvements in speed and accuracy.
Note that as Nero points out, the functions are using only ints (integer variables and return values). The distance between a point and the origin is not in general an integer value, even if the coordinates of the point are all integers. The distance squared returned by vSize2() will be accurate, but the distance itself returned by vSize() will be accurate only to the nearest integer.
• Additionally, the code for vSize() does not return very precise results as the square root of an integer is not necessarily also an integer, but the method returns an int. Actually, in most cases the given method will return imprecise results. – Nero May 20 '16 at 15:36