>> 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

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 Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

질문:

2018년 3월 4일

댓글:

2018년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by