Re: float saving using only N bits

Discussion in 'C Programming' started by Derk Gwen, Jun 30, 2003.

  1. Derk Gwen

    Derk Gwen Guest

    (Helen) wrote:
    # Hello
    #
    # I have one float a
    # I am trying to write a into a file but only use
    # n bits for that, what's the best way to do
    # for example
    #
    # a=0.391790
    #
    # I am trying to use 8 bits for that.

    You can try something like
    int exp; int fraction = (int)(MAX*frexp(number,&exp));
    Then
    number =approx= scalb((double)fraction/MAX,exp)

    If you want to pack it into that 3 bits exponent + 5 bits fraction, you can have
    something like
    enum {MAX=32};
    ...
    int packed = (exp&7)<<5 | fraction;

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    I love the smell of commerce in the morning.
     
    Derk Gwen, Jun 30, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom St Denis

    Re: float saving using only N bits

    Tom St Denis, Jun 29, 2003, in forum: C Programming
    Replies:
    0
    Views:
    694
    Tom St Denis
    Jun 29, 2003
  2. Ben Pfaff

    Re: float saving using only N bits

    Ben Pfaff, Jun 29, 2003, in forum: C Programming
    Replies:
    1
    Views:
    665
    CBFalconer
    Jun 29, 2003
  3. bd
    Replies:
    0
    Views:
    651
  4. sarahh

    sum of 64 bits using 32 bits cpu

    sarahh, May 13, 2008, in forum: C Programming
    Replies:
    18
    Views:
    542
    Richard Tobin
    May 14, 2008
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,607
    James Kanze
    Oct 8, 2009
Loading...

Share This Page