CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

how to convert float to int16?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ronnarone.r



Joined: 23 Dec 2008
Posts: 12

View user's profile Send private message

how to convert float to int16?
PostPosted: Sat Mar 13, 2010 6:44 am     Reply with quote

I want to convert float to int16. How to?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sat Mar 13, 2010 7:19 am     Reply with quote

What is wrong with the obvious:

long L;
float F = 12345.6;

L = F;

Should leave L = 12345
_________________
The search for better is endless. Instead simply find very good and get the job done.
Gavin Pinto



Joined: 18 Oct 2009
Posts: 27
Location: Australia

View user's profile Send private message

PostPosted: Sun Mar 14, 2010 9:55 pm     Reply with quote

SherpaDoug wrote:
What is wrong with the obvious:

long L;
float F = 12345.6;

L = F;

Should leave L = 12345


int16 i;

Cast it

i=(int16)F;
_________________
Clear Logic
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Mon Mar 15, 2010 3:42 am     Reply with quote

Both Sherpa Dog's, and Gavin Pinto's answers, do exactly the same thing.
C peforms an automatic 'cast', when variables are assigned to other variables of different types.
Where they would differ, is if you do something 'with' the value on the right of the equation. The explicit cast as shown by Gavin, converts the value 'then and there', into a value of the lower type. The automatic cast, does it at the time the value is put into the variable. Example of the difference:
Code:

float fpval=1234.56;
int16 ival;

ival=fpval; //ival=1234
ival=(int16)fpval; //ival=1234

ival=fpval*2; //ival=2469 - maths is performed using fp arithmetic,
//since 'fpval' is a float value. 1234.56*2 = 2469.12.

ival=(int16)fpval*2; //ival=2468 - the _integer_ 1234, is multipled by two
//using integer arithmetic (faster), giving 2468.....


Best Wishes
Gavin Pinto



Joined: 18 Oct 2009
Posts: 27
Location: Australia

View user's profile Send private message

PostPosted: Mon Mar 15, 2010 6:09 pm     Reply with quote

In addition I will show how float is stored. It is stored with 32 bits in memory.

Now look at my demonstration below

Example 1

int buff[10];

int32 *fp;
float F =123.45;
fp = &F;
itoa(*fp,16,buff);
puts(buff);

on hyterminal you will see

66E67685

This is bit pattern in memory.

23 bit mantissa, sign bit, 8 bit exponent with bias of 7F

Example 2

int buff[10];

int32 *fp;
float F =123.45E20;
fp = &F;
itoa(*fp,16,buff);
puts(buff);

on hyterminal you will see

534E27C8

This is bit pattern in memory.

23 bit mantissa, sign bit, 8 bit exponent with bias of 7F



Best wishes
_________________
Clear Logic
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group