From 67bff281f9befbea8b4ff028fabe056b8c4e184b Mon Sep 17 00:00:00 2001 From: Winfried Donkers Date: Sat, 4 Mar 2017 09:29:58 +0100 Subject: Add constraint and divide by 0 check for Calc function RRI. Plus rename variables with correct prefixes and common names. Test for type of error in unit test. Change-Id: I48d2fee180e5e093189c1bb6fe889cbf74260fc4 Reviewed-on: https://gerrit.libreoffice.org/34882 Tested-by: Jenkins Reviewed-by: Eike Rathke --- sc/qa/unit/data/functions/financial/fods/rri.fods | 51 +++++++++++++++-------- sc/source/core/tool/interpr2.cxx | 11 +++-- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/sc/qa/unit/data/functions/financial/fods/rri.fods b/sc/qa/unit/data/functions/financial/fods/rri.fods index 09f9760cae21..b687cbd11291 100644 --- a/sc/qa/unit/data/functions/financial/fods/rri.fods +++ b/sc/qa/unit/data/functions/financial/fods/rri.fods @@ -2409,7 +2409,7 @@ - #VALUE! + #NUM! error @@ -2429,7 +2429,7 @@ - #VALUE! + #NUM! error @@ -2450,18 +2450,21 @@ - #NUM! + Err:502 - - error + + Err:502 - + TRUE =RRI(0,10000,11000) - + + constraint N>0 + + @@ -2490,18 +2493,21 @@ - #NUM! + Err:502 - - error + + Err:502 - + TRUE =RRI(96,,11000) - + + check for divide by 0 + + @@ -2509,11 +2515,22 @@ - - - - - + + Err:502 + + + Err:502 + + + TRUE + + + =RRI(4,0,10000) + + + check for divide by 0 + + diff --git a/sc/source/core/tool/interpr2.cxx b/sc/source/core/tool/interpr2.cxx index a5dae57722fe..cf305836b3a2 100644 --- a/sc/source/core/tool/interpr2.cxx +++ b/sc/source/core/tool/interpr2.cxx @@ -1948,10 +1948,13 @@ void ScInterpreter::ScRRI() nFuncFmtType = css::util::NumberFormat::PERCENT; if ( MustHaveParamCount( GetByte(), 3 ) ) { - double nValueInFuture = GetDouble(); - double nValueNow = GetDouble(); - double nSpaceOfTime = GetDouble(); - PushDouble(pow(nValueInFuture / nValueNow, 1.0 / nSpaceOfTime) - 1.0); + double fFutureValue = GetDouble(); + double fPresentValue = GetDouble(); + double NrOfPeriods = GetDouble(); + if ( NrOfPeriods <= 0.0 || fPresentValue == 0.0 ) + PushIllegalArgument(); + else + PushDouble(pow(fFutureValue / fPresentValue, 1.0 / NrOfPeriods) - 1.0); } } -- cgit v1.2.3