This is an automated email from the ASF dual-hosted git repository.

jimjag pushed a commit to branch openformula
in repository https://gitbox.apache.org/repos/asf/openoffice.git

commit 437c29e0263d14a9a86bc95a314464c020c82d41
Author: Jim Jagielski <[email protected]>
AuthorDate: Wed Nov 18 11:47:30 2026 -0500

    sc: F.DIST for very large X, RRI with Nper <= 0
    
    F.DIST(1E308;5;10) gave #VALUE! and F.DIST(1E17;5;1;0) gave Err:502.
    RRI(0;100;100) returned 0 and RRI(-2;100;200) a finite rate. Both are errors
    now, as they should have been.
---
 main/sc/source/core/tool/interpr2.cxx |  5 +++++
 main/sc/source/core/tool/interpr3.cxx | 20 ++++++++++++++++++--
 test/odf-validate/calcval-cases14.tsv |  5 +++++
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/main/sc/source/core/tool/interpr2.cxx 
b/main/sc/source/core/tool/interpr2.cxx
index f0ec9d7035..e7400a868d 100644
--- a/main/sc/source/core/tool/interpr2.cxx
+++ b/main/sc/source/core/tool/interpr2.cxx
@@ -1301,6 +1301,11 @@ void ScInterpreter::ScZGZ()
         double nZukunftswert = GetDouble();
         double nGegenwartswert = GetDouble();
         double nZeitraum = GetDouble();
+        if (nZeitraum <= 0.0)
+        {
+            PushIllegalArgument();
+            return;
+        }
         PushDouble(pow(nZukunftswert / nGegenwartswert, 1.0 / nZeitraum) - 
1.0);
     }
 }
diff --git a/main/sc/source/core/tool/interpr3.cxx 
b/main/sc/source/core/tool/interpr3.cxx
index 25c4b509da..465f7a0b0f 100644
--- a/main/sc/source/core/tool/interpr3.cxx
+++ b/main/sc/source/core/tool/interpr3.cxx
@@ -1601,6 +1601,13 @@ void ScInterpreter::ScFDist()
     PushDouble(GetFDist(fF, fF1, fF2));
 }
 
+// r1*x/(r1*x+r2) without forming inf/inf when r1*x overflows.
+static double lcl_FDistLeftBetaArg( double fF1, double fF2, double fX )
+{
+    double fR = fF1 * fX;
+    return ::rtl::math::isFinite( fR ) ? fR / (fR + fF2) : 1.0;
+}
+
 void ScInterpreter::ScFDistLeft()
 {
     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", 
"ScInterpreter::ScFDistLeft" );
@@ -1623,9 +1630,18 @@ void ScInterpreter::ScFDistLeft()
     }
     // F(x;r1;r2) is Beta(r1/2;r2/2) at y = r1*x/(r1*x+r2).
     double fDenom = fF1 * fF + fF2;
-    double fY = fF1 * fF / fDenom;
+    double fY = lcl_FDistLeftBetaArg( fF1, fF2, fF );
     if (bCumulative)
         PushDouble( GetBetaDist( fY, fF1 / 2.0, fF2 / 2.0));
+    else if (fY >= 1.0 || fDenom > 1.0E150)
+    {
+        // y has rounded to 1 or denom^2 would overflow: use the density in 
log form
+        double fA = fF1 / 2.0;
+        double fB = fF2 / 2.0;
+        double fLogDenom = log( fF1) + log( fF) + std::log1p( fF2 / (fF1 * 
fF));
+        PushDouble( exp( (fA - 1.0) * log( fF) + fA * log( fF1) + fB * log( 
fF2)
+                         - GetLogBeta( fA, fB) - (fA + fB) * fLogDenom));
+    }
     else
         PushDouble( GetBetaDistPDF( fY, fF1 / 2.0, fF2 / 2.0) * fF1 * fF2 / 
(fDenom * fDenom));
 }
@@ -2034,7 +2050,7 @@ public:
             ScFDistLeftFunction( ScInterpreter& rI, double fpVal, double 
fF1Val, double fF2Val ) :
                 rInt(rI), fp(fpVal), fF1(fF1Val), fF2(fF2Val) {}
 
-    double  GetValue( double x ) const  { return fp - rInt.GetBetaDist( fF1 * 
x / (fF1 * x + fF2), fF1 / 2.0, fF2 / 2.0); }
+    double  GetValue( double x ) const  { return fp - rInt.GetBetaDist( 
lcl_FDistLeftBetaArg( fF1, fF2, x), fF1 / 2.0, fF2 / 2.0); }
 };
 
 class ScFDistFunction : public ScDistFunc
diff --git a/test/odf-validate/calcval-cases14.tsv 
b/test/odf-validate/calcval-cases14.tsv
index f30b899c1f..4038eafd62 100644
--- a/test/odf-validate/calcval-cases14.tsv
+++ b/test/odf-validate/calcval-cases14.tsv
@@ -340,6 +340,9 @@ F.DIST.pole =F.DIST(0;1;5;0)        cell    #ERR    6.18.22 
density error for r1 = 1 at x = 0
 F.DIST.zero.r1.2       =F.DIST(0;2;5;0)        cell    1       density at x = 
0 for r1 = 2
 F.DIST.bad.df  =F.DIST(2.5;0;10)       cell    #ERR    6.18.22 r1 must be 
positive
 F.DIST.plus.legacy     =F.DIST(2.5;5;10)+FDIST(2.5;5;10)       cell    1       
left and right tails add up to 1
+F.DIST.huge.x.cdf      =F.DIST(1E308;5;10)     cell    1       r1*x overflows; 
must not give NaN
+F.DIST.huge.x.pdf      =F.DIST(1E308;5;10;0)   cell    0       density 
underflows to 0
+F.DIST.y.rounds.to.1   =F.DIST(1E17;5;1;0)     cell    1.20042175487614E-26    
r2 = 1: y rounds to 1, density must not be an error
 F.INV.mid      =F.INV(0.5;5;10)        cell    0.9319331608510479      6.18.24 
FINV left tail
 F.INV.zero     =F.INV(0;5;10)  cell    0       6.18.24 p = 0
 F.INV.one      =F.INV(1;5;10)  cell    #ERR    6.18.24 p = 1 has no finite x
@@ -367,6 +370,8 @@ ERRORTYPE.legacy    =ERRORTYPE(1/0) cell    532     
ORG.OPENOFFICE.ERRORTYPE keeps the int
 ERRORTYPE.legacy.na    =ERRORTYPE(NA())        cell    32767   
ORG.OPENOFFICE.ERRORTYPE keeps the internal code
 RRI.basic      =RRI(10;100;200)        cell    0.07177346253629313     6.12.44 
(Fv/Pv)^(1/N) - 1
 RRI.zgz        =ZGZ(10;100;200)        cell    0.07177346253629313     old 
name ZGZ still read
+RRI.nper.zero  =RRI(0;100;100) cell    #ERR    Nper must be greater than 0
+RRI.nper.negative      =RRI(-2;100;200)        cell    #ERR    Nper must be 
greater than 0
 ODDFPRICE.short        
=ODDFPRICE(DATE(2008;11;11);DATE(2021;3;1);DATE(2008;10;15);DATE(2009;3;1);0.0785;0.0625;100;2;1)
       cell    113.59771747407883      6.12.31 short odd first period (Excel 
example)
 ODDFPRICE.long 
=ODDFPRICE(DATE(2022;8;2);DATE(2028;4;5);DATE(2021;11;16);DATE(2023;10;5);0.0764;0.1492;100;2;2)
        cell    71.46287909084108       6.12.31 long odd first period
 ODDFPRICE.long.monthend        
=ODDFPRICE(DATE(2011;9;19);DATE(2031;11;14);DATE(2010;4;28);DATE(2012;11;14);0.0569;0.0035;95.5;1;2)
    cell    199.43997530092304      6.12.31 long odd first period, annual

Reply via email to