double

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • normd@online.knorrassociates.com

    #1

    double

    I'm trying to multiply two doubles and assign the result to a double as well.
    But it seems the result is rounded up.
    x = 907.18474
    y = 2.2046226218487 757
    the result I calculate 1999.9999999999 99902722818
    But this is calculated to be 2000.0.
    I looked up double and it is specified as follows;
    double ±5.0 × 10−324 to ±1.7 × 10308
    Precision: 15-16 digits
    What am I missing here?
  • Scott M.

    #2
    Re: double

    What are you using to display the answer? Have you tried applying a custom
    numerical format to the output field? One that specifies the number of
    decimal places to show?


    "normd@online.k norrassociates. com"
    <normdonlinekno rrassociatescom @discussions.mi crosoft.com> wrote in message
    news:FD644147-494A-4B6F-8C3A-0E793712C989@mi crosoft.com...[color=blue]
    > I'm trying to multiply two doubles and assign the result to a double as
    > well.
    > But it seems the result is rounded up.
    > x = 907.18474
    > y = 2.2046226218487 757
    > the result I calculate 1999.9999999999 99902722818
    > But this is calculated to be 2000.0.
    > I looked up double and it is specified as follows;
    > double ±5.0 × 10?324 to ±1.7 × 10308
    > Precision: 15-16 digits
    > What am I missing here?[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: double

      normd@online.kn orrassociates.c om
      <normdonlinekno rrassociatescom @discussions.mi crosoft.com> wrote:[color=blue]
      > I'm trying to multiply two doubles and assign the result to a double as well.
      > But it seems the result is rounded up.
      > x = 907.18474
      > y = 2.2046226218487 757
      > the result I calculate 1999.9999999999 99902722818
      > But this is calculated to be 2000.0.
      > I looked up double and it is specified as follows;
      > double ?5.0 ? 10?324 to ?1.7 ? 10308
      > Precision: 15-16 digits
      > What am I missing here?[/color]

      See http://www.pobox.com/~skeet/csharp/floatingpoint.html

      You're actually multiplying
      907.18474000000 003343302523717 284202575683593 75
      by
      2.2046226218487 756653985343291 424214839935302 734375

      The result of that is

      1999.9999999999 999450401001164 764179157374400 704388006048267 67859378833
      236405625939369 20166015625

      The two exactly representable numbers closest to that are
      1999.9999999999 997726263245567 679405212402343 75
      and
      2000

      2000 is closer to the actual number than the number slightly lower than
      it, hence your result.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...