我的任务是将用F#编写的一些特定财务函数迁移到C# 我正在使用的F#库非常好(http://archive.msdn.microsoft.com/FinancialFunctions)但是环境考虑因素阻止了CLR程序集部署到我们的SQL Server,并且需要部署它的无限制权限
我遇到问题的具体功能是MacAuley Duration公式。我是F#的绝对新手,如果F#专家会解释下面的代码片段,我将不胜感激:
let aggrFunction acc index =
let x5 = float index - 1. + x1
let x6 = pow x3 x5
( x6 <> 0.) |> elseThrow "x6 must be different from 0)"
let x7 = (100. * coupon / frequency) / x6
let a, b = acc
a + x7 * x5, b + x7
let term2, term4 = aggrBetween 1 (int n) aggrFunction (0., 0.)
我最好的C#尝试看起来并返回错误的结果(我并排运行两个)。 btw频率是一个枚举因此演员
double term2 = 0;
double term4 = 0;
for (int i = 0; i <= n ; i++)
{
var x5 = i - 1 + x1;
var x6 = Math.Pow(x3, x5);
if (x6 == 0)
{
throw new Exception("x6 must be different from 0)");
}
var x7 = (100D * coupon/(int) frequency) / x6;
term2 += (x7*x5);
term4 += x7;
}