I would appreciate it if someone could tell me whether I can set definitely the precision of a float variable in Python. I mean a function like long/short for format in MATLAB. Thank you!
Please here is an example!
import numpy as np
from matplotlib import pyplot as plt
from mayavi import mlab
import matplotlib.cm as cm
from matplotlib import animation
from decimal import *
setcontext(program_context)
program_context = Context(prec=4, rounding=ROUND_UP)
a=.1
dt=.1
nx=31
ny=31
p=6
x = np.linspace(-p,p,nx)
y = np.linspace(-p,p,ny)
qx=2.0*p/(nx-1)
qy=2.0*p/(ny-1)
ax.set_xlim(-p,p)
ax.set_ylim(-p,p)
X,Y = np.meshgrid(x,y)
U=-a*Y
V=a*X
xp = np.empty(11)
yp = np.empty(11)
xp[0]=2
yp[0]=2
for i in range(10):
xp[i+1]=xp[i]+dt*U[i+(2+p)/qx,i+(2+p)/qy]
yp[i+1]=yp[i]+dt*V[i+(2+p)/qx,i+(2+p)/qy]
print (xp[i], yp[i])
I obtain the following:
(2.0, 2.0)
(1.98, 2.02)
(1.956, 2.044)
(1.9279999999999999, 2.0720000000000001)
(1.8959999999999999, 2.1040000000000001)
(1.8599999999999999, 2.1400000000000001)
(1.8199999999999998, 2.1800000000000002)
(1.7759999999999998, 2.2240000000000002)
(1.7279999999999998, 2.2720000000000002)
(1.6759999999999997, 2.3240000000000003)
Is is possible to obtain just 4 numbers in the decimal part? Thank you for your help! Strömungsmechanik