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)

 Accepted Answer

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 Comment

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

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!