本文介绍了在MATLAB中控制浮点数的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要除以一个数字
x=2;
y=1/3;
z=x*y;
我希望z为0.66666666666667
,即小数点后14个数字,y相同.现在,当我这样做时,只会得到0.6667
.如何将答案扩展为精确的14位精度?
I expect z to be 0.66666666666667
i.e. 14 numbers after decimal point and the same for y. Now when I do that I get 0.6667
only. How to expand the answer into exact 14 digit precision?
推荐答案
如果只是显示数字,则可以使用sprintf
函数
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);
或fprintf
仅一行:
fprintf('%.14f', pi);
fprintf 用于写入文件,但将字符串结果打印到屏幕上如果没有给出文件标识符.
fprintf is for writing to files but prints the string result to the screen if no file identifier is given.
这篇关于在MATLAB中控制浮点数的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!