Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set global output precision python

I've written a library of functions to make my engineering homework easier, and use them in the python interpreter (kinda like a calculator). Some return matrices, some return floats.

The problem is, they return too many decimals. For example, currently, when a number is 0, I get an extremely small number as a return (e.g. 6.123233995736766e-17)

I know how to format outputs individually, but that would require adding a formatter for every line I type in the interpreter. I'm using python 2.6.

Is there a way to set the global output formatting (precision, etc...) for the session?

*Note: For scipy functions, I know I can use

scipy.set_printoptions(precision = 4, suppress = True)

but this doesn't seem to work for functions that don't use scipy.

like image 517
jiminy_crist Avatar asked Sep 15 '12 17:09

jiminy_crist


2 Answers

With numpy, you could use the set_printoptions method (http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html).

For example:

import numpy as np
np.set_printoptions(precision=4)
print(np.pi * np.arange(8))
like image 188
Marc Avatar answered Oct 04 '22 02:10

Marc


What you are seeing is the fact that decimal floating point numbers can only be approximated by binary floating point. See Floating Point Arithmetic: Issues and Limitations.

You could put a module-level variable in your library and use that as the second parameter of round() to round off the return value of the functions in your module, but that is rather drastic.

If you use ipython (which I would recommend for interactive use, much better than the regular interpreter), you can use the 'magic' function %precision.

like image 23
Roland Smith Avatar answered Oct 04 '22 01:10

Roland Smith