1

I am trying to write a program for finding the sum upto nth term of the Leibniz Series.

I want to print answers on different lines correct upto 15 decimal places.

The code.

a=[]
for _ in range(int(input().strip())):
   n=int(input().strip())
   a.append(sum([(-1)**i/(2*i+1) for i in range(n)]))
print("\n".join(str(i) for i in a))

My output:

0.7604599047323508
0.77290595166696

Expected Output:

0.760459904732351
0.772905951666960

NOTE- I want to keep the code as minimal as possible

void
  • 2,571
  • 2
  • 20
  • 35

3 Answers3

2

If you want,

0.7604599047323508

to be rounded-off upto 15 decimal places like this,

0.760459904732351

That's pretty simple:

i = 0.7604599047323508
print(i)
print("%.15f" % i) 

Output:

0.7604599047323508
0.760459904732351

If you have number of decimal length less than 15. Then 0s are added. Take a look,

x = 0.760459904732 #Less than 15

print(x)
print("%.15f" % x)

Output:

0.760459904732
0.760459904732000

UPDATE:

There is also the built-in function round() you can use that too. round(value,places)

Example:

>>> a=0.7604599047323508
>>> round(a,15)
0.760459904732351

For your code: Using print()

a=[]
for __ in range(int(input().strip())):
   n=int(input().strip())
   a.append(sum([(-1)**i/(2*i+1) for i in range(n)]))

for i in a:
    print("%.15f" % i)

Also why do you use _ ? Use some other name instead. To know the purpose of _ try this in your interpreter.

>>> a=10
>>> b=20
>>> a+b
30
>>> _
30
>>> a*10000
100000 variable name.
>>> _
100000
>>> a
10

_ stores the most recent calculation in interpreter. _ is used as a throwaway variable __ is better than _ I guess.

void
  • 2,571
  • 2
  • 20
  • 35
  • Why exactly did someone downvote my answer? Can you elaborate? – void Jul 01 '17 at 08:56
  • Yes, `_` is used as a throwaway variable in python, see [this](https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python) for example, but many other soruces say the same. Plus you add a superfluous `for` cycle for printing – gionni Jul 01 '17 at 08:59
  • **`_` should be avoided `__` should be used** is mentioned clearly in the accepted answer. To avoid conflicts – void Jul 01 '17 at 09:01
  • True, but you should specify it in your answer instead of saying to use another name instead – gionni Jul 01 '17 at 09:03
  • *superfluous `for `* why do you say that? Explain – void Jul 01 '17 at 12:45
  • It can be replaced with `join` as in OP example (and my own answer :P ), although i don't know which one is faster (i guess `join` because it's a built in, but I'd have to try). The point is OP explicitly asks to keep code as minimal as possible – gionni Jul 02 '17 at 10:25
  • You use a `for` inside the join to get elements. I use them to print. Do you really think mine is *superfluous* ? – void Jul 02 '17 at 14:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148175/discussion-between-gionni-and-s-vishnu). – gionni Jul 02 '17 at 16:51
0

You can use the Decimal module to get exact precision

from decimal import *
for x in range (0,int(input())):
    s,n=Decimal('0'),int(input())
    for i in range(0,n):
        s+= Decimal(str(((-1)**i)/((2*i)+1)))
    print (s)
Akshat
  • 165
  • 1
  • 1
  • 14
-1

Here you go:

"\n".join("{:.15f}".format(i) for i in a)

gionni
  • 1,284
  • 1
  • 12
  • 32