0
$\begingroup$

Suppose I have the number 0.101 in binary. If I want to round it to 2 places after the radix point using the algorithm rounding to the nearest I can easily find the difference between two available options (0.10 and 0.11):

0.101 - 0.10 =  0.001 = 1/8
0.101 - 0.11 = -0.001 = -1/8

However, how I would do the subtraction for the infinite fraction like 0.1:

$$ 0.1_{10} = 0.0001100110011001100110011001100110011001100110011..._2 $$

Suppose, I want to round it to 10 places after the radix point. How would I go about that?

$\endgroup$
4
  • 1
    $\begingroup$ Ten places takes you to $0.0001100110$ the next few digits are $011$. So if you round to the nearest you round down in this case. If the next few digits begin with a 0 you will always round down. If the next few digits begin with 11 you will always round up. You will also round up if the next digit is 1 and at some point there is a subsequent 1, eg 10000000001. The only tie-breaker case is 1000... . $\endgroup$ – almagest May 18 '16 at 11:00
  • $\begingroup$ Thanks, but how did you calculate that? To get the same result I'd need to do subtraction $\endgroup$ – Max Koretskyi May 18 '16 at 11:02
  • 1
    $\begingroup$ Doing a subtraction is a huge waste of time. What is the tie breaker case where the number is exactly halfway between the two rounding possibilities? Answer it is the smaller one followed by a single 1. If there is any 1 after that, then it must be closer to the larger number. If the first digit is a 0, then we always round down (except in the case where that is followed by nothing but 1s, since that is another way of writing a 1 followed by 0s) because the smaller number must be closer. $\endgroup$ – almagest May 18 '16 at 11:05
  • $\begingroup$ Okay, let me digest it and I'll get back to you with questions. Maybe you could post it as an answer? $\endgroup$ – Max Koretskyi May 18 '16 at 11:08
1
$\begingroup$

There is a much quicker way of applying the "round to the nearest" rule than by carrying out subtractions.

Suppose rounding down gives $a$ and rounding up gives $b$. The "round to the nearest" rule says round to whichever of $a,b$ is nearer. That is unambiguous unless the number is $\frac{a+b}{2}$ which is equidistant. We deal with that case by a tie-breaker rule (which you asked about in an earlier question).

But another way of expressing the rule is that we round up if the number is $>\frac{a+b}{2}$ and round down if it is $<\frac{a+b}{2}$.

In the case of binary fractions the number $\frac{a+b}{2}$ is the smaller number $a$ followed by a digit 1 (and nothing or only 0s thereafter). So this alternative method is much quicker. If the next digit is a 1 and there is at some point a subsequent 1, then we round up, because the number must be closer to $b$. Similarly, if the next digit is a 0 and that is not followed by an infinite string of nothing but 1s, then we round down, because the number must be closer to $a$.

You asked in the comments below for an example. For example, suppose you want to round $x=0.0110110$ to 6 places. Your choices are $a=0.011011$ or $b=0.011100$. The midpoint is $\frac{a+b}{2}=0.0110111$ which can be obtained by adding a 1 at the end of $a$. In this case $x$ has a $0$ at the end so you round it down to $a$ because we have $x<\frac{a+b}{2}$.

$\endgroup$
10
  • $\begingroup$ can you please elaborate on this In the case of binary fractions the number (a+b)/2 is the smaller number a followed by a digit 1 (and nothing or only 0s thereafter). ? I think some example would be very helpful. thanks $\endgroup$ – Max Koretskyi May 19 '16 at 9:23
  • $\begingroup$ thanks for you edition, but I still have a lot of questions :(, bear with me please) I don't really understand how you came up with the b version 0.011100? I thought it should be 0.011010 $\endgroup$ – Max Koretskyi May 19 '16 at 9:40
  • 1
    $\begingroup$ If you truncate $x$ at 6 places you get $0.011011$. Rounding up means increasing the last digit by 1. That gives $0.011012$. But digits have to be 1 or 2, so we rewrite that as $0.01110$. Or more formally $0.011011+0.000001=0.011100$. $\endgroup$ – almagest May 19 '16 at 9:44
  • $\begingroup$ thanks a lot, I understand that the a (smaller) number will always be just truncated number? $\endgroup$ – Max Koretskyi May 19 '16 at 10:16
  • $\begingroup$ @Maximus Yes, that is correct. $\endgroup$ – almagest May 19 '16 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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