convert a float to a short

Discussion in 'C Programming' started by Partho, Jun 19, 2006.

  1. Partho

    Partho Guest

    I have a float variable which I need to add to a short variable. How do
    I do this? Do I have to typecast or is there a way around? I tried
    typecasting the float to a short, but that gives me a 0 or +/-1, which
    is unacceptable.

    Any pointers to how to get over this?
     
    Partho, Jun 19, 2006
    #1
    1. Advertising

  2. Partho

    Chris Dollin Guest

    Partho wrote:

    > I have a float variable which I need to add to a short variable. How do
    > I do this?


    myShort += (short) myFloat;

    looks like a plausible first cut. Of course the float will be truncated
    to a short value.

    > Do I have to typecast or is there a way around?


    You'll have to cast, since adding a float to an integer will get you
    a floaty value, and to unfloat something floaty needs an explicit
    conversion - a cast.

    > I tried
    > typecasting the float to a short, but that gives me a 0 or +/-1, which
    > is unacceptable.


    I'm /guessing/ that's because the float has values like 1.7, which truncate
    to 1 but that you'd rather treat as 2? If so, round it.

    [I do wonder what you're doing that involves adding floating-point numbers
    to shorts.]

    > Any pointers to how to get over this?


    Heavens, don't use /pointers/ for this. I'd keep anything with sharp edges
    away from /my/ shorts.

    --
    Chris "summer has just turned to rain" Dollin
    "Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
     
    Chris Dollin, Jun 19, 2006
    #2
    1. Advertising

  3. Partho

    Ian Collins Guest

    Partho wrote:
    > I have a float variable which I need to add to a short variable. How do
    > I do this? Do I have to typecast or is there a way around? I tried
    > typecasting the float to a short, but that gives me a 0 or +/-1, which
    > is unacceptable.
    >
    > Any pointers to how to get over this?
    >

    Just assign it, assuming it's in the range of your short.

    --
    Ian Collins.
     
    Ian Collins, Jun 19, 2006
    #3
  4. "Partho" <> writes:
    > I have a float variable which I need to add to a short variable. How do
    > I do this? Do I have to typecast or is there a way around? I tried
    > typecasting the float to a short, but that gives me a 0 or +/-1, which
    > is unacceptable.


    Yes, casting (not "typecasting") should do the trick. For example:

    float f = 123.0;
    short s = 42;
    short t;
    t = s + (short)f;
    /* or */
    t = s = (int)f;

    (The latter is equivalent, since values of type short are promoted to
    int anyway.)

    You say the conversion is giving you 0 or +/-1. What float value are
    you starting with?

    (Also, it's usually better to use double rather than float.)


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jun 19, 2006
    #4
  5. Partho

    pete Guest

    Partho wrote:
    >
    > I have a float variable which I need to add to a short variable.
    > How do
    > I do this? Do I have to typecast or is there a way around? I tried
    > typecasting the float to a short, but that gives me a 0 or +/-1, which
    > is unacceptable.
    >
    > Any pointers to how to get over this?


    It depends on what the type of the result of the addition
    is supposed to be.

    (short)42 + 42.42f is 84
    (short)42 + 42.42f is 84.419998

    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    float fv = 42.42f;
    short sv = 42;

    printf("(short)42 + 42.42f is %hd\n", (short)(sv + fv));
    printf("(short)42 + 42.42f is %f\n", sv + fv);
    return 0;
    }

    /* END new.c */


    --
    pete
     
    pete, Jun 19, 2006
    #5
  6. Partho

    John Smith Guest

    Partho wrote:
    > I have a float variable which I need to add to a short variable. How do
    > I do this? Do I have to typecast or is there a way around? I tried
    > typecasting the float to a short, but that gives me a 0 or +/-1, which
    > is unacceptable.
    >
    > Any pointers to how to get over this?
    >


    No cast is required in the simplest case. The short is
    implicitily converted to a float when the two types are combined
    in an expression. Note use of %f to display the result.

    #include <stdio.h>

    int main(void)
    {
    short a = 55;
    float b = 12.6;

    printf("%f\n", a + b);

    return 0;
    }
     
    John Smith, Jun 19, 2006
    #6
  7. Partho

    Partho Guest

    Hi all:

    I tried this:

    since a float is a 32 bit number, and is of the form 1.15, for ex., the
    simplest technique is to scale the float by 2^15, and then type cast to
    short:

    short s = (short) (f * 2^15);

    this puts the lower 16 bits of the float into the short var.

    correct me if I am wrong.




    John Smith wrote:
    > Partho wrote:
    > > I have a float variable which I need to add to a short variable. How do
    > > I do this? Do I have to typecast or is there a way around? I tried
    > > typecasting the float to a short, but that gives me a 0 or +/-1, which
    > > is unacceptable.
    > >
    > > Any pointers to how to get over this?
    > >

    >
    > No cast is required in the simplest case. The short is
    > implicitily converted to a float when the two types are combined
    > in an expression. Note use of %f to display the result.
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > short a = 55;
    > float b = 12.6;
    >
    > printf("%f\n", a + b);
    >
    > return 0;
    > }
     
    Partho, Jun 20, 2006
    #7
  8. "Partho" <> writes:
    > I tried this:
    >
    > since a float is a 32 bit number, and is of the form 1.15, for ex., the
    > simplest technique is to scale the float by 2^15, and then type cast to
    > short:
    >
    > short s = (short) (f * 2^15);
    >
    > this puts the lower 16 bits of the float into the short var.
    >
    > correct me if I am wrong.


    Please don't top-post. See <http://www.caliburn.nl/topposting.html>.

    Yes, you're wrong. For starters, "^" is the bitwise xor operator in
    C; 2^15 is 13, not 32767. C has no exponentiation operator. (One way
    to represent 2**15 is 1<<15; I'm using "**" here as an exponentiation
    operator, though it doesn't exist in C.)

    Your original question was:

    | I have a float variable which I need to add to a short variable. How do
    | I do this? Do I have to typecast or is there a way around? I tried
    | typecasting the float to a short, but that gives me a 0 or +/-1, which
    | is unacceptable.

    What exactly do you mean by "of the form 1.15"?

    This business about scaling by 2**15 is either new information that
    you should have told us about in the first place, or a considerable
    misunderstanding on your part.

    Based on your original question, we assumed that, for example, you had
    a short variable with a value of 5, and a float variable with a value
    of 10.0, and you wanted to add them to yield a short value of 15 (or
    whatever actual values you happen to have). If so, don't worry about
    how the float is represented internally; it's just a number. A cast
    (an explicit conversion) on a number merely converts the number to
    another type with the same value (with truncation in the case of
    floating-to-integer).

    If at all possible, show us some actual code that illustrates your
    problem, ideally a small, complete, self-contained program that we can
    compile and run ourselves. Have it display the actual values of your
    short and float variables (use printf with "%d" for short, "%g" for
    float).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jun 20, 2006
    #8
    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. Andy
    Replies:
    7
    Views:
    6,332
    Roedy Green
    May 10, 2004
  2. bd
    Replies:
    0
    Views:
    669
  3. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    596
    Keith Thompson
    Jan 11, 2007
  4. Replies:
    4
    Views:
    867
    Kaz Kylheku
    Oct 17, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,655
    James Kanze
    Oct 8, 2009
Loading...

Share This Page