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. summation

summation

Scheduled Pinned Locked Moved C#
csharpjavascriptphpquestion
10 Posts 8 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.
  • M Offline
    M Offline
    Monin D
    wrote on last edited by
    #1

    float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.

    D D E realJSOPR L 5 Replies Last reply
    0
    • M Monin D

      float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #2

      What every computer scientist should know about floating point arithmatic[^]

      Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

      1 Reply Last reply
      0
      • M Monin D

        float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        in addition to Dan's link, did you know you can declare a float as a literal by suffixing with an 'f' and save the expense of casting from a double (or int).

        float a = 93.3f;

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        1 Reply Last reply
        0
        • M Monin D

          float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.

          E Offline
          E Offline
          ezazazel
          wrote on last edited by
          #4

          I try to make it short: If you compute float numbers, the computer uses "MachineNumbers" which are to a certain level adequate. So the computer has internal numbers which are taken when given a float to compute. e.g the Machinenumbers are 0.1, 0.2, 0.3, 0.4, 0.5, 1, 1.2, 1.5, 2, 2.4, 2.5 now you pass the number 1.2 -> computer takes 1.2 because it exists then you take 1.3 -> computer takes again 1.2 because it is the nearest "MachineNumber" you summ them and allthough there would be a "MachineNumber" 2.5 the computer computes 1.2 + 1.2 and you get the nearest "MachineNumber" which is 2.4 That's been very simplified, but I hope that helps

          M 1 Reply Last reply
          0
          • M Monin D

            float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            If you want to do floating point math, I've found that using the decimal type is more precise. Try it this way:

            decimal a = 93.3M;
            decimal b = 2.3M;
            decimal c = a + b;

            You should end up with precisely 95.6. If you need to, you can follow that up by casting the result to a float or double as need be.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            D 1 Reply Last reply
            0
            • realJSOPR realJSOP

              If you want to do floating point math, I've found that using the decimal type is more precise. Try it this way:

              decimal a = 93.3M;
              decimal b = 2.3M;
              decimal c = a + b;

              You should end up with precisely 95.6. If you need to, you can follow that up by casting the result to a float or double as need be.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #6

              The caveat to using the decimal type is that unlike floats it's not implemented in hardware directly and is significantly slower to use as result. This isn't a problem if you're doing light computations, but if you're doing larger amounts of calculation working within the limits of native floating points may be needed.

              Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

              E 1 Reply Last reply
              0
              • D Dan Neely

                The caveat to using the decimal type is that unlike floats it's not implemented in hardware directly and is significantly slower to use as result. This isn't a problem if you're doing light computations, but if you're doing larger amounts of calculation working within the limits of native floating points may be needed.

                Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                To further and agree with your statement, decimal should only be used for financial or monetary values in .NET. At least that is my rule of thumb.

                Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
                Most of this sig is for Google, not ego.

                M 1 Reply Last reply
                0
                • E ezazazel

                  I try to make it short: If you compute float numbers, the computer uses "MachineNumbers" which are to a certain level adequate. So the computer has internal numbers which are taken when given a float to compute. e.g the Machinenumbers are 0.1, 0.2, 0.3, 0.4, 0.5, 1, 1.2, 1.5, 2, 2.4, 2.5 now you pass the number 1.2 -> computer takes 1.2 because it exists then you take 1.3 -> computer takes again 1.2 because it is the nearest "MachineNumber" you summ them and allthough there would be a "MachineNumber" 2.5 the computer computes 1.2 + 1.2 and you get the nearest "MachineNumber" which is 2.4 That's been very simplified, but I hope that helps

                  M Offline
                  M Offline
                  Monin D
                  wrote on last edited by
                  #8

                  Thanks to everyone for answers!

                  1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    To further and agree with your statement, decimal should only be used for financial or monetary values in .NET. At least that is my rule of thumb.

                    Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                    Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
                    Most of this sig is for Google, not ego.

                    M Offline
                    M Offline
                    Mark Churchill
                    wrote on last edited by
                    #9

                    To really jump in on the good vibes agreeance bandwagon, if the 96.1000001 is annoying you, then you really should be using an appropriate format string when you display it to the user.

                    Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                    Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                    1 Reply Last reply
                    0
                    • M Monin D

                      float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Additionally to what the others already said, it's not safe to compare two float values like this:

                      if(float1 == float2)

                      Instead, for floats you should rather use something like this:

                      if((float1 - float2) < delta)

                      where delta can be a small number like 0.001. regards

                      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