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 Offline
    I Offline
    Ivor S Sargoytchev
    wrote on last edited by
    #1

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

    D L T B N 8 Replies Last reply
    0
    • I Ivor S Sargoytchev

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

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      It will return the same as any IEEE floating point software will: rounding errors. http://www.statsdirect.com/discussion/_disc1/0000000d.htm[^]

      // Quantum sort algorithm implementation
      while (!sorted)
      ;

      N 1 Reply Last reply
      0
      • I Ivor S Sargoytchev

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

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Thru a microscope, everything looks big. Believe me that is -0.8. Try Console.Write(). leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        I 1 Reply Last reply
        0
        • I Ivor S Sargoytchev

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

          T Offline
          T Offline
          Tim Smith
          wrote on last edited by
          #4

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

          M 1 Reply Last reply
          0
          • L leppie

            Thru a microscope, everything looks big. Believe me that is -0.8. Try Console.Write(). leppie::AllocCPArticle(Generic DFA State Machine for .NET);

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

            It is not since "if (d == -0.8)" will fail. Ivor S. Sargoytchev Dundas Software

            D C 2 Replies Last reply
            0
            • D Daniel Turini

              It will return the same as any IEEE floating point software will: rounding errors. http://www.statsdirect.com/discussion/_disc1/0000000d.htm[^]

              // Quantum sort algorithm implementation
              while (!sorted)
              ;

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

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

              I O 2 Replies Last reply
              0
              • N Nemanja Trifunovic

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

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

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

                D realJSOPR 2 Replies Last reply
                0
                • I Ivor S Sargoytchev

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

                  D Offline
                  D Offline
                  Daniel Turini
                  wrote on last edited by
                  #8

                  Ivor S. Sargoytchev wrote: I guess I should use decimals. Decimals are very, very slow. Depending on what you're doing, there are techniques to workaround most IEEE rounding problems.

                  // Quantum sort algorithm implementation
                  while (!sorted)
                  ;

                  1 Reply Last reply
                  0
                  • I Ivor S Sargoytchev

                    It is not since "if (d == -0.8)" will fail. Ivor S. Sargoytchev Dundas Software

                    D Offline
                    D Offline
                    Daniel Turini
                    wrote on last edited by
                    #9

                    Yes, but you should compare it like:

                    if (Math.Abs(d - 0.8) < Double.Epsilon)

                    And everything looks fine. See Double.Epsilon docs on MSDN.

                    // Quantum sort algorithm implementation
                    while (!sorted)
                    ;

                    I 1 Reply Last reply
                    0
                    • D Daniel Turini

                      Yes, but you should compare it like:

                      if (Math.Abs(d - 0.8) < Double.Epsilon)

                      And everything looks fine. See Double.Epsilon docs on MSDN.

                      // Quantum sort algorithm implementation
                      while (!sorted)
                      ;

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

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

                        It is not since "if (d == -0.8)" will fail. Ivor S. Sargoytchev Dundas Software

                        C Offline
                        C Offline
                        Chris Maunder
                        wrote on last edited by
                        #11

                        Ivor S. Sargoytchev wrote: if (d == -0.8) You're comparing floating point values for equality?? :omg: cheers, Chris Maunder

                        I 1 Reply Last reply
                        0
                        • C Chris Maunder

                          Ivor S. Sargoytchev wrote: if (d == -0.8) You're comparing floating point values for equality?? :omg: cheers, Chris Maunder

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

                          Chris, With respect, some constructive criticism please. Ivor S. Sargoytchev Dundas Software

                          P C 2 Replies Last reply
                          0
                          • I Ivor S Sargoytchev

                            Chris, With respect, some constructive criticism please. Ivor S. Sargoytchev Dundas Software

                            P Offline
                            P Offline
                            peterchen
                            wrote on last edited by
                            #13

                            ok, you asked for it... It is *well known* (;P) that floating point numbers have a limited resolution (around 7 digits for single, 15 for double*), and comparing the result of an arithmetic operation using "==" is prone to err. if you know the order of magnitude of one comparand, or at least you know that it's "far enough" from zero, use the abs(a-b) < 1e-6*(abs of known comparand) comparison. Otherwise it gets a little bit more tricky. *) don't quote me on the numbers, look up the IEEE definitions for the format


                            "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                            sighist | Agile Programming | doxygen

                            1 Reply Last reply
                            0
                            • I Ivor S Sargoytchev

                              Chris, With respect, some constructive criticism please. Ivor S. Sargoytchev Dundas Software

                              C Offline
                              C Offline
                              Chris Maunder
                              wrote on last edited by
                              #14

                              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

                              L M I 3 Replies 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

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

                                Chris Maunder wrote: 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. So this is how you count your beer consumption isn't? Especially when Kiwi and Aussie mates are involved. ;P Michael Martin Australia "I personally love it because I can get as down and dirty as I want on the backend, while also being able to dabble with fun scripting and presentation games on the front end." - Chris Maunder 15/07/2002

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