Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Question to the C specialists

Status
Not open for further replies.

minoss

Junior Member level 1
Junior Member level 1
Joined
May 15, 2003
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
germany
Activity points
147
Hello

I've a question to the C specialists. I can't find an answer in any book.

For example: If i have a long int variable named A and I make a calculation. A = x*y; And the variables x and y are float types, the variable A will be automatically converted to float.
My question is , how long is A a float variable ??

For the whole program time , or does it fall back to long int if it is possible ?

Regards
Michael
 

I suppose that A can not be converted to float becaue it is fixed when you declare the A var . May be you are talking about format how the data will be stored in A and this will be stored in floating point format ?
From other side the length of memory to keep and its format is compiler and machine specific . Please have a look to compiler manual to find how bytes does the A occupy .

Actually good compiler must produce warning when you will try to make such assignment .

If you compiler is ANSI standard specific , have a look to ANSI standard were type conversion is specified for lvalue in assignment .

Please have a look to K&R book clause 2.7 where the type conversion is described .

As lasst instance - standard must be consulted .
 

Try to use manual (I'm not sure is this right word in english) conversion:

unsigned long A,B;
float C;

C = (float) A + (float) B
 

arturt134 said:
Try to use manual (I'm not sure is this right word in english) conversion:

unsigned long A,B;
float C;

C = (float) A + (float) B

This is also called as type casting...
Regards
 

foat and long could have different formats
and type casting can lead to data corruption .
 

float and long int HAVE different formats. Therefore type casting is necessary.
 

Type casting I assume will tell compiler that it must treat this data as of different type , but it does not (if I am not wrong ) convert data from one format to another . There are special functions in C library (have a look to 2.7 K&R book) used to convert those from one format to another .
 

float x,y;
long A;

....
A = x*y;

The code generated by the C compiler does the following:
1) calculates the product x*y as a float (using the standard floating point routine)
2) this result is converted to long by truncation (not rounding) and assigned to A. No check is performed in this conversion; in the case there is an overflow you can get a wrong result. ( If you want rounding, you need A=x*y+.5; ).
The compiler can generate a warning when performing this type of implicit conversion from float to integer types (depending on the compiler and the warning level you have set).

Regards

Z
 

I agree with friends.If you don't want data loss when type casting or converting, you should define your variables carefully.I recommend you to define all of them as float type.Then you can convert and/or do operations on it.
Cheers,

Analyzer.
 

If you define A as a long int, then it remains a long int. The multiplication is done using floating point, then the product is converted to a long int and stored into A. Here is an excerpt from the ANSI spec describing the conversion ...

6.3.1.4 Real floating and integer
When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
 

The best way to test it, is to make some tests....
I've look at the assembler source in my compiler (I'm using IAR C for AVR):

unsigned a,b;
float c,d;

c = a / b; //first fixed point divison, next conversion fixed point to floating point

d = (float) a / (float) b; //first conversion of each fixed point variable to floating point, next floting point division
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top