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. The Lounge
  3. In C#: double d = 0.4 - 1.2; What is d?

In C#: double d = 0.4 - 1.2; What is d?

Scheduled Pinned Locked Moved The Lounge
questioncsharp
51 Posts 24 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.
  • I Ivor S Sargoytchev

    Well? What is d equal to? Ivor S. Sargoytchev Dundas Software

    M Offline
    M Offline
    Maxwell Chen
    wrote on last edited by
    #27

    In Visual C++ 6.0, d = -0.80000004172325 ;P BuggyMax

    B 1 Reply Last reply
    0
    • I Ivor S Sargoytchev

      So Epsilon should be some value that you pick up, not Double.Epsilon as some people suggested, right? P.S. Chris, I know you are not supposed to compare 2 doubles with equality test. I was making a point that the result was not -0.8 as a response to leppie who wrote: "Thru a microscope, everything looks big. Believe me that is -0.8. Try Console.Write()." So, I accept your apology.:) Ivor S. Sargoytchev Dundas Software

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #28

      Ivor S. Sargoytchev wrote: So Epsilon should be some value that you pick up, not Double.Epsilon as some people suggested, right? Nope, check your local MSDN documentation on the Double.Epsilon Field. .NET Documentation wrote: For example, the C# expression, (double)1/3 == (double)0.33333, does not compare equal because the division operation on the left-hand side has maximum precision while the constant on the right-hand side is only precise to the visible digits. Instead, determine if the two sides of a comparison are close enough to equal for your purposes by comparing whether the absolute value of the difference between the left and right-hand sides is less than Epsilon. -Nick Parker

      I 1 Reply Last reply
      0
      • I Ivor S Sargoytchev

        Well? What is d equal to? Ivor S. Sargoytchev Dundas Software

        S Offline
        S Offline
        Stephane Rodriguez
        wrote on last edited by
        #29

        Ok, so MS has still problem defining a simple and safe addition/substract operation on floating points. Nice. Now that's high-tech. Where is the Accessories / calculator SDK when I need it ? :laugh: Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

        C E 2 Replies Last reply
        0
        • N Nemanja Trifunovic

          Hehehe, I ran into this problem 15 years ago while writing a Fortran program.

          O Offline
          O Offline
          Olli
          wrote on last edited by
          #30

          Nemanja Trifunovic wrote: Hehehe, I ran into this problem 15 years ago while writing a Fortran program. AAAAAAAAAAAAAAAAARRRRRRRRRRRRRRRGHHHHHHHHH You made my old trauma come back.... Fortrandoubleintc++verystrangehelpfirstclass :wtf::wtf::wtf::wtf::omg::omg:

          Olli "Ooooooh, they have the internet on computers now!"
          Homer Simpson
          :beer: + :java: = NULL :=> X|

          1 Reply Last reply
          0
          • M Maxwell Chen

            In Visual C++ 6.0, d = -0.80000004172325 ;P BuggyMax

            B Offline
            B Offline
            Brit
            wrote on last edited by
            #31

            BuggyMax wrote: In Visual C++ 6.0, d = -0.80000004172325 How'd you come up with that answer? Because I came up with a different number with Visual Studio 6.0: -0.79999999999999993 BTW, you'd better not say that you did "d += 0.8" and then looked at the value in the debugger. ------------------------------------------ The ousted but stubbornly non-dead leader reportedly released an audiotape this weekend, ending by calling on Iraqis to, quote, "resist the occupation in any way you can, from writing on walls, to boycotting, to demonstrating and taking up arms." adding, "you know, pretty much anything I used to kill you for."

            M 1 Reply Last reply
            0
            • B Brit

              BuggyMax wrote: In Visual C++ 6.0, d = -0.80000004172325 How'd you come up with that answer? Because I came up with a different number with Visual Studio 6.0: -0.79999999999999993 BTW, you'd better not say that you did "d += 0.8" and then looked at the value in the debugger. ------------------------------------------ The ousted but stubbornly non-dead leader reportedly released an audiotape this weekend, ending by calling on Iraqis to, quote, "resist the occupation in any way you can, from writing on walls, to boycotting, to demonstrating and taking up arms." adding, "you know, pretty much anything I used to kill you for."

              M Offline
              M Offline
              Maxwell Chen
              wrote on last edited by
              #32

              Ooooops! I used this: double d = 0.4F - 1.2F; And the result is wrong (d = -0.800000041723251) because casting float to double. This time I use this with VC++ 6: double d = 0.4 - 1.2; And I got the output message for printf("d = %.15f\n", d); is d = -0.800000000000000 But I still got negative result ("No") in the comparison:

              if(d == -0.8)
              std::cout << "Yes\n";
              else
              std::cout << "No\n";

              BuggyMax

              B 1 Reply Last reply
              0
              • I Ivor S Sargoytchev

                I guess I should use decimals. Ivor S. Sargoytchev Dundas Software

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

                No, you should be a programmer and write a function called "AlmostEqual" which will compare two floating point numbers asstrings out to a programmer-specified number of positions to the right of the decimal point. I had to do that when I was programming in Pascal over 15 years ago. ------- signature starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- signature ends

                R 1 Reply Last reply
                0
                • S Stephane Rodriguez

                  Ok, so MS has still problem defining a simple and safe addition/substract operation on floating points. Nice. Now that's high-tech. Where is the Accessories / calculator SDK when I need it ? :laugh: Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

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

                  Thanks Stephane. I just knew this had to be an MS screw up all along. :rolleyes: Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. Oh and for those that ask programming questions in the lounge. Seek the truth here[^].

                  1 Reply Last reply
                  0
                  • N Nick Parker

                    Ivor S. Sargoytchev wrote: So Epsilon should be some value that you pick up, not Double.Epsilon as some people suggested, right? Nope, check your local MSDN documentation on the Double.Epsilon Field. .NET Documentation wrote: For example, the C# expression, (double)1/3 == (double)0.33333, does not compare equal because the division operation on the left-hand side has maximum precision while the constant on the right-hand side is only precise to the visible digits. Instead, determine if the two sides of a comparison are close enough to equal for your purposes by comparing whether the absolute value of the difference between the left and right-hand sides is less than Epsilon. -Nick Parker

                    I Offline
                    I Offline
                    Ivor S Sargoytchev
                    wrote on last edited by
                    #35

                    Nick, This cannot be true since the absolute value of ((0.4 - 1.2) + 0.8) is 0.00000000000000011102230246251565. The value of Double.Epsilon is 4.94065645841247E-324, which is much much smaller.

                    1 Reply Last reply
                    0
                    • S Stephane Rodriguez

                      Ok, so MS has still problem defining a simple and safe addition/substract operation on floating points. Nice. Now that's high-tech. Where is the Accessories / calculator SDK when I need it ? :laugh: Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

                      E Offline
                      E Offline
                      Eco Jones
                      wrote on last edited by
                      #36

                      Stephane Rodriguez. wrote: MS has still problem defining Yeah, and if they'd ignored the IEEE standard then chances are good you would be complaining that they're not "standards compliant." :rolleyes: Eco

                      S 1 Reply Last reply
                      0
                      • E Eco Jones

                        Stephane Rodriguez. wrote: MS has still problem defining Yeah, and if they'd ignored the IEEE standard then chances are good you would be complaining that they're not "standards compliant." :rolleyes: Eco

                        S Offline
                        S Offline
                        Stephane Rodriguez
                        wrote on last edited by
                        #37

                        If MS had ignored IEEE decades ago, then they would still be playing with their dicks. MS products are high priced. So they at least have to deliver. This limitation is beyond my imagination of most stupid case I could think of. Now, I should compare this behaviour to the JVM just to check if the issue was already there back a few years ago (just before they rebranded the JVM into the CLR and sold it like a new development platform). Grrrrr!!!! Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

                        E E 2 Replies Last reply
                        0
                        • S Stephane Rodriguez

                          If MS had ignored IEEE decades ago, then they would still be playing with their dicks. MS products are high priced. So they at least have to deliver. This limitation is beyond my imagination of most stupid case I could think of. Now, I should compare this behaviour to the JVM just to check if the issue was already there back a few years ago (just before they rebranded the JVM into the CLR and sold it like a new development platform). Grrrrr!!!! Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

                          E Offline
                          E Offline
                          Eddie Velasquez
                          wrote on last edited by
                          #38

                          Stephane Rodriguez. wrote: (just before they rebranded the JVM into the CLR and sold it like a new development platform What a pile of steamy, stinkin' sh... rubish! This is nothing more than your typical anti-microsoft rant, come on give it break! Provide some useful feedback or abstain for crying out loud! :mad:


                          The nice thing about C++ is that only your friends can handle your private parts.

                          S 1 Reply Last reply
                          0
                          • S Stephane Rodriguez

                            If MS had ignored IEEE decades ago, then they would still be playing with their dicks. MS products are high priced. So they at least have to deliver. This limitation is beyond my imagination of most stupid case I could think of. Now, I should compare this behaviour to the JVM just to check if the issue was already there back a few years ago (just before they rebranded the JVM into the CLR and sold it like a new development platform). Grrrrr!!!! Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

                            E Offline
                            E Offline
                            Eco Jones
                            wrote on last edited by
                            #39

                            Stephane Rodriguez. wrote: blah, blah, blah, MS EVIL, blah, blah Here's an e-quarter () - call someone who cares. Then you can use the change to buy a notebook where you record the amount of time you spend whining and complaining about Microsoft and the amount of time you spend actually trying to improve the situation and do a comparitive study and post it on slashdot and make a BIGGER frigging pretentious image in your signature and form anti-MS demonstrations and die happy knowing that you haven't really done a single worthwhile thing in your whole miserable existence other than slightly annoy a whole bunch of people. Worst-case scenario, of course. Eco

                            S 1 Reply Last reply
                            0
                            • realJSOPR realJSOP

                              No, you should be a programmer and write a function called "AlmostEqual" which will compare two floating point numbers asstrings out to a programmer-specified number of positions to the right of the decimal point. I had to do that when I was programming in Pascal over 15 years ago. ------- signature starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- signature ends

                              R Offline
                              R Offline
                              Roger Wright
                              wrote on last edited by
                              #40

                              John Simmons / outlaw programmer wrote: AlmostEqual I used a similar approach in Pascal, but the function took floating point inputs along with an acceptable error value, returning false if the difference between the two input values was greater than the error value. Handy tool!

                              "Welcome to Arizona!
                              Drive Nice - We're Armed..."
                              - Proposed Sign at CA/AZ Border

                              1 Reply Last reply
                              0
                              • M Maxwell Chen

                                Ooooops! I used this: double d = 0.4F - 1.2F; And the result is wrong (d = -0.800000041723251) because casting float to double. This time I use this with VC++ 6: double d = 0.4 - 1.2; And I got the output message for printf("d = %.15f\n", d); is d = -0.800000000000000 But I still got negative result ("No") in the comparison:

                                if(d == -0.8)
                                std::cout << "Yes\n";
                                else
                                std::cout << "No\n";

                                BuggyMax

                                B Offline
                                B Offline
                                Brit
                                wrote on last edited by
                                #41

                                Maybe you need more decimal places. What I did was: char buffer[100]; sprintf( buffer, "%20.20f", d ); ------------------------------------------ The ousted but stubbornly non-dead leader reportedly released an audiotape this weekend, ending by calling on Iraqis to, quote, "resist the occupation in any way you can, from writing on walls, to boycotting, to demonstrating and taking up arms." adding, "you know, pretty much anything I used to kill you for."

                                M 1 Reply Last reply
                                0
                                • E Eddie Velasquez

                                  Stephane Rodriguez. wrote: (just before they rebranded the JVM into the CLR and sold it like a new development platform What a pile of steamy, stinkin' sh... rubish! This is nothing more than your typical anti-microsoft rant, come on give it break! Provide some useful feedback or abstain for crying out loud! :mad:


                                  The nice thing about C++ is that only your friends can handle your private parts.

                                  S Offline
                                  S Offline
                                  Stephane Rodriguez
                                  wrote on last edited by
                                  #42

                                  You have to prove I am wrong. Watch rotor, compare it to the Sun JVM. Amazingly similar isn't it? Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

                                  E 1 Reply Last reply
                                  0
                                  • E Eco Jones

                                    Stephane Rodriguez. wrote: blah, blah, blah, MS EVIL, blah, blah Here's an e-quarter () - call someone who cares. Then you can use the change to buy a notebook where you record the amount of time you spend whining and complaining about Microsoft and the amount of time you spend actually trying to improve the situation and do a comparitive study and post it on slashdot and make a BIGGER frigging pretentious image in your signature and form anti-MS demonstrations and die happy knowing that you haven't really done a single worthwhile thing in your whole miserable existence other than slightly annoy a whole bunch of people. Worst-case scenario, of course. Eco

                                    S Offline
                                    S Offline
                                    Stephane Rodriguez
                                    wrote on last edited by
                                    #43

                                    Eco Jones wrote: miserable existence ??? I have a full-time job, and I have a full-time life. I also enjoy programming on personal projects using a bunch of technologies and languages. Currently I am being caught in text-rendering and image effects à la Longhorn. Surprised? Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

                                    E 1 Reply Last reply
                                    0
                                    • S Stephane Rodriguez

                                      You have to prove I am wrong. Watch rotor, compare it to the Sun JVM. Amazingly similar isn't it? Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

                                      E Offline
                                      E Offline
                                      Eddie Velasquez
                                      wrote on last edited by
                                      #44

                                      I've never checked the source code for Sun's JVM and have barely investigated Rotor. And... Rotor is NOT the same as .NET's CLR.


                                      The nice thing about C++ is that only your friends can handle your private parts.

                                      S 1 Reply Last reply
                                      0
                                      • S Stephane Rodriguez

                                        Eco Jones wrote: miserable existence ??? I have a full-time job, and I have a full-time life. I also enjoy programming on personal projects using a bunch of technologies and languages. Currently I am being caught in text-rendering and image effects à la Longhorn. Surprised? Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

                                        E Offline
                                        E Offline
                                        Eco Jones
                                        wrote on last edited by
                                        #45

                                        Stephane Rodriguez. wrote: Surprised? Yes, with all this talk of programming, I thought you were a rodeo clown. http://dictionary.reference.com/search?q=sarcasm[^] Reading your resume was really boring. If you really want to defend your existence to me or someone else who will almost certainly pay attention and truly cares about what you have to say, next time pass on some hilarious anecdotes of your life and maybe end it off with some kind of inspirational song. http://dictionary.reference.com/search?q=sarcasm[^]

                                        S 1 Reply Last reply
                                        0
                                        • E Eddie Velasquez

                                          I've never checked the source code for Sun's JVM and have barely investigated Rotor. And... Rotor is NOT the same as .NET's CLR.


                                          The nice thing about C++ is that only your friends can handle your private parts.

                                          S Offline
                                          S Offline
                                          Stephane Rodriguez
                                          wrote on last edited by
                                          #46

                                          Yes rotor and the the .NET CLR have not the same baseline. But it's 99% the same. In fact rotor is only the JVM, while the actual CLR also includes winforms, asp.net, ... You've got to read Chris Brumme's weblog (CLR guy) and the amount of work his team is doing to have Rotor face to face with the CLR releases. Taking advantage of InternetExplorer to steal user's name and password. Taking advantage of InternetExplorer to steal user's clipboard.

                                          E 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