This JavaScript code returns 115.3:
function FV(rate, nper, pmt, pv, type) {
var pow = Math.pow(1 + rate, nper);
var fv;
if (rate) {
fv = (pmt*(1+rate*type)*(1-pow)/rate)-pv*pow;
} else {
fv = -1 * (pv + pmt * nper);
}
return fv.toFixed(2);
}
document.write( FV(0.06/12,12,-(2750+1375)/12,-0,0)-(0+2750+1375) )
This Java code returns 106.1:
public double FV(double rate, double nper, double pmt, double pv, int type) {
double pow = Math.pow(1 + rate, nper);
double fv;
if (rate > 0) {
fv = (pmt*(1+rate*type)*(1-pow)/rate)-pv*pow;
} else {
fv = -1 * (pv + pmt * nper);
}
return fv;
}
System.out.println(FV(0.06/12,12,-(2750+1375)/12,-0,0)-(0+2750+1375));
They look the same to me, but they return different values. What's wrong?
integerliteralsintand int devision round up the value. whereas in javascript devision operator return value dynamically, ex.1 / 2 // returns 0.5 in JavaScriptand1 / 2 // returns 0 in JavaInteger division rounds toward 0. That is, the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d · q| ≤ |n|. Moreover, q is positive when |n| ≥ |d| and n and d have the same sign, but q is negative when |n| ≥ |d| and n and d have opposite signs.