I am writing a simple program to verify the formula of derivate and in output I am getting too many decimal places which look awful.
I am pretty new to python. I want to only show four decimal places.
here is my code,
from math import *
x = -pi
h = 0.001
while x <= pi:
print("d/dx sin(x) = sin(x+h) - sin(x) / h :", (sin(x + h) - sin(x)) / h)
print("d/dx sin(x) = cos(x): ", cos(x))
x += h
>Solution :
You should use string formatting while printing like this
from math import *
x = -pi
h = 0.001
while x <= pi:
print("d/dx sin(x) = sin(x+h) - sin(x) / h :", "{:.4f}".format((sin(x + h) - sin(x)) / h))
print("d/dx sin(x) = cos(x): ", "{:.4f}".format(cos(x)))
x += h