Controlling the precision of floating point number in matlab

前端 未结 3 1249
甜味超标
甜味超标 2021-01-27 09:36

I am dividing a number, say

x=2;
y=1/3;
z=x*y;

I expect z to be 0.66666666666667 i.e. 14 numbers after decimal point and the same

3条回答
  •  不思量自难忘°
    2021-01-27 09:57

    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.

提交回复
热议问题