NSDecimalNumber到NSDecimalNumber的幂 [英] NSDecimalNumber to the power of NSDecimalNumber

查看:59
本文介绍了NSDecimalNumber到NSDecimalNumber的幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个NSDecimalNumbers,我需要将一个应用于其他幂,最初此代码使用的是double,我可以使用pow()函数来计算此值,如下所示:

I have two NSDecimalNumbers and I need to apply one to the power of the other, originally this code was using doubles and I could compute this with the pow() function like this:

double result = pow(value1, value2);

我遇到的问题是我正在转换代码以使用NSDecimalNumbers,尽管它们包括toThePowerOf方法,但它仅接受int值.目前,我唯一需要解决的问题是临时转换NSDecimalNumbers,但这会导致精度损失.

The problem I have is I am converting the code to use NSDecimalNumbers and although they include the method toThePowerOf, it only accepts int values. At the moment the only solution I have to this problem is to convert the NSDecimalNumbers Temporarily but this results in a loss of precision.

double value1 = [decimal1 doubleValue];
double value2 = [decimal2 doubleValue];

double result = pow(value1, value2);
NSDecimalNumber *decimalResult = [[NSDecimalNumber alloc] initWithDouble:result];

有没有一种方法可以使用NSDecimalNumbers进行计算,而又不会失去精度?

Is there a way I can do this computation with NSDecimalNumbers and not lose the precision?

我需要使用它来处理非整数值,例如:

I need this to work with non integer values for example:

value1 = 1.06
value2 = 0.0277777777

推荐答案

正如Joe指出的那样,如果要对正整数次幂执行此操作,则可以在NSDecimal结构上使用 NSDecimalPower()从您的NSDecimalNumber派生而来(我个人更喜欢使用结构,用于

As Joe points out, if you want to do this for positive integer powers, you can use NSDecimalPower() on an NSDecimal struct derived from your NSDecimalNumber (I personally prefer working with the structs, for performance reasons).

对于使用负整数和小数值的更一般的情况,我有一些代码是从Dave DeLong的DDMathParser库修改而来的.此后,他已删除了该库的NSDecimal部分,但您可以找到对此支持的最后一次提交.我将Dave的指数支持扩展到以下功能:

For the more general case of working with negative integers and fractional values, I have some code that I've modified from Dave DeLong's DDMathParser library. He has since removed the NSDecimal portion of this library, but you can find the last commit for this support. I extended Dave's exponential support into the following function:

extern NSDecimal DDDecimalPower(NSDecimal d, NSDecimal power) {
    NSDecimal r = DDDecimalOne();
    NSDecimal zero = DDDecimalZero();
    NSComparisonResult compareToZero = NSDecimalCompare(&zero, &power);
    if (compareToZero == NSOrderedSame) {
        return r;
    }
    if (DDDecimalIsInteger(power))
    {
        if (compareToZero == NSOrderedAscending)
        {
            // we can only use the NSDecimal function for positive integers
            NSUInteger p = DDUIntegerFromDecimal(power);
            NSDecimalPower(&r, &d, p, NSRoundBankers);
        }
        else
        {
            // For negative integers, we can take the inverse of the positive root
            NSUInteger p = DDUIntegerFromDecimal(power);
            p = -p;
            NSDecimalPower(&r, &d, p, NSRoundBankers);
            r = DDDecimalInverse(r);
        }
    } else {
        // Check whether this is the inverse of an integer        
        NSDecimal inversePower = DDDecimalInverse(power);
        NSDecimalRound(&inversePower, &inversePower, 34, NSRoundBankers); // Round to 34 digits to deal with cases like 1/3

        if (DDDecimalIsInteger(inversePower))
        {
            r = DDDecimalNthRoot(d, inversePower);
        }
        else
        {
            double base = DDDoubleFromDecimal(d);
            double p = DDDoubleFromDecimal(power);
            double result = pow(base, p);
            r = DDDecimalFromDouble(result);
        }        
    }
    return r;
}

这将对直接映射到根的正整数幂,负整数幂和小数幂进行精确计算.不过,对于不能完全落入其中一个区间的分数幂,它仍然依赖于浮点计算.

This runs exact calculations on positive integer powers, negative integer powers, and fractional powers that map directly to roots. It still falls back on floating point calculations for fractional powers that don't cleanly fall into one of those bins, though.

不幸的是,这需要他的其他一些辅助功能才能起作用.因此,我上传了他的 _DDDecimalFunctions.h 和 <提供此功能的href ="http://sunsetlakesoftware.com/sites/default/files/_DDDecimalFunctions.m" rel ="nofollow noreferrer"> _ DDDecimalFunctions.m .它们还包括NSDecimal三角函数,对数和其他一些函数.当前在切线实现上存在一些收敛性问题,这就是为什么我还没有完成关于此的公开帖子.

Unfortunately, this requires a few of his other supporting functions to work. Therefore, I've uploaded my enhanced versions of his _DDDecimalFunctions.h and _DDDecimalFunctions.m that provide this functionality. They also include NSDecimal trigonometry, logarithm, and a few other functions. There are currently some issues with convergence on the tangent implementation, which is why I haven't finished a public post about this.

这篇关于NSDecimalNumber到NSDecimalNumber的幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆