how do I show decimal point in print function in python?

Question:

I am trying to find the roots of an equation and I need to print the roots with 10 decimal points I used the following code but it produced an error. how can I tell python to print 10 decimal points?

def find_root(a1,b1,c1,d1,e1,f1,a2,b2,c2,d2,e2,f2):
    coeff=[a1-a2,b1-b2,c1-c2,d1-d2,e1-e2,f1-f2]
    print('%.10f' %np.roots(coeff))

Traceback (most recent call last):

  File "C:UsersMaedehAppDataLocalTempipykernel_144123234902339.py", line 1, in <cell line: 1>
    find_root(0.0024373075,-0.0587498671,0.4937598857,-1.7415327555,3.6624839316,20.8771496554,0.0021396943,-0.0504345999,0.4152634229,-1.4715228738,3.3049020821,20.8406692002)

  File "C:UsersMaedehAppDataLocalTempipykernel_144123260022606.py", line 3, in find_root
    print('%.10f' %np.roots(coeff))

TypeError: only size-1 arrays can be converted to Python scalars
Asked By: david

||

Answers:

You probably want to use np.set_printoptions. One of the parameters it takes is precision.

Answered By: yagod

numpy.roots has no direct parameter to pass while calling function for setting up precision. It will take a array of values and returns array of elements at once. You can set precession on numpy after importing like so.

def find_root(a1,b1,c1,d1,e1,f1,a2,b2,c2,d2,e2,f2):
    coeff=[a1-a2,b1-b2,c1-c2,d1-d2,e1-e2,f1-f2]
 
    print(np.roots(coeff))
     

import numpy as np
np.set_printoptions(precision=10)
find_root(0.0024373075,-0.0587498671,0.4937598857,-1.7415327555,3.6624839316,20.8771496554,0.0021396943,-0.0504345999,0.4152634229,-1.4715228738,3.3049020821,20.8406692002)

which outputs #

[11.41569308+2.82260609j 11.41569308-2.82260609j  2.60173681+1.60008454j
  2.60173681-1.60008454j -0.09501304+0.j]
Answered By: Bhargav
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.