C - Serialization of the floating point numbers (floats, doubles)

后端 未结 10 1049
攒了一身酷
攒了一身酷 2020-11-28 11:18

How to convert a floating point number into a sequence of bytes so that it can be persisted in a file? Such algorithm must be fast and highly portable. It must allow also th

10条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 12:00

    This might give you a good start - it packs a floating point value into an int and long long pair, which you can then serialise in the usual way.

    #define FRAC_MAX 9223372036854775807LL /* 2**63 - 1 */
    
    struct dbl_packed
    {
        int exp;
        long long frac;
    };
    
    void pack(double x, struct dbl_packed *r)
    {
        double xf = fabs(frexp(x, &r->exp)) - 0.5;
    
        if (xf < 0.0)
        {
            r->frac = 0;
            return;
        }
    
        r->frac = 1 + (long long)(xf * 2.0 * (FRAC_MAX - 1));
    
        if (x < 0.0)
            r->frac = -r->frac;
    }
    
    double unpack(const struct dbl_packed *p)
    {
        double xf, x;
    
        if (p->frac == 0)
            return 0.0;
    
        xf = ((double)(llabs(p->frac) - 1) / (FRAC_MAX - 1)) / 2.0;
    
        x = ldexp(xf + 0.5, p->exp);
    
        if (p->frac < 0)
            x = -x;
    
        return x;
    }
    

提交回复
热议问题