Algorithm 高功率双精度

Algorithm 高功率双精度,algorithm,math,numeric,exponent,Algorithm,Math,Numeric,Exponent,你如何用自己选择的编程语言来求解下面的方程 (1-1/X)^Y 轻松点 但是当X&Y非常大并且X>>Y时呢 e、 g 看起来这应该是一个足够简单的问题,但在应用电源之前绕过双精度问题是我无法解决的。嗯,(1-1/X)^Y=exp(Y*log(1-1/X))。如果X非常大,并且远大于Y,则可以使用 log(1 - 1/x) = -1/x -1/(2*X^2) + O(1/X^3) 计算 exp(-(Y/X+ Y/(2*X*X))) 如果X比Y大不了多少,可能需要使用对数泰勒级数的第三项甚至第

你如何用自己选择的编程语言来求解下面的方程

(1-1/X)^Y
轻松点

但是当X&Y非常大并且X>>Y时呢

e、 g

看起来这应该是一个足够简单的问题,但在应用电源之前绕过双精度问题是我无法解决的。

嗯,
(1-1/X)^Y=exp(Y*log(1-1/X))
。如果
X
非常大,并且远大于
Y
,则可以使用

log(1 - 1/x) = -1/x -1/(2*X^2) + O(1/X^3)
计算

exp(-(Y/X+ Y/(2*X*X)))
如果
X
Y
大不了多少,可能需要使用对数泰勒级数的第三项甚至第四项。

嗯,
(1-1/X)^Y=exp(Y*log(1-1/X))
。如果
X
非常大,并且远大于
Y
,则可以使用

log(1 - 1/x) = -1/x -1/(2*X^2) + O(1/X^3)
计算

exp(-(Y/X+ Y/(2*X*X)))
如果
X
没有比
Y
大多少,可能需要使用对数泰勒级数的第三项甚至第四项。

使用的计算是近似的:

octave:1> x = 10^40
x =  1.0000e+40
octave:2> y = 10^12
y =  1.0000e+12
octave:3> (1-1/x)^y
ans =  1

octave:8> exp(-(y/x + y /(2*x*x)))
ans =  1
如果Daniel Fischer所做的计算是正确的,那么在Java中使用BigDecimal计算
exp(-Y/X+Y/(2*X*X))
的代码是:

public static void main(String[] args) {
    BigDecimal x = new BigDecimal(10,MathContext.UNLIMITED).pow(40);
    BigDecimal y = new BigDecimal(10,MathContext.UNLIMITED).pow(12);

    BigDecimal twoXSquared = new BigDecimal(2,MathContext.UNLIMITED).multiply(x).multiply(x);
    BigDecimal yDividedByTwoXSquared = y.divide(twoXSquared);

    BigDecimal yDividedByX = y.divide(x);


    BigDecimal exponent = new BigDecimal(-1,MathContext.UNLIMITED).multiply(yDividedByX.add(yDividedByTwoXSquared));
    System.out.println(exponent.toEngineeringString());

    BigDecimal result = new BigDecimal(Math.E,MathContext.UNLIMITED).pow(exponent.intValue());

    System.out.println(result.toEngineeringString());

}
使用以下近似计算:

octave:1> x = 10^40
x =  1.0000e+40
octave:2> y = 10^12
y =  1.0000e+12
octave:3> (1-1/x)^y
ans =  1

octave:8> exp(-(y/x + y /(2*x*x)))
ans =  1
如果Daniel Fischer所做的计算是正确的,那么在Java中使用BigDecimal计算
exp(-Y/X+Y/(2*X*X))
的代码是:

public static void main(String[] args) {
    BigDecimal x = new BigDecimal(10,MathContext.UNLIMITED).pow(40);
    BigDecimal y = new BigDecimal(10,MathContext.UNLIMITED).pow(12);

    BigDecimal twoXSquared = new BigDecimal(2,MathContext.UNLIMITED).multiply(x).multiply(x);
    BigDecimal yDividedByTwoXSquared = y.divide(twoXSquared);

    BigDecimal yDividedByX = y.divide(x);


    BigDecimal exponent = new BigDecimal(-1,MathContext.UNLIMITED).multiply(yDividedByX.add(yDividedByTwoXSquared));
    System.out.println(exponent.toEngineeringString());

    BigDecimal result = new BigDecimal(Math.E,MathContext.UNLIMITED).pow(exponent.intValue());

    System.out.println(result.toEngineeringString());

}

我相信它依赖于语言-在某些语言中,我相信您会使用一些实现定点算术的库,如java。使用wolframalpha.com扩展到一个没有任何X^Y术语的系列表示,然后根据需要计算出数列中的任意多个项,以达到所需的精度。向最接近的投票者:这是如何脱离主题的?我相信这取决于语言-在某些语言中,我相信您会使用实现定点算术的库,就像在java中一样。使用wolframalpha.com扩展到没有任何X^Y项的序列表示,然后根据需要计算尽可能多的序列项,以达到所需的精度。对于closevoter:这是如何离题的?