"Round towards even" in sprintf

조회 수: 7 (최근 30일)
K E
K E 2011년 12월 23일
It seems that sprintf doesn't always round up if a value is halfway between two integers:
sprintf('%4.1f', 0.95) ; % Returns 0.9
But it also seems to disobey the IEEE standard of "round toward the even number", though I could be misunderstanding. Left column is the input to the sprintf statement above and right column is the output.
-0.95 -1.0
-0.85 -0.8
-0.75 -0.8
-0.65 -0.7
-0.55 -0.6
-0.45 -0.5
-0.35 -0.4
-0.25 -0.3
-0.15 -0.1
-0.05 -0.1
0.05 0.1
0.15 0.1
0.25 0.3
0.35 0.4
0.45 0.5
0.55 0.6
0.65 0.7
0.75 0.8
0.85 0.8
0.95 1.0
[Note that roundsd.m enforces the "rounding up" of trailing decimals which can then be fed into sprintf. http://www.mathworks.com/matlabcentral/fileexchange/26212-round-with-significant-digits]

채택된 답변

Walter Roberson
Walter Roberson 2011년 12월 24일
I looked through the documentation earlier today, but I could not find any evidence that sprintf() is required to conform to IEEE rounding rules. The most detailed sprintf() documentation simply indicates that the value will be rounded, without indicating that any particular rounding scheme will be used.
It is not clear at the moment that MATLAB supports round-towards-even. There is an undocumented call
feature('setround',VALUE)
but none of the four known VALUE are for round towards even.
Historically MATLAB is known not to have used round to even; see http://www.mathworks.com/matlabcentral/newsreader/view_thread/16691
  댓글 수: 2
K E
K E 2012년 1월 3일
Holy inconsistency, Batman! Guess I will stick with roundsd.m where rounding follows a predictable pattern of rounding up.
Walter Roberson
Walter Roberson 2012년 1월 3일
The discussion goes back at least a decade; see http://www.mathworks.com/matlabcentral/newsreader/view_thread/24439

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by