|
Prev: workbook saveas: how? (interop)
Next: reference type
From: hswerdfe on 4 Apr 2006 13:18 I have a function to convert Strings to Doubles double StringToFloat(CString a_test); some times StringToFloat returns a -1.#IND for values that can't be converted This value is supposed to be in a Range. and sometimes that range is not bounded -1.#INF to 1.#INF so if I do something Like this, when GetMin() returns -1.#INF and GetMax() return 1.#INF double testNum = StringToFloat(strBAdString); //testNum is now -1.#IND bool tmp = testNum <= m_max; bool tmp2 = testNum >= GetMin(); bool tmp3 = testNum <= GetMax(); bool tmp4 = testNum == GetMax(); double tmpMax = GetMax(); bool tmp5 = testNum <= tmpMax; I have a problem that all the tmp bool's are true, except tmp3. tmp3 is false. GetMax() is exactly a one line function that looks like this. double myClass::GetMax() const { return m_max; } and m_max is a member function double m_max; So my question is Why? Why is tmp3 false? not only that but shouldn't tmp4 be false. why is it true? I'm using VC++6 running in Debug mode. thanks , how
From: Simon Trew on 4 Apr 2006 19:34 "hswerdfe" <hswerdfe(a)example.com> wrote in message news:e0u986$757$1(a)nrc-news.nrc.ca... > > So my question is Why? Why is tmp3 false? Don't know, but there is something fishy about m_max-- what is it? This complete program sets b to true: #include <limits> double GetMax() { return std::numeric_limits<double>::quiet_NaN(); } int main(int, char**) { double max = GetMax(); bool const b = max <= max; return b; } How is m_max initialized? NaNs never compare equal to themselves, but I'm not sure about <=. If we assume the tautology (a<=b) === !(b>a) then we'd hope it was always consistent, but I'm not sure that's actually required.
From: hswerdfe on 5 Apr 2006 12:48 Simon Trew wrote: > "hswerdfe" <hswerdfe(a)example.com> wrote in message > news:e0u986$757$1(a)nrc-news.nrc.ca... > >>So my question is Why? Why is tmp3 false? > > > Don't know, but there is something fishy about m_max-- what is it? This > complete program sets b to true: > > #include <limits> > > double GetMax() > { > return std::numeric_limits<double>::quiet_NaN(); > } > > int main(int, char**) > { > double max = GetMax(); > bool const b = max <= max; > return b; > } > > How is m_max initialized? if no value is passed into the Object construtor SetMax(HUGE_VAL); where Set Max looks like this BOOL myClass::SetMax(const double a_newVal) { m_max = a_newVal; return true; } > NaNs never compare equal to themselves, but I'm > not sure about <=. If we assume the tautology (a<=b) === !(b>a) then we'd > hope it was always consistent, but I'm not sure that's actually required. now I am a little fuzzy I though '(a<=b) == !(b>a)' this had to be true. I guess math works diffrent in the world of Nans. as a follow up I thought the problem might have something to do with the fact that GetMax was a const function so I made 2 Test Functions double myClass::TestConst() const { double Zero = 0.0; double NaN = 0.0/ Zero; bool tmp = NaN <= m_max; bool tmp2 = NaN >= GetMin(); bool tmp3 = NaN <= GetMax(); //bool tmp4 = NaN <= GetMaxNotConst(); return 1.0; } double myClass::TestNotConst() { double Zero = 0.0; double NaN = 0.0/ Zero; bool tmp = NaN <= m_max; bool tmp2 = NaN >= GetMin(); bool tmp3 = NaN <= GetMax(); bool tmp4 = NaN <= GetMaxNotConst(); return 1.0; } where GetMaxNotConst() looks like this double myClass::GetMaxNotConst() { return m_max; } this had no effect tmp, tmp2 are true in both of the above functions and tmp3, tmp4 are both false. at anyrate I have conluded that it is probably not safe to assume anything when comparing with any opperator #IND, and #INF. so I am now using int _finite( double x ); and int _fpclass( double x ); to check for this kind of stuff. and returning the compaison value I want ..:) It would still be nice to know why? but as my old DOS based risk game used to say. * ours is not to reason why ours is but to do or die. back to work, now. Thanks, how > >
From: Murrgon on 5 Apr 2006 14:28 There is a really good article in "Game Programming Gems 6" called "Floating-Point Tricks". It should provide you with some clarity. Here is a small excerpt which may help you: "For two float values v1 and v2, is it true that one of the clauses v1 > v2, v1 == v2 or v1 < v2 is true? Again, NO. All comparisons of any flavor fail if one operand is a NaN." Murrgon
From: hswerdfe on 5 Apr 2006 17:07
Murrgon wrote: > There is a really good article in "Game Programming Gems 6" > called "Floating-Point Tricks". It should provide you with > some clarity. Here is a small excerpt which may help you: > > "For two float values v1 and v2, is it true that one of the > clauses v1 > v2, v1 == v2 or v1 < v2 is true? Again, NO. > All comparisons of any flavor fail if one operand is a NaN." while this may be the C++ standard, (I wouldn't know). This is not result I experience, with the implementation I am using (MS VC++ 6.0) as sometimes they do return true. > Murrgon thanks for your help Murrgon. |