如何pow函数工作? [英] how does the pow function work?

查看:324
本文介绍了如何pow函数工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待我自己写的幂函数与NSDecimalNumbers和不是整数指数工作。我第一次尝试使用牛顿法的组合,内置的整数次幂的方法,但由于牛顿法我得到溢出错误,当我有超过2位小数指数。所以我想也许浮点值POW功能可以作为我自己的函数一个很好的模式。所以,我想知道是否有人知道在哪里我可以喜欢某种对pow函数的内部工作文件?

I am looking to write my own power function to work with NSDecimalNumbers and exponents that are not whole numbers. I first tried to use a combination of newtons method and the built in integer power method, but due to newtons method i am getting overflow errors when I have exponents with more than 2 decimals. So I thought maybe the float value pow function might serve as a good model for my own function. So I was wondering if anyone knows where I can fond some sort of documentation on the inner workings of the pow function?

编辑:

@ wombat57,这些链接看起来像他们可能是我所期待的,但是我不知道去阅读。你认为该算法实际上是我在用的。溢出来自牛顿法由于非常大的指数。因为我越来越小数形式指数我必须首先转换成分数。在code这个鼎的唯一方式,因为据我所知,十小数乘以直到你有一个整数,并将它作为分子。这样做你会得到100 +指数与3个或更多小数位的数字。这会导致溢出错误。

@wombat57, those links look like they could be what I am looking for however I have no idea to read them. The algorithm you suggest is in fact what I am using. the overflow comes from newtons method due to very large exponents. Because I am getting exponents in decimal form I have to convert it to a fraction first. the only way of ding this in code, as far as I know, multiplying the decimal by ten until you have a whole number, and using that as the numerator. Doing this you get exponents of 100+ for numbers with 3 or more decimals. this causes an overflow error.

推荐答案

编辑1:这里是链接到实际的源

EDIT 1: Here are links to the actual source

http://opensource.apple.com/源/的libm / libm中-2026 /来源/英特尔/ expf_logf_powf.c
http://opensource.apple.com/source/Libm /Libm-315/Source/ARM/powf.c

我得到了这个问题,它有一堆相关讨论的链接

I got the links from this question, which has a bunch of relevant discussion

自制POW()C ++

本页面介绍的算法: http://mathforum.org/library/drmath/查看/ 55896.html
的x ^(1 / N)= x的n次方根,且x ^ MN =(X ^米)^ N。因此,的x ^(M ​​/ N)=(x的n次方根)^微米。任意根可以用牛顿法来计算。整数幂可以与幂的平方进行计算。对于不合理的指数,可以直到你得到的显著想要选择的位数使用越来越准确有理逼近。

This page describes an algorithm: http://mathforum.org/library/drmath/view/55896.html. x^(1/n) = the nth root of x, and x^mn = (x^m)^n. Thus, x^(m/n) = (the nth root of x)^m. Arbitrary roots can be calculated with Newton's method. Integer powers can be calculated with Exponentiation by squaring. For irrational exponents, you can use increasingly accurate rational approximations until you get the desired number of significant digits.

编辑2:

牛顿的方法是提高你的猜测目前向你要找到根本的力量。如果功率大,而猜测是甚至有点太高,这可能会导致溢出。这里的一个解决方案是识别这种情况。如果曾经发生溢出,这意味着猜测是太高了。你可以解决这个问题(只要猜结果溢出),目前的猜测设定到最后猜测,没有溢出和当前的猜测之间的值(您可能不得不这样做几次)。也就是说,只要牛顿法溢出,做一个二进制搜索向下延伸未溢出的最后一个猜测。下面是一些Python实现这一切的:

Newton's method involves raising your current guess to the power of the root that you're trying to find. If that power is large, and the guess is even a little too high, this can result in overflow. One solution here is to identify this case. If overflow ever occurs, this means that the guess was too high. You can solve the problem by (whenever a guess results in overflow), setting the current guess to a value between the last guess that did not overflow and the current guess (you may have to do this several times). That is, whenever Newton's method overflows, do a binary search down toward the last guess that did not overflow. Here's some python that implements all of this:

def nroot(n, b, sig_figs = 10):
    g1 = 1.0
    g2 = 1.0
    while True:
        done = False
        while not done:  
            try:
                g3 = g2 - ((g2**b) - n) / (b * (g2**(b-1)))
                done = True
            except OverflowError:
                g2 = (g1 + g2) / 2.0 

        if abs(g2 - g3) < 1.0 / (10**sig_figs):
            return g3
        g1 = g2
        g2 = g3

def npowbysqr(n, p):
    if p == 0:
        return 1.0
    if p % 2 == 0:
        v = npowbysqr(n, p/2)
        return v*v 
    else:
        return n*npowbysqr(n, p-1)

def npow(n, p):
    return npowbysqr(nroot(n, 1000000), int(p*1000000))

print npow(5, 4.3467)
print 5**4.3467   

我要补充一点,有可能是更好的解决方案。这似乎工作,但

I should add that there are probably much better solutions. This does seem to work, however

这篇关于如何pow函数工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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