chart2/source/view/charttypes/Splines.cxx         |   25 ++++++----------------
 filter/source/graphicfilter/icgm/actimpr.cxx      |    2 -
 filter/source/graphicfilter/icgm/bitmap.cxx       |    4 +--
 filter/source/graphicfilter/icgm/class4.cxx       |    2 -
 svx/source/customshapes/EnhancedCustomShape2d.cxx |   10 ++++----
 svx/source/engine3d/viewpt3d2.cxx                 |    2 -
 svx/source/svdraw/svdpdf.cxx                      |   11 ++-------
 svx/source/xoutdev/_xpoly.cxx                     |    2 -
 8 files changed, 21 insertions(+), 37 deletions(-)

New commits:
commit ab9896bfda4d2ef16f3cbb373edced33f9021492
Author:     offtkp <parisop...@gmail.com>
AuthorDate: Thu Mar 24 11:32:24 2022 +0200
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Mon Mar 28 11:43:28 2022 +0200

    tdf#147906 change all sqrt(a * a + b * b) occurences to std::hypot(a, b)
    
    Other changes:
    In Splines.cxx, no longer divides dx, dy with fDiffMax because we
    switched to std::hypot, and hypot avoids intermediate overflows.
    
    Change-Id: I8c459a0e56deb86606fc9b1bf3e68b132a60705d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132073
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Tested-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/chart2/source/view/charttypes/Splines.cxx 
b/chart2/source/view/charttypes/Splines.cxx
index c1acc4a4600c..2633f7798741 100644
--- a/chart2/source/view/charttypes/Splines.cxx
+++ b/chart2/source/view/charttypes/Splines.cxx
@@ -411,26 +411,19 @@ bool createParameterT(const tPointVecType& rUniquePoints, 
double* t)
     bool bIsSuccessful = true;
     const lcl_tSizeType n = rUniquePoints.size() - 1;
     t[0]=0.0;
-    double dx = 0.0;
-    double dy = 0.0;
-    double fDiffMax = 1.0; //dummy values
     double fDenominator = 0.0; // initialized for summing up
     for (lcl_tSizeType i=1; i<=n ; ++i)
     {   // 4th root(dx^2+dy^2)
-        dx = rUniquePoints[i].first - rUniquePoints[i-1].first;
-        dy = rUniquePoints[i].second - rUniquePoints[i-1].second;
-        // scaling to avoid underflow or overflow
-        fDiffMax = std::max(fabs(dx), fabs(dy));
-        if (fDiffMax == 0.0)
+        double dx = rUniquePoints[i].first - rUniquePoints[i-1].first;
+        double dy = rUniquePoints[i].second - rUniquePoints[i-1].second;
+        if (dx == 0 && dy == 0)
         {
             bIsSuccessful = false;
             break;
         }
         else
         {
-            dx /= fDiffMax;
-            dy /= fDiffMax;
-            fDenominator += sqrt(sqrt(dx * dx + dy * dy)) * sqrt(fDiffMax);
+            fDenominator += sqrt(std::hypot(dx, dy));
         }
     }
     if (fDenominator == 0.0)
@@ -444,13 +437,9 @@ bool createParameterT(const tPointVecType& rUniquePoints, 
double* t)
             double fNumerator = 0.0;
             for (lcl_tSizeType i=1; i<=j ; ++i)
             {
-                dx = rUniquePoints[i].first - rUniquePoints[i-1].first;
-                dy = rUniquePoints[i].second - rUniquePoints[i-1].second;
-                fDiffMax = std::max(fabs(dx), fabs(dy));
-                // same as above, so should not be zero
-                dx /= fDiffMax;
-                dy /= fDiffMax;
-                fNumerator += sqrt(sqrt(dx * dx + dy * dy)) * sqrt(fDiffMax);
+                double dx = rUniquePoints[i].first - rUniquePoints[i-1].first;
+                double dy = rUniquePoints[i].second - 
rUniquePoints[i-1].second;
+                fNumerator += sqrt(std::hypot(dx, dy));
             }
             t[j] = fNumerator / fDenominator;
 
diff --git a/filter/source/graphicfilter/icgm/actimpr.cxx 
b/filter/source/graphicfilter/icgm/actimpr.cxx
index bb17a380c3b2..c77b55184c5a 100644
--- a/filter/source/graphicfilter/icgm/actimpr.cxx
+++ b/filter/source/graphicfilter/icgm/actimpr.cxx
@@ -791,7 +791,7 @@ void CGMImpressOutAct::DrawText(awt::Point const & 
rTextPos, awt::Size const & r
     maXShape->setSize( awt::Size( nWidth, nHeight ) );
     double nX = mpCGM->pElement->nCharacterOrientation[ 2 ];
     double nY = mpCGM->pElement->nCharacterOrientation[ 3 ];
-    double fSqrt = sqrt(nX * nX + nY * nY);
+    double fSqrt = std::hypot(nX, nY);
     double nOrientation = fSqrt != 0.0 ? basegfx::rad2deg(acos(nX / fSqrt)) : 
0.0;
     if ( nY < 0 )
         nOrientation = 360 - nOrientation;
diff --git a/filter/source/graphicfilter/icgm/bitmap.cxx 
b/filter/source/graphicfilter/icgm/bitmap.cxx
index cb319cb038e0..2dc6ff370757 100644
--- a/filter/source/graphicfilter/icgm/bitmap.cxx
+++ b/filter/source/graphicfilter/icgm/bitmap.cxx
@@ -226,7 +226,7 @@ void CGMBitmap::ImplGetBitmap( CGMBitmapDescriptor& rDesc )
         nX = rDesc.mnR.X - rDesc.mnP.X;
         nY = rDesc.mnR.Y - rDesc.mnP.Y;
 
-        double fSqrt = sqrt(nX * nX + nY * nY);
+        double fSqrt = std::hypot(nX, nY);
         rDesc.mnOrientation = fSqrt != 0.0 ? basegfx::rad2deg(acos(nX / 
fSqrt)) : 0.0;
         if ( nY > 0 )
             rDesc.mnOrientation = 360 - rDesc.mnOrientation;
@@ -240,7 +240,7 @@ void CGMBitmap::ImplGetBitmap( CGMBitmapDescriptor& rDesc )
         nX = fCos * nX + fSin * nY;
         nY = -( fSin * nX - fCos * nY );
 
-        fSqrt = sqrt(nX * nX + nY * nY);
+        fSqrt = std::hypot(nX, nY);
         fAngle = fSqrt != 0.0 ? basegfx::rad2deg(acos(nX / fSqrt)) : 0.0;
         if ( nY > 0 )
             fAngle = 360 - fAngle;
diff --git a/filter/source/graphicfilter/icgm/class4.cxx 
b/filter/source/graphicfilter/icgm/class4.cxx
index edf34970a27f..c915576c2bdb 100644
--- a/filter/source/graphicfilter/icgm/class4.cxx
+++ b/filter/source/graphicfilter/icgm/class4.cxx
@@ -36,7 +36,7 @@ double CGM::ImplGetOrientation( FloatPoint const & rCenter, 
FloatPoint const & r
     double nX = rPoint.X - rCenter.X;
     double nY = rPoint.Y - rCenter.Y;
 
-    double fSqrt = sqrt(nX * nX + nY * nY);
+    double fSqrt = std::hypot(nX, nY);
     double fOrientation = fSqrt != 0.0 ? basegfx::rad2deg(acos(nX / fSqrt)) : 
0.0;
     if (nY > 0)
         fOrientation = 360 - fOrientation;
diff --git a/svx/source/customshapes/EnhancedCustomShape2d.cxx 
b/svx/source/customshapes/EnhancedCustomShape2d.cxx
index 9f3b91297bd9..32c7a73cc4dd 100644
--- a/svx/source/customshapes/EnhancedCustomShape2d.cxx
+++ b/svx/source/customshapes/EnhancedCustomShape2d.cxx
@@ -1439,7 +1439,7 @@ static double lcl_getRadiusDistance(double fWR, double 
fHR, double fX, double fY
     else if (fY == 0.0)
         return std::min(fWR - fX, fHR);
 
-    double fD = std::min(fWR, fHR) - sqrt(fX * fX + fY * fY); // iteration 
start value
+    double fD = std::min(fWR, fHR) - std::hypot(fX, fY); // iteration start 
value
     sal_uInt8 nIter(0);
     bool bFound(false);
     do
@@ -1648,8 +1648,8 @@ bool EnhancedCustomShape2d::SetHandleControllerPosition( 
const sal_uInt32 nIndex
                             double fHelpAngle = atan2(fHelpY, fHelpX);
                             double fOuterX = fWidth / 2.0 * cos(fHelpAngle);
                             double fOuterY = fHeight / 2.0 * sin(fHelpAngle);
-                            double fOuterRadius = sqrt(fOuterX * fOuterX + 
fOuterY * fOuterY);
-                            double fHandleRadius = sqrt(fDX * fDX + fDY * fDY);
+                            double fOuterRadius = std::hypot(fOuterX, fOuterY);
+                            double fHandleRadius = std::hypot(fDX, fDY);
                             fRadius = (fOuterRadius - fHandleRadius) / 2.0;
                             double fss(std::min(fWidth, fHeight));
                             if (fss != 0.0)
@@ -1676,7 +1676,7 @@ bool EnhancedCustomShape2d::SetHandleControllerPosition( 
const sal_uInt32 nIndex
                         // no special meaning of radius or angle, suitable for 
"ooxml-arc",
                         // "ooxml-chord", "ooxml-pie" and circular arrows 
value adj4.
                         fAngle = lcl_getAngleInOOXMLUnit(fDY, fDX);
-                        fRadius = sqrt(fDX * fDX + fDY * fDY);
+                        fRadius = std::hypot(fDX, fDY);
                         double fss(std::min(fWidth, fHeight));
                         if (fss != 0.0)
                             fRadius = fRadius * 100000.0 / fss;
@@ -1702,7 +1702,7 @@ bool EnhancedCustomShape2d::SetHandleControllerPosition( 
const sal_uInt32 nIndex
                     // ToDo: Angle unit is degree, but range ]-180;180] or 
[0;360[? Assume ]-180;180].
                     if (fDX != 0.0 || fDY != 0.0)
                     {
-                        fRadius = sqrt(fDX * fDX + fDY * fDY);
+                        fRadius = std::hypot(fDX, fDY);
                         fAngle = basegfx::rad2deg(atan2(fDY, fDX));
                     }
                 }
diff --git a/svx/source/engine3d/viewpt3d2.cxx 
b/svx/source/engine3d/viewpt3d2.cxx
index 660af6adfbe6..c2e55a085d0e 100644
--- a/svx/source/engine3d/viewpt3d2.cxx
+++ b/svx/source/engine3d/viewpt3d2.cxx
@@ -91,7 +91,7 @@ const basegfx::B3DPoint& Viewport3D::GetViewPoint()
         // (preliminary) view coordinate system.
         fXupVp = aViewTf.get(0, 0) * aVUV.getX() + aViewTf.get(0, 1) * 
aVUV.getY() + aViewTf.get(0, 2) * aVUV.getZ();
         fYupVp = aViewTf.get(1, 0) * aVUV.getX() + aViewTf.get(1, 1) * 
aVUV.getY() + aViewTf.get(1, 2) * aVUV.getZ();
-        fV = sqrt(fXupVp * fXupVp + fYupVp * fYupVp);
+        fV = std::hypot(fXupVp, fYupVp);
 
         if ( fV != 0 )
         {
diff --git a/svx/source/svdraw/svdpdf.cxx b/svx/source/svdraw/svdpdf.cxx
index 3ad208ba78b9..648485baafd3 100644
--- a/svx/source/svdraw/svdpdf.cxx
+++ b/svx/source/svdraw/svdpdf.cxx
@@ -72,11 +72,6 @@
 #include <sal/log.hxx>
 #include <osl/diagnose.h>
 
-namespace
-{
-double sqrt2(double a, double b) { return sqrt(a * a + b * b); }
-}
-
 using namespace com::sun::star;
 
 ImpSdrPdfImport::ImpSdrPdfImport(SdrModel& rModel, SdrLayerID nLay, const 
tools::Rectangle& rRect,
@@ -724,8 +719,8 @@ void 
ImpSdrPdfImport::ImportText(std::unique_ptr<vcl::pdf::PDFiumPageObject> con
     OUString sText = pPageObject->getText(pTextPage);
 
     const double dFontSize = pPageObject->getFontSize();
-    double dFontSizeH = fabs(sqrt2(aMatrix.a(), aMatrix.c()) * dFontSize);
-    double dFontSizeV = fabs(sqrt2(aMatrix.b(), aMatrix.d()) * dFontSize);
+    double dFontSizeH = fabs(std::hypot(aMatrix.a(), aMatrix.c()) * dFontSize);
+    double dFontSizeV = fabs(std::hypot(aMatrix.b(), aMatrix.d()) * dFontSize);
 
     dFontSizeH = convertPointToMm100(dFontSizeH);
     dFontSizeV = convertPointToMm100(dFontSizeV);
@@ -1002,7 +997,7 @@ void 
ImpSdrPdfImport::ImportPath(std::unique_ptr<vcl::pdf::PDFiumPageObject> con
     aPolyPoly.transform(aTransform);
 
     float fWidth = pPageObject->getStrokeWidth();
-    const double dWidth = 0.5 * fabs(sqrt2(aPathMatrix.a(), aPathMatrix.c()) * 
fWidth);
+    const double dWidth = 0.5 * fabs(std::hypot(aPathMatrix.a(), 
aPathMatrix.c()) * fWidth);
     mnLineWidth = convertPointToMm100(dWidth);
 
     vcl::pdf::PDFFillMode nFillMode = vcl::pdf::PDFFillMode::Alternate;
diff --git a/svx/source/xoutdev/_xpoly.cxx b/svx/source/xoutdev/_xpoly.cxx
index 844a21f07bcc..4d556b1f9feb 100644
--- a/svx/source/xoutdev/_xpoly.cxx
+++ b/svx/source/xoutdev/_xpoly.cxx
@@ -486,7 +486,7 @@ double XPolygon::CalcDistance(sal_uInt16 nP1, sal_uInt16 
nP2)
     const Point& rP2 = pImpXPolygon->pPointAry[nP2];
     double fDx = rP2.X() - rP1.X();
     double fDy = rP2.Y() - rP1.Y();
-    return sqrt(fDx * fDx + fDy * fDy);
+    return std::hypot(fDx, fDy);
 }
 
 void XPolygon::SubdivideBezier(sal_uInt16 nPos, bool bCalcFirst, double fT)

Reply via email to