Controlling the precision of floating point number in matlab

大城市里の小女人 提交于 2019-12-02 10:20:52

If it is just about diplaying the number you can use the sprintf function

str = sprintf('%.14f', pi); % print PI with exactly 14 digits after decimal point
disp(str);

Or fprintf with just one line:

fprintf('%.14f', pi);

fprintf is for writing to files but prints the string result to the screen if no file identifier is given.

If you actually want the number to have that precision then round off to 14 places like this:

format long g

x=2;
y=1/3;
z=x*y;
round(z*1e14)/1e14

ans =      0.66666666666667

If it's just about displaying then use Deve's sprintf solution.

zizi

you can type in matlab :

digits(10)

it gives you more floating point precision, or also

digits(20)

for example:

 a=1/3
ans = 0.3333
digits(10)
 vpa(a)
 ans= 0.3333333333

search help of matlab by typing doc in command window and then search : digits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!