elementary_functions: use constants on log10 to increase precision 12/17912/2
Clément DAVID [Fri, 11 Mar 2016 12:17:58 +0000 (13:17 +0100)]
See also https://blogs.gnome.org/mortenw/2016/03/11/floating-point-accuracy-for-scaling-numbers/

Change-Id: I82d4aa73234474a463106d573d1fbb0dbd7e1297

scilab/modules/elementary_functions/sci_gateway/c/sci_log10.c

index ee61b6b..f28e9df 100644 (file)
@@ -41,7 +41,11 @@ int sci_log10(char *fname, void* pvApiCtx)
     double* pIn = NULL;
     double* pOutR = NULL;
     double* pOutI = NULL;
-    double imag = M_PI / log(10.0);
+
+    // Wolfram Alpha : Pi / log(10)
+    const double imag = 1.364376353841841347485783625431355770210127483723925399900;
+    // Wolfram Alpha : 1 / log(10)
+    const double inverseLog10 = 0.434294481903251827651128918916605082294397005803666566114;
 
     int iRhs = nbInputArgument(pvApiCtx);
 
@@ -133,12 +137,14 @@ int sci_log10(char *fname, void* pvApiCtx)
     {
         if (pIn[i] < 0)
         {
-            pOutR[i] = log10(-pIn[i]);
+            // log10 = log * 1/log(10)
+            pOutR[i] = log(-pIn[i]) * inverseLog10;
             pOutI[i] = imag;
         }
         else
         {
-            pOutR[i] = log10(pIn[i]);
+            // log10 = log * 1/log(10)
+            pOutR[i] = log(pIn[i]) * inverseLog10;
         }
     }
 
@@ -146,3 +152,4 @@ int sci_log10(char *fname, void* pvApiCtx)
     ReturnArguments(pvApiCtx);
     return 0;
 }
+