double

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brekehan

    #1

    double

    I ran across some code that is initializing a double:

    double x = 1e-5;

    Does the standard dictate the compiler to interpret that as 0.00001?
    or is the above representation error prone?

    ,
    Chris

  • Gianni Mariani

    #2
    Re: double

    brekehan wrote:
    I ran across some code that is initializing a double:
    >
    double x = 1e-5;
    >
    Does the standard dictate the compiler to interpret that as 0.00001?
    It does.
    or is the above representation error prone?
    I've yet to see an error with this.

    Comment

    • brekehan

      #3
      Re: double

      Well, I am experiencing some very odd behavior where stepping through
      my code, a comparison between doubles is resulting in true when it
      should be false.

      inline bool LessThan(double high, double low)
      {
      double error = 1e-5;
      return (high - low) error;
      }


      LessThan(0.0, 0);
      // returns true!!!
      //(0.0 - 0.0) 0.00001 should evaluate to false.

      There is a rumor around the office that this is some gcc or processor
      specific artifact where doubles have a differant precision in
      registers vs memory or something along those lines. I falsely assumed
      the representation of the double being assigned was at fault.

      Has anyone come across problems comparing two doubles before?

      Comment

      • AnonMail2005@gmail.com

        #4
        Re: double

        On Oct 10, 5:27 pm, brekehan <cp...@austin.r r.comwrote:
        Well, I am experiencing some very odd behavior where stepping through
        my code, a comparison between doubles is resulting in true when it
        should be false.
        >
        inline bool LessThan(double high, double low)
        {
        double error = 1e-5;
        return (high - low) error;
        >
        }
        >
        LessThan(0.0, 0);
        // returns true!!!
        //(0.0 - 0.0) 0.00001 should evaluate to false.
        >
        There is a rumor around the office that this is some gcc or processor
        specific artifact where doubles have a differant precision in
        registers vs memory or something along those lines. I falsely assumed
        the representation of the double being assigned was at fault.
        >
        Has anyone come across problems comparing two doubles before?
        As long as you use a tolerance you should be fine.

        But you should use something like this:
        std::fabs (high - low) tolerance; // std::fabs is in <cmathbtw

        hth

        Comment

        • Gianni Mariani

          #5
          Re: double

          brekehan wrote:
          ....
          Has anyone come across problems comparing two doubles before?
          all the time.

          The code below returns false for me.

          inline bool LessThan(double high, double low)
          {
          double error = 1e-5;
          return (high - low) error;
          }

          #include <iostream>

          int main()
          {
          std::cout << LessThan(0.0, 0);
          }

          ..... I can't reproduce your problem.

          Comment

          • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

            #6
            Re: double

            On 2007-10-10 23:27, brekehan wrote:
            Well, I am experiencing some very odd behavior where stepping through
            my code, a comparison between doubles is resulting in true when it
            should be false.
            >
            inline bool LessThan(double high, double low)
            {
            double error = 1e-5;
            return (high - low) error;
            }
            >
            >
            LessThan(0.0, 0);
            // returns true!!!
            //(0.0 - 0.0) 0.00001 should evaluate to false.
            It works for me with VC++.
            There is a rumor around the office that this is some gcc or processor
            specific artifact where doubles have a differant precision in
            registers vs memory or something along those lines. I falsely assumed
            the representation of the double being assigned was at fault.
            >
            Has anyone come across problems comparing two doubles before?
            When directly comparing doubles one should expect problems, but with
            code like yours, never.

            --
            Erik Wikström

            Comment

            • brekehan

              #7
              Re: double

              On Oct 10, 4:48 pm, Gianni Mariani <gi4nos...@mari an.wswrote:
              brekehan wrote:
              >
              ...
              >
              Has anyone come across problems comparing two doubles before?
              >
              all the time.
              >
              The code below returns false for me.
              >
              inline bool LessThan(double high, double low)
              {
              double error = 1e-5;
              return (high - low) error;
              >
              }
              >
              #include <iostream>
              >
              int main()
              {
              std::cout << LessThan(0.0, 0);
              >
              }
              >
              .... I can't reproduce your problem.


              Most likely because of some compiler flag we are using combined with
              the version of gcc, the processor, and what instructions it is
              actually using :( I've been told we are using "double precision". I am
              still trying to decypher all these compiler flags.


              Comment

              • AnonMail2005@gmail.com

                #8
                Re: double

                On Oct 10, 5:44 pm, "AnonMail2...@g mail.com" <AnonMail2...@g mail.com>
                wrote:
                On Oct 10, 5:27 pm, brekehan <cp...@austin.r r.comwrote:
                >
                >
                >
                Well, I am experiencing some very odd behavior where stepping through
                my code, a comparison between doubles is resulting in true when it
                should be false.
                >
                inline bool LessThan(double high, double low)
                {
                double error = 1e-5;
                return (high - low) error;
                >
                }
                >
                LessThan(0.0, 0);
                // returns true!!!
                //(0.0 - 0.0) 0.00001 should evaluate to false.
                >
                There is a rumor around the office that this is some gcc or processor
                specific artifact where doubles have a differant precision in
                registers vs memory or something along those lines. I falsely assumed
                the representation of the double being assigned was at fault.
                >
                Has anyone come across problems comparing two doubles before?
                >
                As long as you use a tolerance you should be fine.
                >
                But you should use something like this:
                std::fabs (high - low) tolerance; // std::fabs is in <cmathbtw
                >
                hth- Hide quoted text -
                >
                - Show quoted text -
                That should be a <.

                Comment

                • brekehan

                  #9
                  Re: double

                  On Oct 10, 4:49 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
                  On 2007-10-10 23:27, brekehan wrote:
                  >
                  Well, I am experiencing some very odd behavior where stepping through
                  my code, a comparison between doubles is resulting in true when it
                  should be false.
                  >
                  inline bool LessThan(double high, double low)
                  {
                  double error = 1e-5;
                  return (high - low) error;
                  }
                  >
                  LessThan(0.0, 0);
                  // returns true!!!
                  //(0.0 - 0.0) 0.00001 should evaluate to false.
                  >
                  It works for me with VC++.
                  >
                  There is a rumor around the office that this is some gcc or processor
                  specific artifact where doubles have a differant precision in
                  registers vs memory or something along those lines. I falsely assumed
                  the representation of the double being assigned was at fault.
                  >
                  Has anyone come across problems comparing two doubles before?
                  >
                  When directly comparing doubles one should expect problems, but with
                  code like yours, never.
                  >
                  --
                  Erik Wikström
                  I wonder if it is some combination of compiler version, processor,
                  flags, etc.
                  The forementioned code was compiled on gcc 3.3.1
                  There seems to be a mess of compiler flags too one if which I am told
                  is "double precision"
                  I'd give more info if it was my code and my environment :/ I'll keep
                  searching around.

                  Comment

                  • LR

                    #10
                    Re: double

                    brekehan wrote:
                    >>Well, I am experiencing some very odd behavior where stepping through
                    >>my code, a comparison between doubles is resulting in true when it
                    >>should be false.



                    LR

                    Comment

                    • Christopher Pisz

                      #11
                      Re: double


                      "LR" <lruss@superlin k.netwrote in message
                      news:470d7183$0 $7225$cc2e38e6@ news.uslec.net. ..
                      brekehan wrote:
                      >
                      >>>Well, I am experiencing some very odd behavior where stepping through
                      >>>my code, a comparison between doubles is resulting in true when it
                      >>>should be false.
                      >
                      >

                      >
                      LR
                      That faq says why it isn't working, but doesn't give much insite into how to
                      get it to work. Yes, my mind is blown. I always depended on my compiler
                      being smart enough to know how to give me the result of a floating point
                      comparison accurately. If one value is differant than another depending on
                      its storage, I would think the compiled code would make sure they are stored
                      the same way when comparing...gue ss not. Can't point the finger though. I
                      haven't exactly written my own compiler or created any floating point
                      hardware. The remaining question is, how to safely compare two doubles with
                      all of the comparison operators and how to safely perform arithmetic on
                      floating point numbers and get the desired results? I figured that was the
                      entire purpose if this comparison function existing in the first
                      place...since we can't rely on a built in < , >, or == operator.




                      Comment

                      • LR

                        #12
                        Re: double

                        Christopher Pisz wrote:
                        "LR" <lruss@superlin k.netwrote in message
                        news:470d7183$0 $7225$cc2e38e6@ news.uslec.net. ..
                        >brekehan wrote:
                        >>
                        >>>>Well, I am experiencing some very odd behavior where stepping through
                        >>>>my code, a comparison between doubles is resulting in true when it
                        >>>>should be false.
                        >>
                        >http://www.parashift.com/c++-faq-lit...html#faq-29.17
                        >>
                        >LR
                        >
                        That faq says why it isn't working, but doesn't give much insite into how to
                        get it to work. Yes, my mind is blown. I always depended on my compiler
                        being smart enough to know how to give me the result of a floating point
                        comparison accurately. If one value is differant than another depending on
                        its storage, I would think the compiled code would make sure they are stored
                        the same way when comparing...gue ss not. Can't point the finger though. I
                        haven't exactly written my own compiler or created any floating point
                        hardware. The remaining question is, how to safely compare two doubles with
                        all of the comparison operators and how to safely perform arithmetic on
                        floating point numbers and get the desired results? I figured that was the
                        entire purpose if this comparison function existing in the first
                        place...since we can't rely on a built in < , >, or == operator.
                        >
                        Sorry. Responded too fast.

                        I went back and took a look again.

                        The code the OP posted was:

                        inline bool LessThan(double high, double low) {
                        double error = 1e-5;
                        return (high - low) error;
                        }

                        .....
                        const bool r = LessThan(0.0, 0);

                        // r ends up as true, but
                        //(0.0 - 0.0) 0.00001 should evaluate to false.

                        I wonder if somewhere else in the code there is a function like this:

                        inline bool LessThan(double high, int low) {
                        return true;
                        }

                        LR

                        Comment

                        • Christopher Pisz

                          #13
                          Re: double


                          "LR" <lruss@superlin k.netwrote in message
                          news:470d7b9a$0 $7224$cc2e38e6@ news.uslec.net. ..
                          Christopher Pisz wrote:
                          >"LR" <lruss@superlin k.netwrote in message
                          >news:470d7183$ 0$7225$cc2e38e6 @news.uslec.net ...
                          >>brekehan wrote:
                          >>>
                          >>>>>Well, I am experiencing some very odd behavior where stepping through
                          >>>>>my code, a comparison between doubles is resulting in true when it
                          >>>>>should be false.
                          >>>
                          >>http://www.parashift.com/c++-faq-lit...html#faq-29.17
                          >>>
                          >>LR
                          >>
                          >That faq says why it isn't working, but doesn't give much insite into how
                          >to get it to work. Yes, my mind is blown. I always depended on my
                          >compiler being smart enough to know how to give me the result of a
                          >floating point comparison accurately. If one value is differant than
                          >another depending on its storage, I would think the compiled code would
                          >make sure they are stored the same way when comparing...gue ss not. Can't
                          >point the finger though. I haven't exactly written my own compiler or
                          >created any floating point hardware. The remaining question is, how to
                          >safely compare two doubles with all of the comparison operators and how
                          >to safely perform arithmetic on floating point numbers and get the
                          >desired results? I figured that was the entire purpose if this comparison
                          >function existing in the first place...since we can't rely on a built in
                          >< , >, or == operator.
                          >>
                          >
                          Sorry. Responded too fast.
                          >
                          I went back and took a look again.
                          >
                          The code the OP posted was:
                          >
                          inline bool LessThan(double high, double low) {
                          double error = 1e-5;
                          return (high - low) error;
                          }
                          >
                          ....
                          const bool r = LessThan(0.0, 0);
                          >
                          // r ends up as true, but
                          //(0.0 - 0.0) 0.00001 should evaluate to false.
                          >
                          I wonder if somewhere else in the code there is a function like this:
                          >
                          inline bool LessThan(double high, int low) {
                          return true;
                          }
                          >
                          LR

                          Nope, that isn't it, because stepping through the debugger it goes into the
                          function posted.


                          Comment

                          • Stuart Redmann

                            #14
                            Re: double: The rumours you have heard are true

                            brekehan wrote:
                            Well, I am experiencing some very odd behavior where stepping through
                            my code, a comparison between doubles is resulting in true when it
                            should be false.
                            >
                            inline bool LessThan(double high, double low)
                            {
                            double error = 1e-5;
                            return (high - low) error;
                            }
                            >
                            >
                            LessThan(0.0, 0);
                            // returns true!!!
                            //(0.0 - 0.0) 0.00001 should evaluate to false.
                            >
                            There is a rumor around the office that this is some gcc or processor
                            specific artifact where doubles have a differant precision in
                            registers vs memory or something along those lines. I falsely assumed
                            the representation of the double being assigned was at fault.
                            See http://www.intel.com/support/perform.../cs-007691.htm.
                            As for the case you state above, this issue doesn't matter (the precision is
                            high enough for the comparison under Linux as well as Windows apps).

                            It would be nice to know which compiler + flags you are using (possibly some HW
                            description might help, too). As this question would then become a bit off-topic
                            here, you might consider re-stating your question in a newsgroup dedicated to
                            your compiler/HW.

                            Regards,
                            Stuart

                            Comment

                            • Tim Slattery

                              #15
                              Re: double

                              brekehan <cpisz@austin.r r.comwrote:
                              >I ran across some code that is initializing a double:
                              >
                              >double x = 1e-5;
                              >
                              >Does the standard dictate the compiler to interpret that as 0.00001?
                              >or is the above representation error prone?
                              1e-.5 means 1 times ten to the -5 power, which works out to .00001.
                              You then are converting it to a double precision floating point.
                              Floating point numbers are approximations! Even if you have enough
                              precision to store all the digits (which you do here) you have to
                              remember that not all decimal fractions can be exactly represented by
                              binary fractions. And floating point numbers are nearly always stored
                              internally as binary fractions. So what you get is not exactly what
                              you see.

                              --
                              Tim Slattery
                              Slattery_T@bls. gov

                              Comment

                              Working...