Python mpmath not arbitrary precision?

Ropstah

I'm trying to continue on my previous question in which I'm trying to calculate Fibonacci numbers using Benet's algorithm. To work with arbitrary precision I found mpmath. However the implementation seems to fail above certain value. For instance the 99th value gives:

218922995834555891712

This should be (ref):

218922995834555169026

Here is my code:

from mpmath import *

def Phi():
    return (1 + sqrt(5)) / 2

def phi():
    return (1 - sqrt(5)) / 2

def F(n):
    return (power(Phi(), n) - power(phi(), n)) / sqrt(5)

start = 99
end = 100

for x in range(start, end):
    print(x, int(F(x)))
pts

mpmath provides arbitrary precision (as set in mpmath.mp.dps), but still inaccuate calculation. For example, mpmath.sqrt(5) is not accurate, so any calculation based on that will also be inaccurate.

To get an accurate result for sqrt(5), you have to use a library which supports abstract calculation, e.g. http://sympy.org/ .

To get an accurate result for Fibonacci numbers, probably the simplest way is using an algorithm which does only integer arithmetics. For example:

def fib(n):
  if n < 0:
    raise ValueError

  def fib_rec(n):
    if n == 0:
      return 0, 1
    else:
      a, b = fib_rec(n >> 1)
      c = a * ((b << 1) - a)
      d = b * b + a * a
      if n & 1:
        return d, c + d
      else:
        return c, d

  return fib_rec(n)[0]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Precision loss numpy - mpmath

From Dev

Is Python incorrectly handling this "arbitrary precision integer"?

From Dev

BBP-Algorithm in Python - Working with Arbitrary-precision arithmetic

From Dev

LLVM arbitrary precision integer

From Dev

LLVM arbitrary precision integer

From Dev

Fixed precision vs. arbitrary precision

From Dev

arbitrary floating precision number to string

From Dev

"Multiplication of Arbitrary Precision Numbers" in Scheme

From Dev

stripe node arbitrary precision number

From Dev

Have I encountered a limit to mpmath's floating-point precision?

From Dev

Why mpmath slower than gsl on the same precision? And Which results are right?

From Dev

Is there an equivalent of Python's fractions.Fraction (bignum/arbitrary-precision fraction/rational number class)?

From Dev

Maya Python... workaround for arbitrary precision when coding with maya.cmds

From Dev

Maya Python... workaround for arbitrary precision when coding with maya.cmds

From Dev

Arbitrary precision for decimals square roots in golang

From Dev

Fast arbitrary-precision logarithms with bcmath

From Dev

postgresql date_trunc to arbitrary precision?

From Dev

Arbitrary precision arithmetic with very big factorials

From Dev

Arbitrary precision arithmetic with very big factorials

From Dev

Calculate Pi with arbitrary precision digit by digit

From Dev

How to make mpmath function return python float?

From Dev

precision in python for

From Dev

Calculating pi without arbitrary precision and only basic arithmetic

From Dev

Which programming languages have arbitrary precision floating-point literals?

From Dev

Is it possible to achieve arbitrary-precision arithmetic with no rounding issues in JavaScript?

From Dev

Hash an arbitrary precision value (boost::multiprecision::cpp_int)

From Dev

arbitrary precision linear algebra c/c++ library with complex numbers

From Dev

Arbitrary pre-set precision decimals (Almost like BigDecimal)

From Dev

String format double with arbitrary precision, fixed decimal position

Related Related

  1. 1

    Precision loss numpy - mpmath

  2. 2

    Is Python incorrectly handling this "arbitrary precision integer"?

  3. 3

    BBP-Algorithm in Python - Working with Arbitrary-precision arithmetic

  4. 4

    LLVM arbitrary precision integer

  5. 5

    LLVM arbitrary precision integer

  6. 6

    Fixed precision vs. arbitrary precision

  7. 7

    arbitrary floating precision number to string

  8. 8

    "Multiplication of Arbitrary Precision Numbers" in Scheme

  9. 9

    stripe node arbitrary precision number

  10. 10

    Have I encountered a limit to mpmath's floating-point precision?

  11. 11

    Why mpmath slower than gsl on the same precision? And Which results are right?

  12. 12

    Is there an equivalent of Python's fractions.Fraction (bignum/arbitrary-precision fraction/rational number class)?

  13. 13

    Maya Python... workaround for arbitrary precision when coding with maya.cmds

  14. 14

    Maya Python... workaround for arbitrary precision when coding with maya.cmds

  15. 15

    Arbitrary precision for decimals square roots in golang

  16. 16

    Fast arbitrary-precision logarithms with bcmath

  17. 17

    postgresql date_trunc to arbitrary precision?

  18. 18

    Arbitrary precision arithmetic with very big factorials

  19. 19

    Arbitrary precision arithmetic with very big factorials

  20. 20

    Calculate Pi with arbitrary precision digit by digit

  21. 21

    How to make mpmath function return python float?

  22. 22

    precision in python for

  23. 23

    Calculating pi without arbitrary precision and only basic arithmetic

  24. 24

    Which programming languages have arbitrary precision floating-point literals?

  25. 25

    Is it possible to achieve arbitrary-precision arithmetic with no rounding issues in JavaScript?

  26. 26

    Hash an arbitrary precision value (boost::multiprecision::cpp_int)

  27. 27

    arbitrary precision linear algebra c/c++ library with complex numbers

  28. 28

    Arbitrary pre-set precision decimals (Almost like BigDecimal)

  29. 29

    String format double with arbitrary precision, fixed decimal position

HotTag

Archive