使用NSDecimalNumber进行循环

时间:2014-08-06 19:27:48

标签: objective-c nsdecimalnumber

我正在将使用浮点数转换为NSDecimalNumbers的Yield到成熟度计算。循环工作但经常挂起。我提供了以下代码:

///////////////////////  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!");

}
}

1 个答案:

答案 0 :(得分:1)

我终于解决了上述问题,其中“Do While”的处理将在各个地方停止循环。在少数情况下,它会运行2分钟或更长时间。问题是我的“While”语句“while(finalCalc&gt; temp2);”没有比较两个NSDecimalNumbers的值,而是比较指针地址。经过大量的研究和测试后,while语句的正确编码是“ while([finalCalc compare:temp2] == NSOrderedDescending); ”我已多次测试它是否完美无缺。我的原始代码编译时没有错误或警告。