@@ -7358,40 +7358,30 @@ return /******/ (function(modules) { // webpackBootstrap
7358
7358
}
7359
7359
7360
7360
// Set maximum epsilon for end of iteration
7361
- var epsMax = 1e-10 ;
7361
+ var epsMax = 1e-6 ;
7362
7362
7363
7363
// Set maximum number of iterations
7364
- var iterMax = 50 ;
7365
-
7366
- // Implement Newton's method
7367
- var y , y0 , y1 , x0 , x1 = 0 ,
7368
- f = 0 ,
7369
- i = 0 ;
7364
+ var iterMax = 100 ;
7365
+ var iter = 0 ;
7366
+ var close = false ;
7370
7367
var rate = guess ;
7371
- if ( Math . abs ( rate ) < epsMax ) {
7372
- y = present * ( 1 + periods * rate ) + payment * ( 1 + rate * type ) * periods + future ;
7373
- } else {
7374
- f = Math . exp ( periods * Math . log ( 1 + rate ) ) ;
7375
- y = present * f + payment * ( 1 / rate + type ) * ( f - 1 ) + future ;
7376
- }
7377
- y0 = present + payment * periods + future ;
7378
- y1 = present * f + payment * ( 1 / rate + type ) * ( f - 1 ) + future ;
7379
- i = x0 = 0 ;
7380
- x1 = rate ;
7381
- while ( ( Math . abs ( y0 - y1 ) > epsMax ) && ( i < iterMax ) ) {
7382
- rate = ( y1 * x0 - y0 * x1 ) / ( y1 - y0 ) ;
7383
- x0 = x1 ;
7384
- x1 = rate ;
7385
- if ( Math . abs ( rate ) < epsMax ) {
7386
- y = present * ( 1 + periods * rate ) + payment * ( 1 + rate * type ) * periods + future ;
7387
- } else {
7388
- f = Math . exp ( periods * Math . log ( 1 + rate ) ) ;
7389
- y = present * f + payment * ( 1 / rate + type ) * ( f - 1 ) + future ;
7390
- }
7391
- y0 = y1 ;
7392
- y1 = y ;
7393
- ++ i ;
7368
+
7369
+ while ( iter < iterMax && ! close ) {
7370
+ var t1 = Math . pow ( rate + 1 , periods ) ;
7371
+ var t2 = Math . pow ( rate + 1 , periods - 1 ) ;
7372
+
7373
+ var f1 = future + t1 * present + payment * ( t1 - 1 ) * ( rate * type + 1 ) / rate ;
7374
+ var f2 = periods * t2 * present - payment * ( t1 - 1 ) * ( rate * type + 1 ) / Math . pow ( rate , 2 ) ;
7375
+ var f3 = periods * payment * t2 * ( rate * type + 1 ) / rate + payment * ( t1 - 1 ) * type / rate ;
7376
+
7377
+ var newRate = rate - f1 / ( f2 + f3 ) ;
7378
+
7379
+ if ( Math . abs ( newRate - rate ) < epsMax ) close = true ;
7380
+ iter ++
7381
+ rate = newRate ;
7394
7382
}
7383
+
7384
+ if ( ! close ) return Number . NaN + rate ;
7395
7385
return rate ;
7396
7386
} ;
7397
7387
0 commit comments