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


i'm developing platform without math library, need build own tools. current way of getting fraction convert float fixed point (multiply (float)0xffff, cast int), lower part (mask 0xffff) , convert float again.

however, imprecision killing me. i'm using frac() , invfrac() functions draw anti-aliased line. using modf smooth line. own method pixels start jumping around due precision loss.

this 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!

if understand question correctly, want part after decimal right? don't need in fraction (integer numerator , denominator)?

so have number, 3.14159 , want end 0.14159. assuming our number stored in float f;, can this:

f = f-(long)f; 

which, if insert our number, works this:

0.14159 = 3.14159 - 3; 

what remove whole number portion of float leaving decimal portion. when convert float long, drops decimal portion. when subtract original float, you're left only decimal portion. need use long here because of size of float type (8 bytes on systems). integer (only 4 bytes on many systems) isn't large enough cover same range of numbers float, long should be.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -