CONFUSED!!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan

    CONFUSED!!

    Have a look at the follwoing VB-code. The first MsgBox shows the number 2
    while the second one 6. WHY? How come 2.5 is rounded off to 2 and 5.5 is
    rounded off to 6??

    Dim i As Integer = 5 / 2

    MsgBox(i)

    Dim j As Integer = 11 / 2

    MsgBox(j)


  • Jeff Brown

    #2
    Re: CONFUSED!!

    Change to integer division if possible if not change from integer type and
    they try rounding, i ran in to the same problem .. it baffled me till i
    solved it using int division only and gave up.

    Dim i As Integer = 5 \2

    as to your answer of why the rounding difference .. i dunno?

    "Stefan" <sthr5805@stude nt.uu.se> wrote in message
    news:%23FW3ZZXd DHA.1832@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Have a look at the follwoing VB-code. The first MsgBox shows the number 2
    > while the second one 6. WHY? How come 2.5 is rounded off to 2 and 5.5 is
    > rounded off to 6??
    >
    > Dim i As Integer = 5 / 2
    >
    > MsgBox(i)
    >
    > Dim j As Integer = 11 / 2
    >
    > MsgBox(j)
    >
    >[/color]


    Comment

    • Cowboy \(Gregory A Beamer\)

      #3
      Re: CONFUSED!!

      I forget the name of this type of rounding, but odd and even numbers are
      rounded differently. Try 7/2 and 9/2, as well and see the similarities in
      the way it is handled.

      I will see if I can dig up the article on rounding and post an URL.

      --
      Gregory A. Beamer
      MPV; MCP: +I, SE, SD, DBA

      *************** *************** *************** *************** **********
      Think outside the box!
      *************** *************** *************** *************** **********
      "Stefan" <sthr5805@stude nt.uu.se> wrote in message
      news:%23FW3ZZXd DHA.1832@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > Have a look at the follwoing VB-code. The first MsgBox shows the number 2
      > while the second one 6. WHY? How come 2.5 is rounded off to 2 and 5.5 is
      > rounded off to 6??
      >
      > Dim i As Integer = 5 / 2
      >
      > MsgBox(i)
      >
      > Dim j As Integer = 11 / 2
      >
      > MsgBox(j)
      >
      >[/color]


      Comment

      • Armin Zingler

        #4
        Re: CONFUSED!!

        "Stefan" <sthr5805@stude nt.uu.se> schrieb[color=blue]
        > Have a look at the follwoing VB-code. The first MsgBox shows the
        > number 2 while the second one 6. WHY? How come 2.5 is rounded off to
        > 2 and 5.5 is rounded off to 6??
        >
        > Dim i As Integer = 5 / 2
        >
        > MsgBox(i)
        >
        > Dim j As Integer = 11 / 2
        >
        > MsgBox(j)[/color]

        Mathematical rounding (right words?) means round to the next _even_ number.


        --
        Armin

        Comment

        • Cor

          #5
          Re: CONFUSED!!

          Stefan,
          I was confused too, it is written at "type conversions functions"
          When the fractional part is exactly 0.5, CInt and CLng always round it to
          the nearest even number. For example, 0.5 rounds to 0 and 1.5 rounds to 2.
          CInt and CLng differ from the Fix and Int functions, which truncate, rather
          than round, the fractional part of a number. Also, Fix and Int always return
          a value of the same type as is passed in.



          Cor


          Comment

          • Fergus Cooney

            #6
            Re: CONFUSED!!

            Hi Stefan,

            You have a good point there. I did a loop:

            Dim I As Integer
            For I = 1 To 15
            Dim Idiv2 as Integer = I / 2
            Debug.WriteLine (I & " / 2 = " & Idiv2)
            Next

            This produced (extra spacing added):

            1 / 2 = 0
            2 / 2 = 1
            3 / 2 = 2
            4 / 2 = 2
            5 / 2 = 2
            6 / 2 = 3
            7 / 2 = 4
            8 / 2 = 4
            9 / 2 = 4
            10 / 2 = 5
            11 / 2 = 6
            12 / 2 = 6
            13 / 2 = 6
            14 / 2 = 7
            15 / 2 = 8

            Something is clearly fishy but I know not what.

            Regards,
            Fergus


            Comment

            • Charles Law

              #7
              Re: CONFUSED!!

              Hi Stefan

              It's called Accounting Rounding. The idea is that by rounding to the nearest
              even number, rounding errors all but cancel out. As the name suggests, used
              particularly in accountancy.

              HTH

              Charles


              "Stefan" <sthr5805@stude nt.uu.se> wrote in message
              news:%23FW3ZZXd DHA.1832@TK2MSF TNGP09.phx.gbl. ..[color=blue]
              > Have a look at the follwoing VB-code. The first MsgBox shows the number 2
              > while the second one 6. WHY? How come 2.5 is rounded off to 2 and 5.5 is
              > rounded off to 6??
              >
              > Dim i As Integer = 5 / 2
              >
              > MsgBox(i)
              >
              > Dim j As Integer = 11 / 2
              >
              > MsgBox(j)
              >
              >[/color]


              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: CONFUSED!!

                Hello,

                "Fergus Cooney" <filter-1@tesco.net> schrieb:[color=blue]
                > You have a good point there. I did a loop:
                >
                > Dim I As Integer
                > For I = 1 To 15
                > Dim Idiv2 as Integer = I / 2
                > Debug.WriteLine (I & " / 2 = " & Idiv2)
                > Next
                >
                > This produced (extra spacing added):
                >
                > 1 / 2 = 0
                > 2 / 2 = 1
                > 3 / 2 = 2
                > 4 / 2 = 2
                > 5 / 2 = 2
                > 6 / 2 = 3
                > 7 / 2 = 4
                > 8 / 2 = 4
                > 9 / 2 = 4
                > 10 / 2 = 5
                > 11 / 2 = 6
                > 12 / 2 = 6
                > 13 / 2 = 6
                > 14 / 2 = 7
                > 15 / 2 = 8
                >
                > Something is clearly fishy but I know not what.[/color]

                Seems to be the "round-to-even-rule".

                --
                Herfried K. Wagner
                MVP · VB Classic, VB .NET
                Die Website von H. Wagner zu .NET, Visual Basic .NET und Classic Visual Basic.



                Comment

                • Fergus Cooney

                  #9
                  Re: CONFUSED!!

                  Hi Stefan,

                  Herfried tells me it's called the "round-to-even" rule. And now I
                  understand Charles' post.

                  I found the following explanation on the Net.

                  =============== =============== =====
                  The result of any arithmetic operation involving floating-point quantities
                  is rounded to the nearest representable floating-point number (or to ±? if out
                  of range). In case of ties, where the unrounded result is exactly halfway
                  between two floating-point numbers, one chooses the one that has a last binary
                  digit of 0 (the rule of round to even.) The only exception to this rule is
                  that conversions of floating-point to integer types, using a cast such as
                  (int) x, always truncate-that is, round to the number nearest to 0, throwing
                  the fractional part away.

                  The justifications for the round-to-even rule are subtle. In computations
                  involving many floating-point operations, it can help avoid biasing the
                  arithmetic error in any particular direction. It also has the very interesting
                  property of preventing drift in certain computations. Suppose, for example,
                  that a certain loop has the effect of computing

                  x = (x + y) - y;

                  on each of many iterations (you wouldn't do this explicitly, of course,
                  but it may happen to one of your variables for certain particular values of
                  the input data). The round-to-even rule guarantees that the value of x here
                  will change at most once, and then drift no further.
                  =============== =============== =====

                  I don't like it. The idea that CInt (3 / 2) = CInt (5 / 2) ?? Yuck!!

                  Regards,
                  Fergus


                  Comment

                  • Herfried K. Wagner [MVP]

                    #10
                    Re: CONFUSED!!

                    Hello,

                    "Fergus Cooney" <filter-1@tesco.net> schrieb:[color=blue]
                    > I don't like it. The idea that CInt (3 / 2) = CInt (5 / 2) ?? Yuck!![/color]

                    I don't like it too, because it doesn't work the way my brain is
                    thinking.

                    --
                    Herfried K. Wagner
                    MVP · VB Classic, VB .NET
                    Die Website von H. Wagner zu .NET, Visual Basic .NET und Classic Visual Basic.



                    Comment

                    • Armin Zingler

                      #11
                      Re: CONFUSED!!

                      "Herfried K. Wagner [MVP]" <hirf.nosp@m.ac tivevb.de> schrieb[color=blue]
                      > Hello,
                      >
                      > "Fergus Cooney" <filter-1@tesco.net> schrieb:[color=green]
                      > > I don't like it. The idea that CInt (3 / 2) = CInt (5 / 2) ??
                      > > Yuck!![/color]
                      >
                      > I don't like it too, because it doesn't work the way my brain is
                      > thinking.[/color]


                      If it doesn't work like your brain, it probably works correctly.

                      SCNR ;-))))


                      --
                      Armin

                      Comment

                      • Stefan

                        #12
                        Re: CONFUSED!!

                        Thanks for all your replies. I was really frustrated yesterday but
                        apparently many of you had experienced this problem...

                        Is there a function that rounds 2.5 off to 3? Or, is there a simple solution
                        to my problem?

                        "Armin Zingler" <az.nospam@free net.de> wrote in message
                        news:%23GfAXwZd DHA.568@TK2MSFT NGP11.phx.gbl.. .[color=blue]
                        > "Herfried K. Wagner [MVP]" <hirf.nosp@m.ac tivevb.de> schrieb[color=green]
                        > > Hello,
                        > >
                        > > "Fergus Cooney" <filter-1@tesco.net> schrieb:[color=darkred]
                        > > > I don't like it. The idea that CInt (3 / 2) = CInt (5 / 2) ??
                        > > > Yuck!![/color]
                        > >
                        > > I don't like it too, because it doesn't work the way my brain is
                        > > thinking.[/color]
                        >
                        >
                        > If it doesn't work like your brain, it probably works correctly.
                        >
                        > SCNR ;-))))
                        >
                        >
                        > --
                        > Armin
                        >[/color]


                        Comment

                        • Fergus Cooney

                          #13
                          Re: CONFUSED!!

                          Hi Stefan,

                          D = 2.5
                          I = Int (D + 0.5) 'Rounds up if x.5 or above.

                          Regards,
                          Fergus


                          Comment

                          • Armin Zingler

                            #14
                            Re: CONFUSED!!

                            "Fergus Cooney" <filter-1@tesco.net> schrieb[color=blue]
                            > Hi Stefan,
                            >
                            > D = 2.5
                            > I = Int (D + 0.5) 'Rounds up if x.5 or above.[/color]

                            ....and as i is declared as Integer and Option Strict is used:

                            I = CInt(Int (D + 0.5))

                            ;-)

                            --
                            Armin

                            Comment

                            • Herfried K. Wagner [MVP]

                              #15
                              Re: CONFUSED!!

                              Hello,

                              "Fergus Cooney" <filter-1@tesco.net> schrieb:[color=blue]
                              > D = 2.5
                              > I = Int (D + 0.5) 'Rounds up if x.5 or above.[/color]

                              \\\
                              Dim d As Double
                              Dim i As Integer
                              d = -2.5
                              i = CInt(Fix(d + Math.Sign(d) * 0.5))
                              MsgBox(i)
                              ///

                              ;-)

                              --
                              Herfried K. Wagner
                              MVP · VB Classic, VB .NET
                              Die Website von H. Wagner zu .NET, Visual Basic .NET und Classic Visual Basic.



                              Comment

                              Working...