我有一个循环我试图使用NSDecimalNumber值运行,但返回的值始终是相同的。我理解NSDecimalNumber是不可变的,但我最初使用双值,并在最后得到错误的结果,我认为是一些浮点错误/舍入错误。这是代码:
double balanceAmount = loanAmountValue;
double rtemp = r / (n * 12);
double intA = balanceAmount * rtemp;
double principalA = payfinal - intA;
double principal = balanceAmount - principalA;
NSDecimalNumber *balDeciminal = (NSDecimalNumber *) [NSDecimalNumber numberWithDouble:balanceAmount];
NSDecimalNumber *rTempDecimal = (NSDecimalNumber *) [NSDecimalNumber numberWithDouble:rtemp];
NSDecimalNumber *payFinalDecimal = (NSDecimalNumber *) [NSDecimalNumber numberWithDouble:payfinal];
NSDecimalNumber *principalDecimal = (NSDecimalNumber *) [NSDecimalNumber numberWithDouble:principalA];
for (n = n * 12; n != 0; --n) {
NSDecimalNumber *realBalanceDecimal = [balDeciminal decimalNumberBySubtracting:principalDecimal];
NSDecimalNumber *interestDecimal = [balDeciminal decimalNumberByMultiplyingBy:rTempDecimal];
NSDecimalNumber *principalDecimalAmount = [payFinalDecimal decimalNumberBySubtracting:interestDecimal];
NSString *tempInterest = [NSString stringWithFormat:@"$%@", interestDecimal];
[interestLabels addObject:tempInterest];
NSString *tempPrincipal = [NSString stringWithFormat:@"$%@", principalDecimalAmount];
[pricipalLabels addObject:tempPrincipal];
NSString *tempBalance = [NSString stringWithFormat:@"$%@", balDeciminal];
[balanceLabels addObject: tempBalance];
}
NSLog(@"%@", balanceLabels);
NSLog(@"%@", pricipalLabels);
NSLog(@"%@", interestLabels);
如果NSDecimalNumber不允许我进行这些计算,那么有人会建议其他能够返回准确结果的其他内容吗?
谢谢!
编辑:双重代码
double r = interestAmountValue/200;
//NSLog(@"%f", r);
double n = yearAmountValue;
double rPower = pow(1+r, 0.166666666);
double tophalf = rPower - 1;
double nPower = (-12 * n);
double bothalf = pow(rPower, nPower);
double bothalffinal = 1 - bothalf;
double tempfinal = tophalf / bothalffinal;
double payfinal = loanAmountValue * tempfinal;
double totalPaymentd = payfinal * n * 12;
double totalInterestd = totalPaymentd - loanAmountValue;
for (n = n * 12; n != 0; --n) {
double realBalance = balanceAmount - principalA;
double interest = balanceAmount * rtemp;
NSLog(@"%f", interest);
double principalAmount = payfinal - interest;
balanceAmount -= principalA;
}
答案 0 :(得分:0)
我想知道是什么让你认为将一个双重放入NSDecimalNumber可能会以某种方式创建额外的精度。它在我看来,在循环中你总是做完全相同的计算。
double给出大约15到16个小数的精度。如果您计算贷款30年,由于没有足够的精确度,您不太可能遇到问题。我建议您使用双精度数字发布代码,让人们看看你出错的地方。
答案 1 :(得分:0)
需要清理和检查代码:
for
循环不会每次迭代产生更改
使用调试器找出原因。也许五个变量未被使用的事实是暗示。
未使用以下变量:
intA
principalA
principal
principalDecimal
realBalanceDecimal
答案 2 :(得分:0)
您遇到的问题是,您没有检查贷款是否已经结清,而您输入的是负数,这意味着潜在的问题在于付款的扩散。
double loanAmountValue = 100000;
double balanceAmount = loanAmountValue;
double n = 30;
double interestAmountValue = 8;
double r = interestAmountValue/1200;
double rtemp = r/(n*12);
double intA = balanceAmount * rtemp;
//NSLog(@"%f", r);
double rPower = pow(1+r, 0.166666666);
double tophalf = rPower - 1;
double nPower = (-12 * n);
double bothalf = pow(rPower, nPower);
double bothalffinal = 1 - bothalf;
double tempfinal = tophalf / bothalffinal;
double payfinal = loanAmountValue * tempfinal;
double principalA = balanceAmount - intA;
double totalPaymentd = payfinal * n * 12;
double totalInterestd = totalPaymentd - loanAmountValue;
for (n = n * 12; n != 0; --n) {
double realBalance = balanceAmount - principalA;
double interest = balanceAmount * rtemp;
NSLog(@"interest: %f", interest);
NSLog(@"rtemp: %f",rtemp);
double principalAmount = payfinal - interest;
// Check for negative balance
if (balanceAmount < principalAmount) {
NSLog(@"Balance Amount: %f",balanceAmount);
NSLog(@"Months Left: %f",n);
break;
}
balanceAmount -= principalAmount;
NSLog(@"balanceAmount %f",balanceAmount);
}
使用n = 30年,loanAmountValue = 100000和interestAmountValue = 8%的代码。我还有63个月的贷款还清了。我假设r = interestAmountValue / 1200而不是200来获得每月的百分比? 关于NSDecimal数字的使用,我不知道浮点精度会对你的修正抵押贷款计算器产生什么影响,但你遇到的问题不在于使用双数据类型,而在于付款的扩散。