New to C Please help !
Posted: Wed Jun 23, 2010 6:28 pm
This will probably have you all laughing but I am stuck ....
I need to do a calculation that needs long integers. It also involves a Flowcode variable I will call TESTNO.
Now TESTNO is a defined variable in me Flowcode BUT it is only an integer (16 bit)
TESTNO can never be any larger than 9999.
What I need to do is multiply TESTNO by a 3192 and then divide by 100000. As you will see the result will always fit
into a 16 bits. However the arithmetics dictate we need more that 16 bits ... hence the need for long.
I note this cannot be done directly in Flowcode, so my C code box is as follows....
unsigned long int var;
var = (FCV_TESTNO * 3192)/100000;
FCV_TESTNO = var;
No matter what the value of TESTNO is the result is always 0. Can you tell me what I am doing wrong ... I have a funny feeling
it has something to do with the last line where I am reading a 32 bit result into a 16 bit variable ??? .... how are you meant to do this ??
Sorry
Iain
Now tried this ...
unsigned long int var;
var = (100000 * 3129)/100000;
FCV_REACTION_TIME = (unsigned short int) var;
This returns an answer of 3192 (great), but if I now change it to ....
unsigned long int var;
var = (10000 * 3129)/10000;
FCV_REACTION_TIME = (unsigned short int) var;
I get an answer of 2 !!!!
Where am I going wrong ?
I need to do a calculation that needs long integers. It also involves a Flowcode variable I will call TESTNO.
Now TESTNO is a defined variable in me Flowcode BUT it is only an integer (16 bit)
TESTNO can never be any larger than 9999.
What I need to do is multiply TESTNO by a 3192 and then divide by 100000. As you will see the result will always fit
into a 16 bits. However the arithmetics dictate we need more that 16 bits ... hence the need for long.
I note this cannot be done directly in Flowcode, so my C code box is as follows....
unsigned long int var;
var = (FCV_TESTNO * 3192)/100000;
FCV_TESTNO = var;
No matter what the value of TESTNO is the result is always 0. Can you tell me what I am doing wrong ... I have a funny feeling
it has something to do with the last line where I am reading a 32 bit result into a 16 bit variable ??? .... how are you meant to do this ??
Sorry
Iain
Now tried this ...
unsigned long int var;
var = (100000 * 3129)/100000;
FCV_REACTION_TIME = (unsigned short int) var;
This returns an answer of 3192 (great), but if I now change it to ....
unsigned long int var;
var = (10000 * 3129)/10000;
FCV_REACTION_TIME = (unsigned short int) var;
I get an answer of 2 !!!!
Where am I going wrong ?