basegfx/source/vector/b2dvector.cxx  |   27 +++++++++++----------------
 basegfx/source/vector/b2ivector.cxx  |    4 ++--
 basegfx/source/vector/b3dvector.cxx  |   10 +++++++---
 include/basegfx/vector/b3dvector.hxx |   19 +++++--------------
 vcl/source/filter/idxf/dxfvec.cxx    |    2 +-
 5 files changed, 26 insertions(+), 36 deletions(-)

New commits:
commit 45d951492a34d8d4134a518662377fa4c1e08395
Author:     RMZeroFour <ritobrot...@gmail.com>
AuthorDate: Fri Mar 29 20:19:58 2024 +0530
Commit:     Hossein <hoss...@libreoffice.org>
CommitDate: Fri Apr 5 13:59:33 2024 +0200

    tdf#147906 Use std::hypot for Pythagorean distance
    
    As part of the efforts in tdf#147906 to replace expressions like
    sqrt(a^2 + b^2) with std::hypot(a, b), to eliminate overflow
    errors, this commit performs the changes in certain files.
    
    This also changes the B3DVector::normalize and B2DVector::normalize
    methods to have similar behaviour.
    
    Change-Id: I142585cfa594849f06cd06517ad9d40430df2ade
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165555
    Tested-by: Hossein <hoss...@libreoffice.org>
    Reviewed-by: Hossein <hoss...@libreoffice.org>

diff --git a/basegfx/source/vector/b2dvector.cxx 
b/basegfx/source/vector/b2dvector.cxx
index 85ec6ca81116..1f696237fecf 100644
--- a/basegfx/source/vector/b2dvector.cxx
+++ b/basegfx/source/vector/b2dvector.cxx
@@ -25,28 +25,23 @@ namespace basegfx
 {
     B2DVector& B2DVector::normalize()
     {
-        double fLen(scalar(*this));
+        double fLen(std::hypot(mnX, mnY));
 
-        if(fTools::equalZero(fLen))
-        {
-            mnX = 0.0;
-            mnY = 0.0;
-        }
-        else
+        if(!fTools::equalZero(fLen))
         {
             const double fOne(1.0);
 
             if(!fTools::equal(fOne, fLen))
             {
-                fLen = sqrt(fLen);
-
-                if(!fTools::equalZero(fLen))
-                {
-                    mnX /= fLen;
-                    mnY /= fLen;
-                }
+                mnX /= fLen;
+                mnY /= fLen;
             }
         }
+        else
+        {
+            mnX = 0.0;
+            mnY = 0.0;
+        }
 
         return *this;
     }
@@ -90,7 +85,7 @@ namespace basegfx
 
     B2DVector& B2DVector::setLength(double fLen)
     {
-        double fLenNow(scalar(*this));
+        double fLenNow(std::hypot(mnX, mnY));
 
         if(!fTools::equalZero(fLenNow))
         {
@@ -98,7 +93,7 @@ namespace basegfx
 
             if(!fTools::equal(fOne, fLenNow))
             {
-                fLen /= sqrt(fLenNow);
+                fLen /= fLenNow;
             }
 
             mnX *= fLen;
diff --git a/basegfx/source/vector/b2ivector.cxx 
b/basegfx/source/vector/b2ivector.cxx
index 5b45871a39cf..6d618facf48f 100644
--- a/basegfx/source/vector/b2ivector.cxx
+++ b/basegfx/source/vector/b2ivector.cxx
@@ -42,7 +42,7 @@ namespace basegfx
 
     B2IVector& B2IVector::setLength(double fLen)
     {
-        double fLenNow(scalar(*this));
+        double fLenNow(std::hypot(mnX, mnY));
 
         if(!::basegfx::fTools::equalZero(fLenNow))
         {
@@ -50,7 +50,7 @@ namespace basegfx
 
             if(!::basegfx::fTools::equal(fOne, fLenNow))
             {
-                fLen /= sqrt(fLenNow);
+                fLen /= fLenNow;
             }
 
             mnX = fround( mnX*fLen );
diff --git a/basegfx/source/vector/b3dvector.cxx 
b/basegfx/source/vector/b3dvector.cxx
index 89bc33c2bd9e..68e3fcf205c4 100644
--- a/basegfx/source/vector/b3dvector.cxx
+++ b/basegfx/source/vector/b3dvector.cxx
@@ -24,7 +24,7 @@ namespace basegfx
 {
     B3DVector& B3DVector::normalize()
     {
-        double fLen(scalar(*this));
+        double fLen(std::hypot(mnX, mnY, mnZ));
 
         if(!::basegfx::fTools::equalZero(fLen))
         {
@@ -32,13 +32,17 @@ namespace basegfx
 
             if(!::basegfx::fTools::equal(fOne, fLen))
             {
-                fLen = sqrt(fLen);
-
                 mnX /= fLen;
                 mnY /= fLen;
                 mnZ /= fLen;
             }
         }
+        else
+        {
+            mnX = 0.0;
+            mnY = 0.0;
+            mnZ = 0.0;
+        }
 
         return *this;
     }
diff --git a/include/basegfx/vector/b3dvector.hxx 
b/include/basegfx/vector/b3dvector.hxx
index 44c926e805c8..df7638f73268 100644
--- a/include/basegfx/vector/b3dvector.hxx
+++ b/include/basegfx/vector/b3dvector.hxx
@@ -106,10 +106,7 @@ namespace basegfx
         */
         double getLength() const
         {
-            double fLen(scalar(*this));
-            if((0.0 == fLen) || (1.0 == fLen))
-                return fLen;
-            return sqrt(fLen);
+            return std::hypot(mnX, mnY, mnZ);
         }
 
         /** Calculate the length in the XZ-Plane for this 3D Vector
@@ -118,10 +115,7 @@ namespace basegfx
         */
         double getXZLength() const
         {
-            double fLen((mnX * mnX) + (mnZ * mnZ)); // #i73040#
-            if((0.0 == fLen) || (1.0 == fLen))
-                return fLen;
-            return sqrt(fLen);
+            return std::hypot(mnX, mnZ);
         }
 
         /** Calculate the length in the YZ-Plane for this 3D Vector
@@ -130,10 +124,7 @@ namespace basegfx
         */
         double getYZLength() const
         {
-            double fLen((mnY * mnY) + (mnZ * mnZ));
-            if((0.0 == fLen) || (1.0 == fLen))
-                return fLen;
-            return sqrt(fLen);
+            return std::hypot(mnY, mnZ);
         }
 
         /** Set the length of this 3D Vector
@@ -143,7 +134,7 @@ namespace basegfx
         */
         B3DVector& setLength(double fLen)
         {
-            double fLenNow(scalar(*this));
+            double fLenNow(std::hypot(mnX, mnY, mnZ));
 
             if(!::basegfx::fTools::equalZero(fLenNow))
             {
@@ -151,7 +142,7 @@ namespace basegfx
 
                 if(!::basegfx::fTools::equal(fOne, fLenNow))
                 {
-                    fLen /= sqrt(fLenNow);
+                    fLen /= fLenNow;
                 }
 
                 mnX *= fLen;
diff --git a/vcl/source/filter/idxf/dxfvec.cxx 
b/vcl/source/filter/idxf/dxfvec.cxx
index a11358b510cc..1cef52126eb6 100644
--- a/vcl/source/filter/idxf/dxfvec.cxx
+++ b/vcl/source/filter/idxf/dxfvec.cxx
@@ -28,7 +28,7 @@
 
 double DXFVector::Abs() const
 {
-    return sqrt(SProd(*this));
+    return std::hypot(fx, fy, fz);
 }
 
 

Reply via email to