简单的财务费率函数在JavaScript中 [英] simple financial rate function in javascript

查看:139
本文介绍了简单的财务费率函数在JavaScript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的JavaScript金融RATE函数,并且我找到了这个函数。但这似乎太难理解了。我想简化这个功能,我需要你的帮助。
如果任何人有最简单的功能,请回答。 (这是一个和RATE函数等价的函数)。

$ $ $ $ $ $ $ $ $ $ $ $ var函数var(函数nper,pmt,pv,fv,type,guess) {
if(guess == null)guess = 0.01;
if(fv == null)fv = 0;
if(type == null)type = 0;

var FINANCIAL_MAX_ITERATIONS = 128; //以128
变化的投注准确性VAR FINANCIAL_PRECISION = 0.0000001; // 1.0e-8

var y,y0,y1,x0 ,x1 = 0,f = 0,i = 0;
var rate = guess; (数学证券(利率)<金融利息){
y = pv *(1 + nper *利率)+ pmt *(1+利率*类型)* nper + fv;
} else {
f = Math.exp(nper * Math.log(1 + rate));
y = pv * f + pmt *(1 / rate + type)*(f - 1)+ fv;
}
y0 = pv + pmt * nper + fv;
y1 = pv * f + pmt *(1 / rate + type)*(f - 1)+ fv;

//通过Newton割线方法找到根
i = x0 = 0.0;
x1 =费率; ((Math.abs(y0-y1)> FINANCIAL_PRECISION)&&(i< FINANCIAL_MAX_ITERATIONS)){
rate =(y1 * x0-y0 * x1)/(y1- Y0);
x0 = x1;
x1 =费率;如果(数学证据(费率)<金融利息率){
y = pv *(1 + nper *费率)+ pmt *(1+费率*类型)* nper + fv,

;
} else {
f = Math.exp(nper * Math.log(1 + rate));
y = pv * f + pmt *(1 / rate + type)*(f - 1)+ fv;
}

y0 = y1;
y1 = y;
++ i;
}
return rate;}

谢谢!

解决方案

数学对我来说太复杂了,但是这对你来说可能更容易阅读。一些变量已被重命名为更有意义,并且它的格式设置为让您的眼睛更轻松

 函数速率(paymentsPerYear,如果interest,futureValue,dueEndorBeginning没有设置,现在设置
if(interest == null)
interest = valueAtount,presentValue,futureValue,dueEndOrBeginning,interest)

0.01;

if(futureValue == null)
futureValue = 0;

if(dueEndOrBeginning == null)
dueEndOrBeginning = 0;

var FINANCIAL_MAX_ITERATIONS = 128; //以128
变化的投注准确性VAR FINANCIAL_PRECISION = 0.0000001; // 1.0e-8

var y,y0,y1,x0 ,x1 = 0,f = 0,i = 0;
var rate = interest; (数学证券(利率)< FINANCIAL_PRECISION)
{
y = presentValue *(1+ paymentsPerYear * rate)+ paymentAmount *(1+ rate * dueEndOrBeginning)* paymentsPerYear + futureValue;
}
else
{
f = Math.exp(paymentsPerYear * Math.log(1 + rate));
y = presentValue * f + paymentAmount *(1 / rate + dueEndOrBeginning)*(f - 1)+ futureValue;
}
y0 = presentValue + paymentAmount * paymentsPerYear + futureValue;
y1 = presentValue * f + paymentAmount *(1 / rate + dueEndOrBeginning)*(f - 1)+ futureValue;

//通过Newton割线方法找到根
i = x0 = 0.0;
x1 =费率; ((Math.abs(y0-y1)> FINANCIAL_PRECISION)
&&(i< FINANCIAL_MAX_ITERATIONS))

rate =(y1 * x0 - y0 * x1)/(y1-y0);
x0 = x1;
x1 =费率; (数学证券(利率)< FINANCIAL_PRECISION)

y = presentValue *(1+ paymentsPerYear * rate)+ paymentAmount *(1+利率* dueEndOrBeginning)* b
$ b * paymentsPerYear + futureValue;
}
else
{
f = Math.exp(paymentsPerYear * Math.log(1 + rate));
y = presentValue * f + paymentAmount *(1 / rate + dueEndOrBeginning)*(f - 1)+ futureValue;
}

y0 = y1;
y1 = y;
++ i;
}
退货率;
}


I'm looking for a simple javascript financial RATE function, and I found this one. But it seems too difficult to understand. I want to simplify this function, and I need your help. If anyone has a simplest function, please answer. (It's a excel RATE function equivalent.)

var rate = function(nper, pmt, pv, fv, type, guess) {
  if (guess == null) guess = 0.01;
  if (fv == null) fv = 0;
  if (type == null) type = 0;

  var FINANCIAL_MAX_ITERATIONS = 128;//Bet accuracy with 128
  var FINANCIAL_PRECISION = 0.0000001;//1.0e-8

  var y, y0, y1, x0, x1 = 0, f = 0, i = 0;
  var rate = guess;
  if (Math.abs(rate) < FINANCIAL_PRECISION) {
     y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
  } else {
     f = Math.exp(nper * Math.log(1 + rate));
     y = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;
  }
  y0 = pv + pmt * nper + fv;
  y1 = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;

  // find root by Newton secant method
  i = x0 = 0.0;
  x1 = rate;
  while ((Math.abs(y0 - y1) > FINANCIAL_PRECISION) && (i < FINANCIAL_MAX_ITERATIONS)) {
     rate = (y1 * x0 - y0 * x1) / (y1 - y0);
     x0 = x1;
     x1 = rate;

     if (Math.abs(rate) < FINANCIAL_PRECISION) {
        y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
     } else {
        f = Math.exp(nper * Math.log(1 + rate));
        y = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;
     }

     y0 = y1;
     y1 = y;
     ++i;
  }
  return rate;}

Thanks!

解决方案

The math is too complicated for me to understand, but this might be easier for you to read. Some of the variables have been renamed to make more sense, and it's formatted to be easier on your eyes

function rate(paymentsPerYear, paymentAmount, presentValue, futureValue, dueEndOrBeginning, interest)
{
    //If interest, futureValue, dueEndorBeginning was not set, set now
    if (interest == null)
        interest = 0.01;

    if (futureValue == null)
        futureValue = 0;

    if (dueEndOrBeginning == null)
        dueEndOrBeginning = 0;

    var FINANCIAL_MAX_ITERATIONS = 128;//Bet accuracy with 128
    var FINANCIAL_PRECISION = 0.0000001;//1.0e-8

    var y, y0, y1, x0, x1 = 0, f = 0, i = 0;
    var rate = interest;
    if (Math.abs(rate) < FINANCIAL_PRECISION)
    {
        y = presentValue * (1 + paymentsPerYear * rate) + paymentAmount * (1 + rate * dueEndOrBeginning) * paymentsPerYear + futureValue;
    }
    else
    {
        f = Math.exp(paymentsPerYear * Math.log(1 + rate));
        y = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;
    }
    y0 = presentValue + paymentAmount * paymentsPerYear + futureValue;
    y1 = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;

    // find root by Newton secant method
    i = x0 = 0.0;
    x1 = rate;
    while ((Math.abs(y0 - y1) > FINANCIAL_PRECISION)
        && (i < FINANCIAL_MAX_ITERATIONS))
    {
        rate = (y1 * x0 - y0 * x1) / (y1 - y0);
        x0 = x1;
        x1 = rate;

        if (Math.abs(rate) < FINANCIAL_PRECISION)
        {
            y = presentValue * (1 + paymentsPerYear * rate) + paymentAmount * (1 + rate * dueEndOrBeginning) * paymentsPerYear + futureValue;
        }
        else
        {
            f = Math.exp(paymentsPerYear * Math.log(1 + rate));
            y = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;
        }

        y0 = y1;
        y1 = y;
        ++i;
    }
    return rate;
}

这篇关于简单的财务费率函数在JavaScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆