Commit c515d2e3 authored by Dmitry.Shahtanov's avatar Dmitry.Shahtanov Committed by Alexander.Trofimov

округление параметров даты по целого в финансовых функциях;

 рефакторинг кода.

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@56994 954022d7-b5bf-4e40-9824-e11837661b57
parent 555b8306
......@@ -1664,36 +1664,37 @@ cCUMIPMT.prototype.Calculate = function ( arg ) {
if ( endPeriod instanceof cError ) return this.value = endPeriod;
if ( type instanceof cError ) return this.value = type;
var fRate = rate.getValue(),
nNumPeriods = nper.getValue(),
fVal = pv.getValue(),
nStartPer = startPeriod.getValue(),
nEndPer = endPeriod.getValue(),
nPayType = type.getValue(),
fRmz, fZinsZ = 0;
if ( nStartPer < 1 || nEndPer < nStartPer || fRate <= 0 || nEndPer > nNumPeriods || nNumPeriods <= 0 || fVal <= 0 || ( nPayType != 0 && nPayType != 1 ) )
return this.value = new cError( cErrorType.not_numeric );
rate = rate.getValue();
nper = nper.getValue();
pv = pv.getValue();
startPeriod = startPeriod.getValue();
endPeriod = endPeriod.getValue();
type = type.getValue();
fRmz = getPMT( fRate, nNumPeriods, fVal, 0, nPayType );
var fv, ipmt = 0;
if ( nStartPer == 1 ) {
if ( nPayType <= 0 )
fZinsZ = -fVal;
if ( startPeriod < 1 || endPeriod < startPeriod || rate <= 0 || endPeriod > nper || nper <= 0 || pv <= 0 || ( type != 0 && type != 1 ) )
return this.value = new cError( cErrorType.not_numeric );
nStartPer++;
fv = getPMT( rate, nper, pv, 0, type );
if ( startPeriod == 1 ) {
if ( type <= 0 ){
ipmt = -pv;
}
startPeriod++;
}
for ( var i = nStartPer; i <= nEndPer; i++ ) {
if ( nPayType > 0 )
fZinsZ += getFV( fRate, i - 2, fRmz, fVal, 1 ) - fRmz;
for ( var i = startPeriod; i <= endPeriod; i++ ) {
if ( type > 0 )
ipmt += getFV( rate, i - 2, fv, pv, 1 ) - fv;
else
fZinsZ += getFV( fRate, i - 1, fRmz, fVal, 0 );
ipmt += getFV( rate, i - 1, fv, pv, 0 );
}
fZinsZ *= fRate;
ipmt *= rate;
return this.value = new cNumber( fZinsZ );
return this.value = new cNumber( ipmt );
};
cCUMIPMT.prototype.getInfo = function () {
......@@ -1785,40 +1786,41 @@ cCUMPRINC.prototype.Calculate = function ( arg ) {
if ( endPeriod instanceof cError ) return this.value = endPeriod;
if ( type instanceof cError ) return this.value = type;
var fRate = rate.getValue(),
nNumPeriods = nper.getValue(),
fVal = pv.getValue(),
nStartPer = startPeriod.getValue(),
nEndPer = endPeriod.getValue(),
nPayType = type.getValue(),
fRmz, fKapZ;
if ( nStartPer < 1 || nEndPer < nStartPer || nEndPer < 1 || fRate <= 0 || nNumPeriods <= 0 || fVal <= 0 || ( nPayType != 0 && nPayType != 1 ) )
return this.value = new cError( cErrorType.not_numeric );
rate = rate.getValue();
nper = nper.getValue();
pv = pv.getValue();
startPeriod = startPeriod.getValue();
endPeriod = endPeriod.getValue();
type = type.getValue();
fRmz = getPMT( fRate, nNumPeriods, fVal, 0, nPayType );
var fv, res = 0, nStart = startPeriod;
fKapZ = 0;
if ( startPeriod < 1 || endPeriod < startPeriod || endPeriod < 1 || rate <= 0 || nper <= 0 || pv <= 0 || ( type != 0 && type != 1 ) ){
return this.value = new cError( cErrorType.not_numeric );
}
var nStart = nStartPer;
fv = getPMT( rate, nper, pv, 0, type );
if ( nStart == 1 ) {
if ( nPayType <= 0 )
fKapZ = fRmz + fVal * fRate;
else
fKapZ = fRmz;
if ( type <= 0 ){
res = fv + pv * rate;
}
else{
res = fv;
}
nStart++;
}
for ( var i = nStart; i <= nEndPer; i++ ) {
if ( nPayType > 0 )
fKapZ += fRmz - ( getFV( fRate, i - 2, fRmz, fVal, 1 ) - fRmz ) * fRate;
else
fKapZ += fRmz - getFV( fRate, i - 1, fRmz, fVal, 0 ) * fRate;
for ( var i = nStart; i <= endPeriod; i++ ) {
if ( type > 0 ){
res += fv - ( getFV( rate, i - 2, fv, pv, 1 ) - fv ) * rate;
}
else{
res += fv - getFV( rate, i - 1, fv, pv, 0 ) * rate;
}
}
return this.value = new cNumber( fKapZ );
return this.value = new cNumber( res );
};
cCUMPRINC.prototype.getInfo = function () {
......@@ -1909,23 +1911,24 @@ cDB.prototype.Calculate = function ( arg ) {
if ( month < 1 || month > 12 || salvage <= 0 || life < 0 || period < 0 || cost < 0 ) {
return this.value = new cError( cErrorType.wrong_value_type );
}
var nAbRate = 1 - Math.pow( salvage / cost, 1 / life );
nAbRate = Math.floor( (nAbRate * 1000) + 0.5 ) / 1000;
var nErsteAbRate = cost * nAbRate * month / 12;
var rate = 1 - Math.pow( salvage / cost, 1 / life );
rate = Math.floor( (rate * 1000) + 0.5 ) / 1000;
var firstRate = cost * rate * month / 12;
var res = 0;
if ( Math.floor( period ) == 1 )
res = nErsteAbRate;
res = firstRate;
else {
var nSummAbRate = nErsteAbRate, nMin = life;
if ( nMin > period ) nMin = period;
var iMax = Math.floor( nMin );
for ( var i = 2; i <= iMax; i++ ) {
res = (cost - nSummAbRate) * nAbRate;
nSummAbRate += res;
var sum = firstRate, min = life;
if ( min > period ){ min = period; }
var max = Math.floor( min );
for ( var i = 2; i <= max; i++ ) {
res = (cost - sum) * rate;
sum += res;
}
if ( period > life ){
res = ((cost - sum) * rate * (12 - month)) / 12;
}
if ( period > life )
res = ((cost - nSummAbRate) * nAbRate * (12 - month)) / 12;
}
this.value = new cNumber( res );
......@@ -3956,7 +3959,7 @@ cODDLPRICE.prototype.Calculate = function ( arg ) {
settlement = Math.floor(settlement.getValue());
maturity = Math.floor(maturity.getValue());
last_interest = Math.floor(maturity.getValue());
last_interest = Math.floor(last_interest.getValue());
rate = rate.getValue();
yld = yld.getValue();
redemption = redemption.getValue();
......@@ -4099,7 +4102,7 @@ cODDLYIELD.prototype.Calculate = function ( arg ) {
settlement = Math.floor(settlement.getValue());
maturity = Math.floor(maturity.getValue());
last_interest = Math.floor(maturity.getValue());
last_interest = Math.floor(last_interest.getValue());
rate = rate.getValue();
pr = pr.getValue();
redemption = redemption.getValue();
......@@ -5204,7 +5207,7 @@ cTBILLEQ.prototype.Calculate = function ( arg ) {
return this.value = new cError( cErrorType.not_numeric );
}
this.value = new cNumber( ( 365 * discount.getValue() ) / ( 360 - ( discount.getValue() * nDiff ) ) );
this.value = new cNumber( ( 365 * discount ) / ( 360 - ( discount * nDiff ) ) );
this.value.numFormat = 9;
return this.value;
......
......@@ -81,6 +81,8 @@ Date.prototype.getExcelDate = function () {
Date.prototype.getDateFromExcel = function ( val ) {
val = Math.floor(val);
if ( g_bDate1904 ) {
return new Date( val * c_msPerDay + this.getExcelNullDate() );
}
......@@ -98,22 +100,22 @@ Date.prototype.getDateFromExcel = function ( val ) {
};
Date.prototype.addYears = function ( counts ) {
this.setUTCFullYear( this.getUTCFullYear() + counts );
this.setUTCFullYear( this.getUTCFullYear() + Math.floor(counts) );
};
Date.prototype.addMonths = function ( counts ) {
if( this.lastDayOfMonth() ){
this.setUTCDate(1);
this.setUTCMonth( this.getUTCMonth() + counts );
this.setUTCMonth( this.getUTCMonth() + Math.floor(counts) );
this.setUTCDate(this.getDaysInMonth());
}
else{
this.setUTCMonth( this.getUTCMonth() + counts );
this.setUTCMonth( this.getUTCMonth() + Math.floor(counts) );
}
};
Date.prototype.addDays = function ( counts ) {
this.setUTCDate( this.getUTCDate() + counts );
this.setUTCDate( this.getUTCDate() + Math.floor(counts) );
};
Date.prototype.lastDayOfMonth = function ( ) {
......@@ -3975,20 +3977,20 @@ function lcl_Erf0065( x ) {
8.49717371168693357E-3,
3.64915280629351082E-4
];
var fPSum = 0.0, fQSum = 0.0, fXPow = 1.0;
var pSum = 0.0, qSum = 0.0, xPow = 1.0;
for ( var i = 0; i <= 4; ++i ) {
fPSum += pn[i] * fXPow;
fQSum += qn[i] * fXPow;
fXPow *= x * x;
pSum += pn[i] * xPow;
qSum += qn[i] * xPow;
xPow *= x * x;
}
return x * fPSum / fQSum;
return x * pSum / qSum;
}
/** Approximation algorithm for erfc for 0.65 < x < 6.0. */
function lcl_Erfc0600( x ) {
var fPSum = 0.0,
fQSum = 0.0,
fXPow = 1.0, pn, qn;
var pSum = 0.0,
qSum = 0.0,
xPow = 1.0, pn, qn;
if ( x < 2.2 ) {
pn = [
......@@ -4030,12 +4032,12 @@ function lcl_Erfc0600( x ) {
}
for ( var i = 0; i < 6; ++i ) {
fPSum += pn[i] * fXPow;
fQSum += qn[i] * fXPow;
fXPow *= x;
pSum += pn[i] * xPow;
qSum += qn[i] * xPow;
xPow *= x;
}
fQSum += qn[6] * fXPow;
return Math.exp( -1.0 * x * x ) * fPSum / fQSum;
qSum += qn[6] * xPow;
return Math.exp( -1.0 * x * x ) * pSum / qSum;
}
/** Approximation algorithm for erfc for 6.0 < x < 26.54 (but used for all x > 6.0). */
......@@ -4055,14 +4057,14 @@ function lcl_Erfc2654( x ) {
3.73997570145040850E1
];
var fPSum = 0.0, fQSum = 0.0, fXPow = 1.0;
var pSum = 0.0, qSum = 0.0, xPow = 1.0;
for ( var i = 0; i <= 4; ++i ) {
fPSum += pn[i] * fXPow;
fQSum += qn[i] * fXPow;
fXPow /= x * x;
pSum += pn[i] * xPow;
qSum += qn[i] * xPow;
xPow /= x * x;
}
return Math.exp( -1.0 * x * x ) * fPSum / (x * fQSum);
return Math.exp( -1.0 * x * x ) * pSum / (x * qSum);
}
function rtl_math_erf( x ) {
......@@ -4075,18 +4077,18 @@ function rtl_math_erf( x ) {
bNegative = true;
}
var fErf = 1.0;
var res = 1.0;
if ( x < 1.0e-10 )
fErf = parseFloat( x * 1.1283791670955125738961589031215452 );
res = parseFloat( x * 1.1283791670955125738961589031215452 );
else if ( x < 0.65 )
fErf = lcl_Erf0065( x );
res = lcl_Erf0065( x );
else
fErf = 1.0 - rtl_math_erfc( x );
res = 1.0 - rtl_math_erfc( x );
if ( bNegative )
fErf *= -1.0;
res *= -1.0;
return fErf;
return res;
}
function rtl_math_erfc( x ) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment