Results 1 to 6 of 6

Thread: Rounding of binary fraction strings

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    33

    Rounding of binary fraction strings

    I need help with the rounding of binary strings. I am not sure I have applied the tie-breaking rule correctly.

    This gives an explanation of "nearest even" method.
    https://indepth.dev/posts/1017/how-t...binary-numbers

    What it states, with the conventional rounding (rounding from halfway up to even), the general rule when rounding to the n-th place prescribes to check the digit following the n-th place in the number. If it’s 0, then the number should always be rounded down. If, instead, the digit is 1 and any of the following digits is also 1, then the number should be rounded up. If, however, all of following digits are 0’s, then a tie-breaking rule must be applied and usually it’s the ‘ties to even’. This rule says that we should round to the number that has 0 at the n-th place.

    For example, with rounding to 2 places:
    1. 0.11001 — rounds down to 0.11, because the digit at the 3-rd place is 0
    2. 0.11101 — rounds up to 1.00, because the digit at the 3-rd place is 1and there are following digits of 1 (5-th place)
    3. 0.11100 — apply the ‘ties to even’ tie-breaker rule and round up because the digit at 3-rd place is 1 and the following digits are all 0's.
    4. 0.11011 — rounds to 1.00.

    Code:
    Public Function Rounding( _
           ByVal x As Variant, _
           Optional ByVal n As Variant) As Variant
            
            Dim d$, f$, p%, q%, u$, v$, w$
    
            If VBA.InStr(x, ".") <> 0& Then
                d = VBA.Split(CStr(x), Application.International(xlDecimalSeparator))(0&)
                f = VBA.Split(CStr(x), Application.International(xlDecimalSeparator))(1&)
                
                u = VBA.Mid(f, 1&, n)
                v = VBA.Mid(f, 1&, n + 1&)
                Debug.Print u, v
                
                If VBA.Right(v, 1&) = "0" Then
                    Rounding = d & "." & u
                    Exit Function
                    
                ElseIf VBA.Right(v, 1&) = "1" Then
                    If VBA.Mid(u, n, 1&) = "0" Then
                        Mid(u, n, 1&) = "1"
                    ElseIf VBA.Mid(u, n, 1&) = "1" Then
                        Mid(u, n, 1&) = "0"
                        If Mid(u, n - 1&, 1&) = "0" Then
                            Mid(u, n - 1&, 1&) = "1"
                        Else
                        End If
                    End If
    
                    Rounding = d & "." & u
                    Exit Function
                End If
            Else
            End If
    
            Rounding = CStr(x)
            Exit Function
    
        End If
    
    End Function
    Last edited by Juggler_IN; Jul 5th, 2021 at 05:59 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Rounding of binary fraction strings

    Not sure what language you are using there. Looks like a bad mixture of VB and VB.Net

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    33

    Re: Rounding of binary fraction strings

    I am using just Excel VBA.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Rounding of binary fraction strings

    I'm not sure if I understood your third rule correctly, and didn't look at the code to try to figure it out, but you may be correct.
    Essentially you want to divide the number conceptually at the point you're rounding so 0.11100 becomes 11.100. Since the value to the right of the binary point is = .5 decimal on the right you have to decide if you want to round up or down. That decision is based on the left side value. In your case you're saying if the value on the left is odd, then round up, else if it is even then round down, i.e. 0.11100 will round up and 0.10100 (10.100) will round down.

    So, if the remaining bits after the rounding point are 1 followed by zeros and you have to choose then you look at the least significant bit of your rounded value position, i.e. the second bit after the decimal point in your example, and round up if it is 1 (odd) or round down if it is 0 (even).
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    33

    Re: Rounding of binary fraction strings

    I will add another example,

    For instance, with d = 3/5, the binary is 0.100110011001100110011001100110011001100110011001100110011001 with repeating bits 1001.
    - To 5 places (next bit, 5+1 th, is 0, no add 1 and no carry) 0.100110011001100110011001100110011001100110011001100110011001... and the output is ~ 0.10011.
    - To 4 places (next bit, 4+1 th, is 1, no add 1 with carry one, flips the 4th place up, as it was already 1, carries to 3rd place) ~ 0.1010.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Rounding of binary fraction strings

    But that example is obvious, and not related to rule 3.
    When you round to five places, the fractional value after the fifth place is less that .5 so you round down.
    When you round to four places, the fractional value after the fourth place is greater than .5 so you round up.

    The only questionable rounding issue is when the fractional value after the rounding point is exactly .5, which way do you round.
    I think "Bankers" rounding in that case, decides which way to round based on if the "integer" portion of the value is even or odd, so theoretically, half the time on average it will round up and half the time it will round down. That is what I was suggesting for rule three. Look at your rounding place bit value and round up or down based on that bit, but that is only necessary in the probably rare case where all your trailing bits are zeros.
    Last edited by passel; Jul 6th, 2021 at 10:20 AM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width