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

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

    If it's anything like C++, then d = -0.79999999999999993 ------------------------------------------ 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."

    1 Reply Last reply
    0
    • C Chris Maunder

      The value represented in a floating point variable is only an approximation to a value, not an exact representation. Because of this you cannot assume that something like 2.0 == 4.0 * 0.5 will return true. When comparing floating point values you need to estimate how close is close enough then do a fuzzy comparision (something like ABS(A-B) < Epsilon) cheers, Chris Maunder

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #17

      Chris Maunder wrote: 2.0 == 4.0 * 0.5 Won't that actually be true because all those numbers are powers of 2? :cool: --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER

      J R 2 Replies Last reply
      0
      • T Tim Smith

        Integer Math != IEEE Math Tim Smith I'm going to patent thought. I have yet to see any prior art.

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #18

        That reminds me of the old C= BASIC, there was some number (I'll just use 42 as an example) where you could type:

        PRINT 42

        and the response would be:

        42.0001

        :wtf: --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER

        1 Reply Last reply
        0
        • M Michael Dunn

          Chris Maunder wrote: 2.0 == 4.0 * 0.5 Won't that actually be true because all those numbers are powers of 2? :cool: --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #19

          Yes it should... John

          1 Reply Last reply
          0
          • I Ivor S Sargoytchev

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

            N Offline
            N Offline
            Navin
            wrote on last edited by
            #20

            Ivor S. Sargoytchev wrote: Dundas Software THe only way you're going to get an answer in this crowd, when posting in the Lounge, is if you offer to hook us up with those women from the Summer Sale ads. :-D "When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity." - Albert Einstein

            J 1 Reply Last reply
            0
            • I Ivor S Sargoytchev

              Daniel, This is not true. Double.Epsilon is a very very very small number: Double.Epsilon = 4.94065645841247E-324 But in C# I get 0.4 - 1.2 = -0.79999999999999993. As you can see the deviation from -0.8 is hundreds of times bugger than 4.94065645841247E-324. Ivor S. Sargoytchev Dundas Software

              R Offline
              R Offline
              Rodolfo Lima
              wrote on last edited by
              #21

              I think that what he meant was something like: if(abs(0.4-1.2)-0.8) < Double.Epsilon) which is the proper way to compare floating point numbers, i guess.

              I 1 Reply Last reply
              0
              • N Navin

                Ivor S. Sargoytchev wrote: Dundas Software THe only way you're going to get an answer in this crowd, when posting in the Lounge, is if you offer to hook us up with those women from the Summer Sale ads. :-D "When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity." - Albert Einstein

                J Offline
                J Offline
                John M Drescher
                wrote on last edited by
                #22

                I agree.. :):laugh: John

                1 Reply Last reply
                0
                • R Rodolfo Lima

                  I think that what he meant was something like: if(abs(0.4-1.2)-0.8) < Double.Epsilon) which is the proper way to compare floating point numbers, i guess.

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

                  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
                  • M Michael Dunn

                    Chris Maunder wrote: 2.0 == 4.0 * 0.5 Won't that actually be true because all those numbers are powers of 2? :cool: --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER

                    R Offline
                    R Offline
                    Ryan Binns
                    wrote on last edited by
                    #24

                    Michael Dunn wrote: Won't that actually be true because all those numbers are powers of 2? Yeah it will. Not a good choice of numbers huh? :)

                    Ryan

                    "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                    1 Reply Last reply
                    0
                    • C Chris Maunder

                      The value represented in a floating point variable is only an approximation to a value, not an exact representation. Because of this you cannot assume that something like 2.0 == 4.0 * 0.5 will return true. When comparing floating point values you need to estimate how close is close enough then do a fuzzy comparision (something like ABS(A-B) < Epsilon) cheers, Chris Maunder

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

                      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 1 Reply Last reply
                      0
                      • I Ivor S Sargoytchev

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

                        I Offline
                        I Offline
                        igor1960
                        wrote on last edited by
                        #26

                        I understand that this thread is not serious about anything, but very valid point has been raised. I would give you an answer: nobody knows... until native code is created. During days when C ruled you could heave an option of switching on/off coprocessor, floating point emulator library and etc. So, you can adjust precissions and etc... Now, looks like JIT should be creating best possible code depending on your computer configuration: what exactly does that mean for floating point case -- I didn't figure out yet. But yes, they will be alot of knowledgeable people on this thread willing to answer -- no doubt... "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                        1 Reply Last reply
                        0
                        • 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
                                          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