472,794 Members | 2,203 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

convert a float to a short

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?

Jun 19 '06 #1
7 29801
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.
Jun 19 '06 #2
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/

Jun 19 '06 #3
"Partho" <pa**************@gmail.com> 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) ks***@mib.org <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.
Jun 19 '06 #4
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
Jun 19 '06 #5
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;
}
Jun 19 '06 #6

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;
}


Jun 20 '06 #7
"Partho" <pa**************@gmail.com> 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) ks***@mib.org <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.
Jun 20 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Cally | last post by:
Hello, I would like to convert a field from ntext field found in one database table to float field found in another database table. The reason why I want to do this is a long one. I have...
4
by: jaijai_kumar | last post by:
Select Cast('100.1234' as float) give me the result 100.1234 Now when I convert it back to char I want exactly 100.1234 Select Convert(char(100),Cast('100.1234' as float)) Gives me 100.123 (Here...
4
by: music4 | last post by:
Greetings, I psinfo_t struct, pr_pctcpu is ushort_t type with comment: "pr_pctcpu is 16 bit binary fractions in the range 0.0 to 1.0 with the binary point to the right of the high-orfer bit...
12
by: Alan | last post by:
how to convert double to short ? for example, I want to convert double doubleVal1 = 15000.1; double doubleVal2 = 12000.0; short shortVal; shortVal = doubleVal1 - doubleVal2; I...
3
by: Tony Liu | last post by:
Dear All: I create a very simple DLL by using EVC to test the problem. (The platform I am working for those program is WinCE.NET) ******************************************************* The...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
8
by: avsrk | last post by:
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 ....
7
by: chelle2100 | last post by:
hi... i need to write a converter to convert the text file to a binary file with the following file format... CharacterCount:Unsigned Char(This stores the number of characters that is in the file)...
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.