以下是用于 LibreOffice 中 RATE 函数的 C++ 代码的 Swift 移植。原始源代码可以在以下位置找到https://cgit.freedesktop.org/libreoffice/core/tree/sc/source/core/tool/interpr2.cxx https://cgit.freedesktop.org/libreoffice/core/tree/sc/source/core/tool/interpr2.cxx
这些设置为返回nil
如果尝试计算费率时出现任何错误。
func rateIteration(nper: Double, pmt: Double, pval: Double, fval: Double, type: Bool, guess: Double) -> Double? {
// See also #i15090#
// Newton-Raphson method: x(i+1) = x(i) - f(x(i)) / f'(x(i))
// This solution handles integer and non-integer values of Nper different.
// If ODFF will constraint Nper to integer, the distinction of cases can be
// removed; only the integer-part is needed then.
var valid = true
var found = false
var x = 0.0
var xNew = 0.0
var term = 0.0
var termDerivation = 0.0
var geoSeries = 0.0
var geoSeriesDerivation = 0.0
let iterationsMax = 150
var count = 0
let epsilonSmall = 1.0E-14
let SCdEpsilon = 1.0E-7
var pv = pval
var fv = fval
if type {
// payment at beginning of each period
fv = fv - pmt
pv = pv + pmt
}
if nper == nper.rounded() {
// Integer nper
x = guess
while !found && count < iterationsMax {
let powNminues1 = pow(1 + x, nper - 1)
let powN = powNminues1 * (1 + x)
if x == 0.0 {
geoSeries = nper
geoSeriesDerivation = nper * (nper - 1) / 2
} else {
geoSeries = (powN - 1) / x
geoSeriesDerivation = nper * powNminues1 / x - geoSeries / x
}
term = fv + pv * powN + pmt * geoSeries
termDerivation = pv * nper * powNminues1 + pmt * geoSeriesDerivation
if abs(term) < epsilonSmall {
found = true // will catch root which is at an extreme
} else {
if termDerivation == 0.0 {
xNew = x + 1.1 * SCdEpsilon // move away from zero slope
} else {
xNew = x - term / termDerivation
}
count += 1
// more accuracy not possible in oscillating cases
found = abs(xNew - x) < SCdEpsilon
x = xNew
}
}
valid = x > -1.0
} else {
// nper is not an integer value
x = (guess < -1.0) ? -1.0 : guess
while valid && !found && count < iterationsMax {
if x == 0.0 {
geoSeries = nper
geoSeriesDerivation = nper * (nper - 1) / 2
} else {
geoSeries = (pow(1 + x, nper) - 1) / x
geoSeriesDerivation = nper * pow(1 + x, nper - 1) / x - geoSeries / x
}
term = fv + pv * pow(1 + x, nper) + pmt * geoSeries
termDerivation = pv * nper * pow(1 + x, nper - 1) + pmt * geoSeriesDerivation
if abs(term) < epsilonSmall {
found = true // will catch root which is at an extreme
} else {
if termDerivation == 0.0 {
xNew = x + 1.1 * SCdEpsilon
} else {
xNew = x - term / termDerivation
}
count += 1
// more accuracy not possible in oscillating cases
found = abs(xNew - x) < SCdEpsilon
x = xNew
valid = x >= -1.0 // otherwise pow(1 + x, nper) will fail
}
}
}
if valid && found {
return x
} else {
return nil
}
}
func RATE(nper: Double, pmt: Double, pv: Double, fv: Double, type: Double = 0, guess: Double = 0.1) -> Double? {
let payType = type != 0.0
if nper <= 0.0 { // constraint from ODFF spec
return nil
}
if let res = rateIteration(nper: nper, pmt: pmt, pval: pv, fval: fv, type: payType, guess: guess) {
return res
} else {
if guess == 0.1 {
/* TODO: this is rather ugly, instead of looping over different
* guess values and doing a Newton goal seek for each we could
* first insert the values into the RATE equation to obtain a set
* of y values and then do a bisecting goal seek, possibly using
* different algorithms. */
var x = guess
for step in 2...10 {
var g = x * Double(step)
if let res = rateIteration(nper: nper, pmt: pmt, pval: pv, fval: fv, type: payType, guess: g) {
return res
} else {
g = x / Double(step)
if let res = rateIteration(nper: nper, pmt: pmt, pval: pv, fval: fv, type: payType, guess: g) {
return res
}
}
}
}
}
return nil
}
这是您的两个测试用例:
RATE(nper: 252, pmt: -29002.85, pv: 2500000, fv: 0)
RATE(nper: 24, pmt: -46.14, pv: 1000, fv: 0)
结果是:
0.010833331202068584
0.0083244384774994358