CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2012

    Very Simple question about checking zero value ..

    Dear All ,

    To calculate line slope I am using formula ,

    line_slope = (meanxy - (meanx * meany)) / (meanyy) - (meany * meany));

    All the variables are floats.

    How to check whether the denominator .. (meanyy)- (meany * meany)) is zero ? Cause its float.

    can I check with ..

    float f1 = (meanyy) - (meany * meany) ;

    if (f1 == 0.0) ..

    Will this work surely ?

    I am using this formula in project based on winXP but it survived. As soon as I ported the code
    on DPMI based code , I observed a system hang after some time if all the points I supply are (0,0)
    while drawing a line.

    Thanks a lot in advance

  2. #2
    Join Date
    Apr 1999

    Re: Very Simple question about checking zero value ..

    As you can see, floating point doesn't always work the same way as algebra. Many formulas you see in your algebra book have to be rewritten for computers to use them properly, all due to floating point issues.

    You cannot check for exactly equal to 0 with floating point. You should check if the denominator is close to 0 before dividing. If the denominator is close to 0, then don't divide.

    Please see this link on how to write a function to test if a floating point value is close to another value, within a tolerance limit.



    Paul McKenzie
    Last edited by Paul McKenzie; December 22nd, 2012 at 01:17 AM.

  3. #3
    Join Date
    Mar 2006

    Re: Very Simple question about checking zero value ..

    The best way to fix this depends on whether you normally expect to have steep (nearly vertical) lines or whether they can have any orientation.

    In the former case (if lines will always be steep) the best approach is to reorient your axis so your data is nearly horizontal instead of nearly vertical, then adjust your code for the new orientation.

    If your lines can be of any orientation, you need to actually have code for both axis orientations in place, then choose the orientation based on the way the data presents itself. (One way to do this is to calculate the standard deviation along with the mean and see which axis shows more spread.) If you encapsulate your regression routine into a line object to which you can pass your points in a way where the x coordinates and y coordinates can be swapped, you'll be able to cleanly have both orientations in place without a lot of duplicate code.

    Sorry that's probably not the quick fix you were hoping for, but you're just not going to get good results for steep lines without reorienting. The problem is that as the points upon which you are doing your least squares fit approach infinite slope the error caused by the floating point representation is going to begin to provide uselessly inaccurate results.

    Under Windows XP, I would expect that in some cases you were getting wild results and upon division by zero you should have been getting a +/- INF slope. Under XP you should get a Quiet NAN by giving a series of (0, 0) points (because the numerator and denominator are both zero). INF and NAN won't cause a crash (and are really quite useful) but instead just produce odd results in code not expecting them.

    I think under DPMI (you do mean DOS Protected Mode, right?) you might get a non-maskable interrupt instead of +/-INF or NAN. I only say that based on having seen NMIs result from calling sqrt with negative arguments under DOS (where the same under Windows is NAN). This might be what's causing the hang.

  4. #4
    Join Date
    Jan 2012

    Re: Very Simple question about checking zero value ..

    Thankx a lot Paul and GeoRanger.

    I read the link , Paul has given and will write a function to check zero.

    GeoRanger , I am worst in Geometry but will think on orientation algorithm ..
    XP saved me but DPMI is dangerous. I am working with PCI cardset and FPGA.
    and System hangs after some time .The time varies. It was difficult to get the
    problem but any how I managed.

    Thank u once again and Merry X'mas

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

By using this site, you agree to the Privacy Policy