Getting the fractional part of a float without using modf()

Posted by knight666 on Stack Overflow See other posts from Stack Overflow or by knight666
Published on 2010-04-07T18:27:33Z Indexed on 2010/04/07 18:33 UTC
Read the original article Hit count: 116

Filed under:
|
|
|

Hi,

I'm developing for a platform without a math library, so I need to build my own tools. My current way of getting the fraction is to convert the float to fixed point (multiply with (float)0xFFFF, cast to int), get only the lower part (mask with 0xFFFF) and convert it back to a float again.

However, the imprecision is killing me. I'm using my Frac() and InvFrac() functions to draw an anti-aliased line. Using modf I get a perfectly smooth line. With my own method pixels start jumping around due to precision loss.

This is my code:

const float fp_amount = (float)(0xFFFF);
const float fp_amount_inv = 1.f / fp_amount;

inline float Frac(float a_X)
{
    return ((int)(a_X * fp_amount) & 0xFFFF) * fp_amount_inv;
}

inline float Frac(float a_X)
{
    return (0xFFFF - (int)(a_X * fp_amount) & 0xFFFF) * fp_amount_inv;
}

Thanks in advance!

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++