Why does this decimal division not give an exact result?

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

    Why does this decimal division not give an exact result?

    Decimal a = 42.485M;
    Decimal b = 48M;
    Decimal c = a / b;
    Console.WriteLi ne(c.ToString() );

    gives the result 0.8851041666666 666666666666667

    The correct, exact answer is .885.

    I know that floating point types cannot represent some values
    precisely, but the decimal type (which is a base 10 FP type) CAN
    represent these three values (the dividend, divisor, and quotient)
    exactly.

    Furthermore, doc states that "The result of an operation on values of
    type decimal is that which would result from calculating an exact
    result (preserving scale, as defined for each operator) and then
    rounding to fit the representation. "

    Could someone explain this unexpected result?



  • John Heitmuller.

    #2
    Re: Why does this decimal division not give an exact result?

    Hmmm... I just ran this on Excel and got a result of
    0.8851041666666 67. So, I calculated it by hand and got the same
    result as Excel. Am I not understanding the setup of you problem? I
    don't think the .885 is the propper result.

    John

    Comment

    • Jeff Johnson

      #3
      Re: Why does this decimal division not give an exact result?

      "Elroy Flynn" <leotohill@gmai l.comwrote in message
      news:2edcadcc-7e3b-4301-81eb-7b209c87923f@t4 2g2000hsg.googl egroups.com...
      Decimal a = 42.485M;
      Decimal b = 48M;
      Decimal c = a / b;
      Console.WriteLi ne(c.ToString() );
      >
      gives the result 0.8851041666666 666666666666667
      >
      The correct, exact answer is .885.
      Methinks you were using the numeric keypad and accidentally hit 5 when you
      hit 8, because 42.48 / 48 = .885, not 42.485.


      Comment

      • Peter Duniho

        #4
        Re: Why does this decimal division not give an exact result?

        On Fri, 17 Oct 2008 11:15:40 -0700, Elroy Flynn <leotohill@gmai l.com>
        wrote:
        Decimal a = 42.485M;
        Decimal b = 48M;
        Decimal c = a / b;
        Console.WriteLi ne(c.ToString() );
        >
        gives the result 0.8851041666666 666666666666667
        >
        The correct, exact answer is .885.
        >
        I know that floating point types cannot represent some values
        precisely, but the decimal type (which is a base 10 FP type) CAN
        represent these three values (the dividend, divisor, and quotient)
        exactly.
        Why do you say that?
        Furthermore, doc states that "The result of an operation on values of
        type decimal is that which would result from calculating an exact
        result (preserving scale, as defined for each operator) and then
        rounding to fit the representation. "
        >
        Could someone explain this unexpected result?
        Look at this code:

        decimal a = 2;
        decimal b = 3;
        decimal c = a / b;

        Console.WriteLi ne(c.ToString() );

        What should that print out?

        Pete

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Why does this decimal division not give an exact result?

          Elroy Flynn <leotohill@gmai l.comwrote:
          Decimal a = 42.485M;
          Decimal b = 48M;
          Decimal c = a / b;
          Console.WriteLi ne(c.ToString() );
          >
          gives the result 0.8851041666666 666666666666667
          >
          The correct, exact answer is .885.
          It's really not. 0.885 * 48 = 42.48.
          I know that floating point types cannot represent some values
          precisely, but the decimal type (which is a base 10 FP type) CAN
          represent these three values (the dividend, divisor, and quotient)
          exactly.
          >
          Furthermore, doc states that "The result of an operation on values of
          type decimal is that which would result from calculating an exact
          result (preserving scale, as defined for each operator) and then
          rounding to fit the representation. "
          >
          Could someone explain this unexpected result?
          Your expectations are flawed :) Make a = 42.48M and you'll get your
          expected result.

          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com

          Comment

          • Elroy Flynn

            #6
            Re: Why does this decimal division not give an exact result?

            On Oct 17, 3:21 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
            Elroy Flynn <leotoh...@gmai l.comwrote:
            Decimal a = 42.485M;
            Decimal b = 48M;
            Decimal c = a / b;
            Console.WriteLi ne(c.ToString() );
            >
            gives the result 0.8851041666666 666666666666667
            >
            The correct, exact answer is .885.
            >
            It's really not. 0.885 * 48 = 42.48.
            >
            I know that floating point types cannot represent some values
            precisely, but the decimal type (which is a base 10 FP type) CAN
            represent these three values (the dividend, divisor, and quotient)
            exactly.
            >
            Furthermore, doc states that "The result of an operation on values of
            type decimal is that which would result from calculating an exact
            result (preserving scale, as defined for each operator) and then
            rounding to fit the representation. "
            >
            Could someone explain this unexpected result?
            >
            Your expectations are flawed :) Make a = 42.48M and you'll get your
            expected result.
            >
            --
            Jon Skeet - <sk...@pobox.co m>
            Web site:http://www.pobox.com/~skeet 
            Blog:http://www.msmvps.com/jon.skeet
            C# in Depth:http://csharpindepth.com
            where's the "delete from web" command?

            Comment

            • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

              #7
              Re: Why does this decimal division not give an exact result?

              Michael B. Trausch wrote:
              On Fri, 17 Oct 2008 13:56:44 -0700 (PDT)
              Elroy Flynn <leotohill@gmai l.comwrote:
              >where's the "delete from web" command?
              >
              This is a newsgroup. You cannot delete your post once you have sent
              it, because most servers on Usenet do not honor the commands that used
              to be used for that since they've been abused in the past.
              >
              As far as Google Groups, you'll have to go through the same process
              that anyone that wants to have their messages pulled from their
              archives has to do: Find the form if they still have it and request
              that the message(s) be removed by their Message ID. You will also have
              to prove that you own the email address associated with the Message ID.
              I don't think it was meant that literally.

              Arne

              Comment

              • Peter Morris

                #8
                Re: Why does this decimal division not give an exact result?

                Are you posting in HTML format or something? To me your posts are empty but
                have two attachments...

                01: ATT0096.txt
                02: signature.asc



                --
                Pete
                ====



                Comment

                • Family Tree Mike

                  #9
                  [OT] Strange Postings

                  As Peter Morris indicated with your other post, something strange is
                  happening. You actual text is showing as an attachment to a blank post.
                  The post on this part of the thread that Arne V. responded to did not come
                  through at all. I'm using Windows Mail to view the group.

                  Both appear fine at the microsoft discussion groups page though, but it's
                  strange...

                  "Michael B. Trausch" <mike@trausch.u swrote in message
                  news:2008101723 2431.338ecb01@z est...

                  Comment

                  • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

                    #10
                    Re: [OT] Strange Postings

                    Family Tree Mike wrote:
                    As Peter Morris indicated with your other post, something strange is
                    happening. You actual text is showing as an attachment to a blank post.
                    The post on this part of the thread that Arne V. responded to did not
                    come through at all. I'm using Windows Mail to view the group.
                    >
                    Both appear fine at the microsoft discussion groups page though, but
                    it's strange...
                    I use Thunderbird to view the newsgroups, and there the posts show up
                    just as usual.

                    --
                    Göran Andersson
                    _____
                    Göran Anderssons privata hemsida.

                    Comment

                    Working...