Converting Float to unsigned short and Back

Discussion in 'C Programming' started by avsrk@mailcity.com, May 16, 2006.

  1. Guest

    Hello Folks ,

    General C data types question , more geared up towards embedded folks .

    I have a positive float quantity with a fractional part (one decimal
    point) which occupies
    4 bytes . Now i want to stuff it in to a two byte quantity like
    unsigned short and then
    reconvert it back to float .
    We can use any method like type casting or directly doing low level
    operations on the bytes ,
    or any other way you can think of ..
    The question is , is it possible to get the floating quantity back
    including its fractional part .

    Ex

    float fval = 52.3 ;

    unsigned short sval = (unsigned short ) fval ;

    float fvalnew = (float) sval ;


    will the fvalnew be 52.3 either by the above way( i know it is not we
    lose the fractional part) or
    any other way .

    Thanks
    subra
     
    , May 16, 2006
    #1
    1. Advertising

  2. writes:

    > Hello Folks ,
    >
    > General C data types question , more geared up towards embedded folks .
    >
    > I have a positive float quantity with a fractional part (one decimal
    > point) which occupies
    > 4 bytes . Now i want to stuff it in to a two byte quantity like
    > unsigned short and then
    > reconvert it back to float .
    > We can use any method like type casting or directly doing low level
    > operations on the bytes ,
    > or any other way you can think of ..
    > The question is , is it possible to get the floating quantity back
    > including its fractional part .
    >
    > Ex
    >
    > float fval = 52.3 ;
    >
    > unsigned short sval = (unsigned short ) fval ;
    >
    > float fvalnew = (float) sval ;
    >
    >
    > will the fvalnew be 52.3 either by the above way( i know it is not we
    > lose the fractional part) or
    > any other way .


    Well you could "pre-multiply" by 10, so that you keep the first digit
    after the decimal point.

    float fval = 52.3 ;

    unsigned short sval = (unsigned short) (10*fval) ;

    /* sval == 523 */

    float fvalnew = (float) sval / 10 ;

    --

    John Devereux
     
    John Devereux, May 16, 2006
    #2
    1. Advertising

  3. Vladimir Oka Guest

    opined:

    > Hello Folks ,
    >
    > General C data types question , more geared up towards embedded folks
    > .
    >
    > I have a positive float quantity with a fractional part (one decimal
    > point) which occupies
    > 4 bytes . Now i want to stuff it in to a two byte quantity like
    > unsigned short and then
    > reconvert it back to float .
    > We can use any method like type casting or directly doing low level
    > operations on the bytes ,
    > or any other way you can think of ..
    > The question is , is it possible to get the floating quantity back
    > including its fractional part .
    >
    > Ex
    >
    > float fval = 52.3 ;
    >
    > unsigned short sval = (unsigned short ) fval ;
    >
    > float fvalnew = (float) sval ;
    >
    >
    > will the fvalnew be 52.3 either by the above way( i know it is not we
    > lose the fractional part) or
    > any other way .


    It seems what you're really after is fixed point arithmetic. If you use
    it, you can get rid of floating point variables altogether. Just
    choose a suitable multiplication factor, and use integer arithmetics
    throughout. Since you say you only have one decimal place to care
    about, 10 sounds OK, but if your range allows, a 100 may be better
    especially if you do a lot of calculations.

    You may even find ready-made libraries to deal with fixed point
    numbers. GMP/bignum may be one, but you may want something leaner for
    embedded use. Depending on what you need it may be better to roll your
    own.

    --
    How do I type "for i in *.dvi do xdvi i done" in a GUI?
    (Discussion in comp.os.linux.misc on the intuitiveness of interfaces.)

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
     
    Vladimir Oka, May 16, 2006
    #3
  4. Flash Gordon Guest

    wrote:
    > Hello Folks ,
    >
    > General C data types question , more geared up towards embedded folks .
    >
    > I have a positive float quantity with a fractional part (one decimal
    > point) which occupies
    > 4 bytes . Now i want to stuff it in to a two byte quantity like
    > unsigned short and then
    > reconvert it back to float .


    When you can fit a quart in to a pint pot you can guarantee to do it.
    Until then it depends on the possible range. If 10*float will fit in to
    an unsigned short (which also means the float must be positive) you can
    do it, but in that case why are you using a float in the first place?

    > We can use any method like type casting or directly doing low level
    > operations on the bytes ,
    > or any other way you can think of ..
    > The question is , is it possible to get the floating quantity back
    > including its fractional part .
    >
    > Ex
    >
    > float fval = 52.3 ;
    >
    > unsigned short sval = (unsigned short ) fval ;
    >
    > float fvalnew = (float) sval ;
    >
    >
    > will the fvalnew be 52.3 either by the above way( i know it is not we
    > lose the fractional part) or
    > any other way .


    It would be better if you told us the problem you are actually trying to
    solve.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 16, 2006
    #4
  5. CBFalconer Guest

    wrote:
    >

    .... snip ...
    >
    > I have a positive float quantity with a fractional part (one
    > decimal point) which occupies 4 bytes . Now i want to stuff it
    > in to a two byte quantity like unsigned short and then reconvert
    > it back to float . We can use any method like type casting or
    > directly doing low level operations on the bytes , or any other
    > way you can think of .. The question is , is it possible to get
    > the floating quantity back including its fractional part .


    If you have a gallon of fuel (4 liters) can you put it in a one
    quart (one liter) can, transport it somewhere, and then fill
    another gallon can from it? To quote Hercule Poirot, apply the
    little gray brain cells.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, May 16, 2006
    #5
  6. Joe Smith Guest

    "Vladimir Oka" <> wrote in message
    news:...
    > opined:
    >
    >> Hello Folks ,
    >>
    >> General C data types question , more geared up towards embedded folks
    >> .
    >>
    >> I have a positive float quantity with a fractional part (one decimal
    >> point) which occupies
    >> 4 bytes . Now i want to stuff it in to a two byte quantity like
    >> unsigned short and then
    >> reconvert it back to float .
    >> We can use any method like type casting or directly doing low level
    >> operations on the bytes ,
    >> or any other way you can think of ..
    >> The question is , is it possible to get the floating quantity back
    >> including its fractional part .
    >>
    >> Ex
    >>
    >> float fval = 52.3 ;
    >>
    >> unsigned short sval = (unsigned short ) fval ;
    >>
    >> float fvalnew = (float) sval ;
    >>
    >>
    >> will the fvalnew be 52.3 either by the above way( i know it is not we
    >> lose the fractional part) or
    >> any other way .

    >
    > It seems what you're really after is fixed point arithmetic. If you use
    > it, you can get rid of floating point variables altogether. Just
    > choose a suitable multiplication factor, and use integer arithmetics
    > throughout. Since you say you only have one decimal place to care
    > about, 10 sounds OK, but if your range allows, a 100 may be better
    > especially if you do a lot of calculations.
    >
    > You may even find ready-made libraries to deal with fixed point
    > numbers. GMP/bignum may be one, but you may want something leaner for
    > embedded use. Depending on what you need it may be better to roll your
    > own.

    Won't machine epsilon become an issue if he wants to reach too far down on
    those floats? joe
     
    Joe Smith, May 17, 2006
    #6
  7. Vladimir Oka Guest

    Joe Smith wrote:
    > "Vladimir Oka" <> wrote in message
    > news:...
    > > opined:
    > >
    > >> Hello Folks ,
    > >>
    > >> General C data types question , more geared up towards embedded folks
    > >>
    > >> I have a positive float quantity with a fractional part (one decimal
    > >> point) which occupies 4 bytes . Now i want to stuff it in to a two byte quantity like
    > >> unsigned short and then reconvert it back to float . We can use any method like
    > >> type casting or directly doing low level operations on the bytes , or any other way
    > >> you can think of ..
    > >> The question is , is it possible to get the floating quantity back
    > >> including its fractional part .


    <snip>

    > > It seems what you're really after is fixed point arithmetic. If you use
    > > it, you can get rid of floating point variables altogether. Just
    > > choose a suitable multiplication factor, and use integer arithmetics
    > > throughout. Since you say you only have one decimal place to care
    > > about, 10 sounds OK, but if your range allows, a 100 may be better
    > > especially if you do a lot of calculations.
    > >
    > > You may even find ready-made libraries to deal with fixed point
    > > numbers. GMP/bignum may be one, but you may want something leaner for
    > > embedded use. Depending on what you need it may be better to roll your
    > > own.

    >
    > Won't machine epsilon become an issue if he wants to reach too far down on
    > those floats? joe


    Well, you win some, you lose some.

    With fixed point you have to be even more careful than with floating
    point when it comes to precision. If OP really has to squeeze that last
    bit of size/performance from an embedded system, it may be worth the
    extra (algorithmic) effort.
     
    Vladimir Oka, May 17, 2006
    #7
  8. Joe Smith Guest

    "Vladimir Oka" >
    > Joe Smith wrote:
    >> "Vladimir Oka" <> wrote in message
    >> news:...
    >> > opined:
    >> >
    >> >> Hello Folks ,
    >> >>
    >> >> General C data types question , more geared up towards embedded folks
    >> >>
    >> >> I have a positive float quantity with a fractional part (one decimal
    >> >> point) which occupies 4 bytes . Now i want to stuff it in to a two
    >> >> byte quantity like
    >> >> unsigned short and then reconvert it back to float . We can use any
    >> >> method like
    >> >> type casting or directly doing low level operations on the bytes , or
    >> >> any other way
    >> >> you can think of ..
    >> >> The question is , is it possible to get the floating quantity back
    >> >> including its fractional part .

    >
    > <snip>
    >
    >> > It seems what you're really after is fixed point arithmetic. If you use
    >> > it, you can get rid of floating point variables altogether. Just
    >> > choose a suitable multiplication factor, and use integer arithmetics
    >> > throughout. Since you say you only have one decimal place to care
    >> > about, 10 sounds OK, but if your range allows, a 100 may be better
    >> > especially if you do a lot of calculations.
    >> >
    >> > You may even find ready-made libraries to deal with fixed point
    >> > numbers. GMP/bignum may be one, but you may want something leaner for
    >> > embedded use. Depending on what you need it may be better to roll your
    >> > own.

    >>
    >> Won't machine epsilon become an issue if he wants to reach too far down
    >> on
    >> those floats? joe

    >
    > Well, you win some, you lose some.
    >
    > With fixed point you have to be even more careful than with floating
    > point when it comes to precision. If OP really has to squeeze that last
    > bit of size/performance from an embedded system, it may be worth the
    > extra (algorithmic) effort.

    I hope OP does clarify what he's after in such a manner that survives the
    "buckets of water" objection. I rather doubt somebody is paying him to do
    something so Sysypheun (sp?) joe
     
    Joe Smith, May 17, 2006
    #8
  9. Vladimir Oka Guest

    Joe Smith wrote:
    > "Vladimir Oka" >
    > > Joe Smith wrote:
    > >> "Vladimir Oka" <> wrote in message
    > >> news:...
    > >> > opined:
    > >> >
    > >> >> Hello Folks ,
    > >> >>
    > >> >> General C data types question , more geared up towards embedded folks
    > >> >>
    > >> >> I have a positive float quantity with a fractional part (one decimal
    > >> >> point) which occupies 4 bytes . Now i want to stuff it in to a two
    > >> >> byte quantity like
    > >> >> unsigned short and then reconvert it back to float . We can use any
    > >> >> method like
    > >> >> type casting or directly doing low level operations on the bytes , or
    > >> >> any other way
    > >> >> you can think of ..
    > >> >> The question is , is it possible to get the floating quantity back
    > >> >> including its fractional part .

    > >
    > > <snip>
    > >
    > >> > It seems what you're really after is fixed point arithmetic. If you use
    > >> > it, you can get rid of floating point variables altogether. Just
    > >> > choose a suitable multiplication factor, and use integer arithmetics
    > >> > throughout. Since you say you only have one decimal place to care
    > >> > about, 10 sounds OK, but if your range allows, a 100 may be better
    > >> > especially if you do a lot of calculations.
    > >> >
    > >> > You may even find ready-made libraries to deal with fixed point
    > >> > numbers. GMP/bignum may be one, but you may want something leaner for
    > >> > embedded use. Depending on what you need it may be better to roll your
    > >> > own.
    > >>
    > >> Won't machine epsilon become an issue if he wants to reach too far down
    > >> on
    > >> those floats? joe

    > >
    > > Well, you win some, you lose some.
    > >
    > > With fixed point you have to be even more careful than with floating
    > > point when it comes to precision. If OP really has to squeeze that last
    > > bit of size/performance from an embedded system, it may be worth the
    > > extra (algorithmic) effort.

    > I hope OP does clarify what he's after in such a manner that survives the
    > "buckets of water" objection. I rather doubt somebody is paying him to do
    > something so Sysypheun (sp?) joe


    If OP's sure his values can fit in a 16 bit unsigned value (multiplied
    by 10), I'd still advise him to stick to fixed point. It's always
    possible to temporarily expand the width during calculations if he's
    worried about lack of precision (e.g. use 32 bits with scaling factor
    of 1000 or more for all calculations). This strategy may fail if he
    needs to do some complicated maths on it (like sine or suchlike).
     
    Vladimir Oka, May 17, 2006
    #9
    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. bd
    Replies:
    0
    Views:
    651
  2. Replies:
    4
    Views:
    845
    Kaz Kylheku
    Oct 17, 2006
  3. Ioannis Vranos

    unsigned short, short literals

    Ioannis Vranos, Mar 4, 2008, in forum: C Programming
    Replies:
    5
    Views:
    703
    Eric Sosman
    Mar 5, 2008
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,607
    James Kanze
    Oct 8, 2009
  5. Dipesh Batheja
    Replies:
    1
    Views:
    583
    Bernard Kenik
    Nov 16, 2006
Loading...

Share This Page