What does the following MATLAB code do?

>> x=7.123456789;
>> n=7;
>> delta=0.5;
>> r=x*10^n+delta;
>> r=fix(r);
>> r=r/10^n;
>> disp(r)

 採用された回答

Star Strider
Star Strider 2018 年 3 月 4 日

1 投票

It takes ‘x’, raises it to the ‘n’-th power, adds ‘delta’ to it, then rounds that result toward zero, divides that result by ‘10^n’ and prints the result.
The intent appears to be to round ‘x’ to 7 decimal places.
You can do that easily with the current version of the round function:
r = round(x, 7)
that will produce the same result.

1 件のコメント

Walter Roberson
Walter Roberson 2018 年 3 月 4 日
I noticed that it does not do the same thing as round(x,n) for negative numbers.
>> fix(-pi*100000+0.5)/100000
ans =
-3.14158
>> round(-pi,5)
ans =
-3.14159

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by