Use std::hypot for Pythagorean Addition – EasyHack

There are many places in the code that calculating the Pythagorean addition is needed.It is defined as:

a ⊕ b = sqrt(a^2+b^2)

This is hypotenuse, or the length of the longest side of a right-angled triangle, that we know the other 2 sides, a and b. For a more detailed description, see this:

Theoretically, this calculation could be done using sqrt(a * a + b * b), but there are problems with this approach. For large a or b, there is a possibility of overflow, although the result itself is not that big to cause overflow.

To overcome this problem, there are implementations of hypotenuse that do not use power of 2, and use other methods to calculate the result. One of them which is usable in C++ is the std::hypot. To calculate a ⊕ b, you can easily use std::hypot(a,b). For the description of this function, see this page:

As an example, you can see this commit from Mike:

@@ -3943,8 +3943,6 @@ void HwpReader::makePicture(Picture * hbox)
    }
}

- namespace { double DBL(int x) { return x * x; } }
-
void HwpReader::makePictureDRAW(HWPDrawingObject *drawobj, Picture * hbox)
{
    int x = hbox->pgx;
@@ -4047,8 +4045,8 @@ void HwpReader::makePictureDRAW(HWPDrawingObject *drawobj, Picture * hbox)
                    bIsRotate = true;
                }
                if( bIsRotate ){
-                   drawobj->extent.w = static_cast<int>(sqrt(DBL(pt[1].x-pt[0].x) + DBL(pt[1].y-pt[0].y)));
-                   drawobj->extent.h = static_cast<int>(sqrt(DBL(pt[2].x-pt[1].x) + DBL(pt[2].y-pt[1].y)));
+                   drawobj->extent.w = static_cast<int>(std::hypot(pt[1].x-pt[0].x, pt[1].y-pt[0].y));
+                   drawobj->extent.h = static_cast<int>(std::hypot(pt[2].x-pt[1].x, pt[2].y-pt[1].y));
                    padd("draw:transform", sXML_CDATA, trans);
                }
            }

How to find instances

Among the result of this output, you can find some instances:

git grep sqrt

Afterall, there are good candidates among the results. You can look at the results to find the suitable place where a Pythagorean addition is done. Then, you can replace the std::hypot instead of the manual addition.

Final Notes

EasyHacks are a good start for newcomers. This specific improvement is filed as tdf#147906.

If you want to contribute to LibreOffice code by working on this improvement, but you need to know how to get started with LibreOffice development, I suggest you to see our video tutorial:

Getting Started (Video Tutorial)