Home > Net >  Calculation divergence in Python
Calculation divergence in Python

Time:04-27

I did this calculation in Python:

    ((1.00044*(1.00175/1.00044)**(252/(4-1))**((1-1)/252))**(252/1)-1)*100

    55.36617662154271

The same calculation in Excel presents this result: 11,7234.

What is the reason for this divergence?

CodePudding user response:

In both Excel and Python, your expression can be reduced to:

((1.00044*(1.00175/1.00044)**84**0)**252-1)*100

The difference is how they evaluate power expressions (exponentiation). Python follows mathematical conventions in applying right-associative exponentiation. 2 ** 3 ** 4 == 2 ** (3 ** 4) == 2417851639229258349412352. In Excel, exponentiation is left-to-right so 2 ^ 3 ^ 4 is evaluated as (2 ^ 3) ^ 4 = 4096.

If you add brackets to make Python evaluate the **84 before the **0 then you get the same result as in Excel:

((1.00044*((1.00175/1.00044)**84)**0)**252-1)*100   # 11.7234

Or if you add brackets in a different place you can get the Python result in Excel:

=((1.00044*(1.00175/1.00044)^(84^0))^252-1)*100   # 55.366
  • Related