Rejigging a floating point equation ...

Posted by Jamie on Stack Overflow See other posts from Stack Overflow or by Jamie
Published on 2009-05-27T18:51:24Z Indexed on 2010/12/27 23:54 UTC
Read the original article Hit count: 231

I'd like to know if there is a way to improve the accuracy of calculating a slope. (This came up a few months back here).

It seems by changing:

float get_slope(float dXa, float dXb, float dYa, float dYb) {
    return (dXa - dXb)/(dYa - dYb);
}

to

float get_slope(float dXa, float dXb, float dYa, float dYb) {
    return  dXa/(dYa - dYb) - dXb/(dYa - dYb);
}

might be an improvement. Suggestions?

Edit: It's precision I'm after, not efficiency.

© Stack Overflow or respective owner

Related posts about floating-point

Related posts about precision