CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    Question Mathematical problem with a template

    Hi,
    I have this template
    Code:
    template <class T> class dimensions
    {
      protected:
        T m_xstart;
        T m_xend;
        T m_ystart;
        T m_yend;
    
      public:
       inline T getxstart(){return m_xstart;}
       inline T getxend(){return m_xend;}
       inline T getystart(){return m_ystart;}
       inline T getyend(){return m_yend;}
    
       inline void setxstart(T xstart){m_xstart = xstart;}
       inline void setxend(T xend){m_xend = xend;}
       inline void setystart(T ystart){m_ystart = ystart;}
       inline void setyend(T yend){m_yend = yend;}
    
       inline double getaspectratio()
       {
         T width = m_xend - m_xstart;
         T height = m_yend - m_ystart;
    
         if (height)
           return width / height;}
    };
    Now if I do the following:
    Code:
      dimensions<double> dim;
      dim.setxstart(0);
      dim.setxend(500);
      dim.setystart(0);
      dim.setyend(1000);
    
      double ratio = dim.getaspectratio();
    "ratio" is allways zero. I expect it to be 0.5. Why is this?

    Thanks in advance

    Juergen
    Last edited by AlionSolutions; December 11th, 2006 at 07:40 PM. Reason: Typo

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Mathematical problem with a template

    Quote Originally Posted by AlionSolutions
    "ratio" is allways zero. I expect it to be 0.5. Why is this?
    How do you check the value?

    - petter

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Mathematical problem with a template

    Quote Originally Posted by AlionSolutions
    Hi,
    I have this template
    There is a flaw in your template:
    Code:
       inline double getaspectratio()
       {
         T width = m_xend - m_xstart;
         T height = m_yend - m_ystart;
    
         if (height)
           return width / height;
       }
    If the template is based on integers, this truncates the value. As a matter of fact, your template will not compile at all if it is templatized on a type that does not have an operator /, and if it cannot be converted to double.
    "ratio" is allways zero. I expect it to be 0.5. Why is this?
    This will be my new motto:

    Did you debug the program to figure out why you may be getting the value?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 11th, 2006 at 08:12 PM.

  4. #4
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    Re: Mathematical problem with a template

    Yes I did debug the code and as I am using doubles for the template, in my opinion it must return 0.5.
    No clue why it is not so. In the debugger I checked the return value of getaspectratio() and I checked the values in side the template function. All looks well. I still don't know ehere the problem is.

    Thanks

    Juergen

  5. #5
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    Re: Mathematical problem with a template

    Now I changed the getaspectratio()-function in the following way:

    Code:
    inline double getaspectratio()
       {
         double width = m_xend - m_xstart;
         double height = m_yend - m_ystart;
         return width / height;}
    Ok, now it returns another value, which amazes me much more:

    0.4994994994995

    Why is this? I expected a plain 0.5 ???

    Thanks

    Juergen

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Mathematical problem with a template

    Welcome to the wonderful world if floating point math

    Realize that 0.1 is a repeating binary pattern that can not be expressed in floating point format exactly. This is one reason why floading point numbers should never be compared for equality, and why different processors will return different values for the same calculation....

    Now where did I send that space sattellite off to.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    Re: Mathematical problem with a template

    Thanks CPUWizzard
    Is there any way to get that fixed? I need the "correct" values for further calculations. I have a zoom/scroll function in a matrix implented using doubles and I wondered why those computed values have so many digits after the point. Now I know why... do I? Nope I don't. I thought floating point calculation is the most accurate way to compute and now I get to know that there are such "incorrect" values. Those things ruin my calculations. Do you have any idea how to compute precise ?

    Greetings

    Juergen

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Mathematical problem with a template

    1) Floating point math is not exact. The only exact binary numbers are ones that are multiples of inverse powers of 2 (1/2, 1/4, 3/8, etc).

    2) Going back to my point of the doubles in the template, what other types were you going to call the getaspectratio? I would have expected it to be coded like this:
    Code:
    inline T getaspectratio()
       {
         T width = m_xend - m_xstart;
         T height = m_yend - m_ystart;
         return width / height;
      }
    Now if T were a special type (maybe a 128-bit mathematical class type that represents a double), then this makes much more sense.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Mathematical problem with a template

    Quote Originally Posted by AlionSolutions
    I thought floating point calculation is the most accurate way to compute
    No it isn't. Financial institutions disallow programmers from using doubles to compute monetary or any financially based calculations. That's why those institutions still use COBOL, or if they must use C++, special fixed point or BCD libraries are used, never doubles.
    and now I get to know that there are such "incorrect" values. Those things ruin my calculations. Do you have any idea how to compute precise ?
    How precise? If you can get by with some round-off error, then you can round off most numbers by adding 0.005 or something similar. If you need exact calculations, see the paragraph above.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    Re: Mathematical problem with a template

    Thanx to you too Paul,
    yeah makes sense. Ok, but if you say that floating point math is not exact, what would you recommend for a mathematical calculation software for computing? I allmost thought about using integers for the calculation and splitting up the values. I did this before back then when I coded assembler on the Amiga, because I had no FPU. But I thought it would be clever to use the floats, now that we all have FPUs.

    So what ? Use integers or is there another way to get precise floating point calculations?

    Thanks again

    Juergen

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Mathematical problem with a template

    Quote Originally Posted by AlionSolutions
    Do you have any idea how to compute precise ?
    Aha. So you see why I coded the getaspectratio the way I did? What if you discover a great mathematical class that gives you exact calculations? Not only that the class has overloaded operator / to do division exactly.

    You don't have to change a single line of code in the template, if you recode the getaspectratio the way I did. You just templatize on this new type. You then can use doubles if you want to, or use another class.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Mathematical problem with a template

    Quote Originally Posted by AlionSolutions
    I allmost thought about using integers for the calculation and splitting up the values. I did this before back then when I coded assembler on the Amiga, because I had no FPU. But I thought it would be clever to use the floats, now that we all have FPUs.
    It looks like you need to get back that old code, or use something like MFC's COleCurrency.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Mathematical problem with a template

    Quote Originally Posted by Paul McKenzie
    No it isn't. Financial institutions disallow programmers from using doubles to compute monetary or any financially based calculations. That's why those institutions still use COBOL, or if they must use C++, special fixed point or BCD libraries are used, never doubles.
    I haven't experienced this yet. I have worked on US Treasury bonds, MBS, Swaps and other Interest rate derivatives without any issues. It is true that there is a compromise on the degree of accuracy one gets but we were (and the clients) happy with the results.

    We also used them for calculations involving numerical methods, which themselves are not very accurate.

    To OP - you may be fine here, it is just that you may need a rounding (or some level of error acceptance) mechanism as Paul suggested. What I mean is 0.4994994994995 is not exactly 0.5 but it is more 0.5 than 0.499. There are 2 ways - either you take this value and do further calculations or delay the aspectratio calculation for later (division). For example if this:
    Code:
    getAspectRation()*someValue/someOtherValue
    is what you are to do then postpone division to this expression, i.e. don't do:
    Code:
    0.4994994994995*someValue/someOtherValue
    but do:
    Code:
    500/1000*someValue/someOtherValue
    This may help reduce the error in the result a little bit.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Mathematical problem with a template

    Quote Originally Posted by exterminator
    I haven't experienced this yet. I have worked on US Treasury bonds, MBS, Swaps and other Interest rate derivatives without any issues. It is true that there is a compromise on the degree of accuracy one gets but we were (and the clients) happy with the results.
    If you work in the insurance industry, where exact monetary amounts are required, usage of doubles (or whatever the floating point type is) is forbidden. You cannot compromise anything, else the company is at risk for litigation. If your customer is supposed to be owed $40.45 and you calculate the amount as $40.43, those 2 pennies can get you in a lot of legal trouble.

    I remember working for an insurance company, where one person used doubles to calculate some values using a formula that was developed by the actuaries. More often than not, the actuaries would compare the results of the program with a hand calculation, and the program calculations were almost always off. Multiply that 1 penny off by 1,000 (which many calculations call for), and you see the mess you can get yourself into.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: Mathematical problem with a template

    Quote Originally Posted by AlionSolutions
    Is there any way to get that fixed? I need the "correct" values for further calculations.
    You can use boost::rational for exact computation.

    However, your application most likely does not need it. A certain amount of error is acceptable in most applications, including virtually all scientific applications.

    Jeff

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured