简体   繁体   English

使用NSDecimalNumber执行While循环

[英]Do While Loop with NSDecimalNumber

I am converting a Yield to Maturity Calculation from using float numbers to NSDecimalNumbers. 我正在将使用浮点数转换为NSDecimalNumbers的收益率转换为成熟度计算。 The loop works but hangs up quite often. 循环有效,但经常挂断。 I have provided the code below: 我提供了以下代码:

///////////////////////  YTM TEST ////////////////////////////////

 NSDecimalNumber *years = [NSDecimalNumber decimalNumberWithString:@"10"];
 NSLog(@"Years are: %@", years);


NSDecimalNumber *temp9 = [NSDecimalNumber decimalNumberWithString:@"2"];
    NSLog(@"Coupons per Year: %@", temp9);

NSDecimalNumber *temp2 = [NSDecimalNumber decimalNumberWithString:@"100"]; 
    NSLog(@"Bond Price: %@", temp2);

NSDecimalNumber *temp3 = [NSDecimalNumber decimalNumberWithString:@"100"]; 
    NSLog(@"Bond Par: %@", temp3);

NSDecimalNumber *temp1 = [NSDecimalNumber decimalNumberWithString:@"5"]; 
    NSLog(@"Coupon Interest Rate Input: %@", temp1);

temp1 = [temp1 decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:@"100"]];
    NSLog(@"Coupon Interest Rate: %@", temp1


//temp5 = 0.0; // the initial or lower ytm guess
NSDecimalNumber *temp5 = [NSDecimalNumber decimalNumberWithString:@"0.0"];         

NSLog(@"Initial YTM Calc Guess: %@", temp5);

NSDecimalNumber *finalCalc = [NSDecimalNumber decimalNumberWithString:@"0.0"];
NSLog(@"Initial finalCalc Value is: %@", finalCalc);

{
    do
{
    //if (counter < 300000)


    {

       //temp5 = temp5 + .000005;    // .00005;
        NSDecimalNumber *constant = [NSDecimalNumber decimalNumberWithString:@".000005"];
            //NSLog(@"YTM Calc Constant: %@", constant);

        temp5 = [temp5 decimalNumberByAdding:constant];
            //NSLog(@"Add Constant to Initial Guess: %@", temp5);

       /// Calc 1 & 2
        NSDecimalNumber *cashFlow = [[temp3 decimalNumberByMultiplyingBy:temp1]decimalNumberByDividingBy:temp9];
            //NSLog(@"Cashflow per Coupon Period: %@", cashFlow);

        //Calc 3

        NSDecimalNumber *constantOfOne = [NSDecimalNumber decimalNumberWithString:@"1"];
        NSDecimalNumber *onePlusGuess = [temp5 decimalNumberByAdding:constantOfOne];
        //NSLog(@"One Plus Guess: %@", onePlusGuess);

        // Calc 4

        NSDecimalNumber *thePower = [years decimalNumberByMultiplyingBy:temp9];
        //NSLog(@"thePower: %@", thePower);

        // Calc 5


        NSDecimalNumber *onePlusGuessRaised = [onePlusGuess decimalNumberByRaisingToPower:[thePower floatValue]];
        //NSLog(@"One Plus Guess Raised to Power: %@", onePlusGuessRaised);


        // Calc 6

        NSDecimalNumber *divideIntoOne = [constantOfOne decimalNumberByDividingBy:onePlusGuessRaised];
        //NSLog(@"Divide into One: %@", divideIntoOne);

        //Calc 7

        NSDecimalNumber *complexResult = [constantOfOne decimalNumberBySubtracting:divideIntoOne];                            //NSLog(@"Complex Calc Result: %@", complexResult);


       // Calc 8

        NSDecimalNumber *divideByGuess = [complexResult decimalNumberByDividingBy:temp5];
        //NSLog(@"Divide into One: %@", divideByGuess);


        //Calc 8

        NSDecimalNumber *multipleLeftSide = [cashFlow decimalNumberByMultiplyingBy:divideByGuess];
        //NSLog(@"left Side Done: %@", multipleLeftSide);


        //Calc 9

        NSDecimalNumber *divideIntoPar = [temp3 decimalNumberByDividingBy:onePlusGuessRaised];
        //NSLog(@"Divide into Par: %@", divideIntoPar);

        //Calc 10

        finalCalc = [multipleLeftSide decimalNumberByAdding:divideIntoPar];
       // NSLog(@"Final part of Calc = : %@", finalCalc);


     }



}


while (finalCalc > temp2);


    NSLog(@"I'm Done!");

}
}

I have finally solved the above issue where the processing of the "Do While" would stop looping at various places. 我终于解决了上述问题,“ Do While”的处理将在各个地方停止循环。 In a few cases it would run for 2 minutes or more. 在某些情况下,它将运行2分钟或更长时间。 The problem was that my "While" statement "while (finalCalc > temp2);" 问题是我的“ While”语句为“ while(finalCalc> temp2);”。 was not comparing the values of the two NSDecimalNumbers, but comparing pointer addresses. 不是比较两个NSDecimalNumbers的值,而是比较指针地址。 After a fair amount of research and testing the correct coding for the while statement is " while ([finalCalc compare:temp2] == NSOrderedDescending); " I have tested this multiple times are it works perfectly. 经过大量的研究和测试,while语句的正确编码为“ while([finalCalc compare:temp2] == NSOrderedDescending); ”我已经多次测试了此方法是否完美。 My original code would compile without errors or warnings. 我的原始代码可以编译而不会出现错误或警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM