Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Easy Arithmetic question

Easy Arithmetic question

Scheduled Pinned Locked Moved C#
question
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    northernlights
    wrote on last edited by
    #1

    double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot

    C C M H 4 Replies Last reply
    0
    • N northernlights

      double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Internal rounding error


      "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

      N 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Internal rounding error


        "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

        N Offline
        N Offline
        northernlights
        wrote on last edited by
        #3

        What does that mean exactly ? Do you mean that .NET double type does rounding error ? No exception at all are thrown from framework. I resolved it using decimal type but I'd like to understand why this rounding error happens with Double :sigh:. Thx N0rthernlights

        1 Reply Last reply
        0
        • N northernlights

          double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #4

          The binary representation of fractional numbers is an approximation. Just as the fraction 1/3 must be approximated when represented in a decimal notation. I would pose a question, though. Since the two numbers you are adding have only 5 and 6 digits of significance, why are you expecting more than that when adding the two together. If you round the result to only be 6 significant digits, you will have the correct answer. The double type can only have 17 digits and once you begin operating on them, the accuracy usually falls down to only about 13 or 14 that are correct. Chris Meech We're more like a hobbiest in a Home Depot drooling at all the shiny power tools, rather than a craftsman that makes the chair to an exacting level of comfort by measuring the customer's butt. Marc Clifton VB is like a toolbox, in the hands of a craftsman, you can end up with some amazing stuff, but without the skills to use it right you end up with Homer Simpson's attempt at building a barbeque or his attempt at a Spice rack. Michael P. Butler

          1 Reply Last reply
          0
          • N northernlights

            double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot

            M Offline
            M Offline
            Manster
            wrote on last edited by
            #5

            I've run into problems with the same issue before. The only way I could fix the rounding error was: double c = a + b; c = Math.Round( c, 2 ); Hope this helps. :)

            1 Reply Last reply
            0
            • N northernlights

              double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot

              H Offline
              H Offline
              HAHAHA_NEXT
              wrote on last edited by
              #6

              As far as i remember the double is accurate for 12 digits after the comma. And as Chris Meech mentionned, the internal represntation of fraction is not the same the one for decimals. Also if you Round your Number to 12 digits of precision (you have 13), you would get exactly 7008.350000000000

              N 1 Reply Last reply
              0
              • H HAHAHA_NEXT

                As far as i remember the double is accurate for 12 digits after the comma. And as Chris Meech mentionned, the internal represntation of fraction is not the same the one for decimals. Also if you Round your Number to 12 digits of precision (you have 13), you would get exactly 7008.350000000000

                N Offline
                N Offline
                northernlights
                wrote on last edited by
                #7

                Thx all for your answers :-O. I now understand that internal fraction representation has some special behaviour. I'll use Decimal type because it's much easier to work with it to calculate money values.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups