Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

When rounding numbers (for example, rounding a real number to the nearest integer), the "round to nearest" rule is usually used. For example, 1.4 is rounded down to 1 and 1.6 is rounded up to 2.

However, some sort of tie-breaking rule is needed in the case of a real number exactly halfway between two integers, such as 2.5 or 3.5. There are several such tie-breaking rules; here are two possibilities:

  1. When I was in elementary school in the United States in the 1990s, the rule taught was to round up in the case of a tie. So, 2.5 is rounded up to 3 and 3.5 is rounded up to 4. Wikipedia calls this the "round half up" tie-breaking rule. This method introduces a "toward-infinity" bias, since 0.5 is always rounded up. I guess we were taught this way because that was before we were taught about even and odd integers?

  2. Another (perhaps better) possibility is the "round half to even" tie-breaking rule, where the halfway value is rounded to the nearest even integer. So, 2.5 is rounded to 2 and 3.5 is rounded to 4. This method does not have the overall "toward-infinity" bias that method (1) has, but it does introduce a "toward-zero" bias for even numbers, and a "toward-infinity" bias for odd numbers. Wikipedia says that this method is the default method in IEEE 754 floating point functions, so apparently this is a well-established method.

Of course, there are still other possibilities, like "round half to odd" and so on, but for now, let's just consider methods (1) and (2).

My original question was, which method does Mathematica use? I did the following test using NumberForm and Round:

list1 = {2.5, 3.5};
NumberForm[#, {Infinity, 0}] & /@ list1
Round[#] & /@ list1

which gives this output:

{3., 4.}
{2, 4}

so it seems that NumberForm uses method (1) whereas Round uses method (2). Is this a correct assessment?

The Mathematica (version 8) documentation for NumberForm does not seem to comment on this, as far as I can tell. On the other hand, the Mathematica (version 8) documentation for Round says, "Round rounds numbers of the form x.5 toward the nearest even integer."

Based on this, my question is, why do NumberForm and Round apparently use different tie-breaking methods? (I suppose that there may not be an answer to this question, since perhaps it is just "what it is.")

Moreover, are NumberForm and Round different in other ways? For one thing, NumberForm acts as a wrapper and thus only affects printing, not evaluation, whereas Round affects evaluation. But, besides this, are there other fundamental differences between NumberForm and Round in Mathematica 8?

share|improve this question
3  
"When I was in elementary school in the United States in the 1990s" now I'm feeling old. –  bobthechemist Jul 24 '13 at 23:44
    
It is a good question. Before couple of my scripts have failed me I wasn't even aware that there is something like "rounding x.5 toward the nearest even integer". –  Kuba Jul 25 '13 at 0:16
    
I think the difference in tie-breaking method may be there simply to cater for situations where the bias would matter or not. Nevertheless a very interesting find! –  Jonie Jul 25 '13 at 1:21
3  
Actually AFAIK the exact tie breaking method only matters if you repeatedly round during an algorithm. Since the output of NumberForm isn't used in further calculations, it doesn't matter. As of the difference, I guess they were implemented by different people. It makes sense that Round would be implemented by someone with a solid numeric programming background, while a presentation function would possibly be implemented by someone without special numerics knowledge (but maybe more knowledge about presentation issues), who just implemented the rule he learned in school. –  celtschk Jul 25 '13 at 18:27
add comment

2 Answers

This is not a full answer but may be a start. Note that 2.5 and 3.5 are MachinePrecison numbers and are treated as inherently inaccurate. If you specify the precisions explicitly you will get a consistent result:

Round[{2.5`4, 3.5`4}]

{2, 4}

NumberForm[{2.5`4, 3.5`4}, 1]

{2., 4.}

But why NumberForm and Round treat MachinePrecision 2.5 differently?

share|improve this answer
    
In addition: if we evaluate in a Notebook {2.5`1, 3.5`1} we get as output {3.,4.}. –  Alexey Popkov Oct 13 '13 at 10:42
add comment

Actually, somewhere deep down in the documentation you'll find:

"Round rounds number of the form x.5 toward the nearest EVEN integer."

Round[{0.5, 1.5, 2.5, 3.5, 4.5}]

returns

{0, 2, 2, 4, 4}

You may consider using the Floor or Ceiling functions.

share|improve this answer
2  
It is not deep in the Documentation, it is just the second row under "More Information" for Round. The problem is the inconsistent behavior of Round, NumberForm (which behaves inconsistently even with itself) and StandardForm. –  Alexey Popkov Oct 13 '13 at 10:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.