1

How can I print more than about 10 digits of a float in python? Right now, when do

print sqr_newton(10, 3, 0.001) (where sqr_newton is newton's algorithm for square roots; returns a float)

it only gives so many digits after the decimal place... how can I get more?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
  • 1
    Do you really mean "print", or rather "compute with"? You can print digits until you're blue in the face, but the standard float only has a small number of actually stored digits, so the information beyond that is meaningless. – Kerrek SB Nov 10 '11 at 01:50
  • first "compute with" then "print" them – tekknolagi Nov 10 '11 at 01:51
  • In that case you'll need an arbitrary precision float. – Kerrek SB Nov 10 '11 at 01:53

2 Answers2

5

That is the standard precision for floats in python.

For arbitrary precision, you can use the bigfloat package.

Dennis
  • 14,264
  • 2
  • 48
  • 57
3

I'm not sure what operating system you are using but bigfloat requires that the MPFR and GMP libraries already exist on your computer. It may not be easy to use on Windows.

There are two other multiple precision libraries available that support Windows:

1) mpmath which is written in pure Python and is stable and well documented.

2) gmpy2 which is currently under active development. The next release should have a stable API. (Famous last words...)

If bigfloat works for you, great! It is a well-designed and documented package.

Disclaimer: I maintain gmpy2 and have helped with mpmath.

casevh
  • 11,093
  • 1
  • 24
  • 35