## Large multiplication

Any general or miscellaneous queries that do not fit into the other forum catagories

Moderators: Benj, Mods

henker
Posts: 39
Joined: Mon Dec 22, 2008 11:22 pm
Has thanked: 7 times
Been thanked: 2 times
Contact:

### Large multiplication

Hello all,

I'm somewhat in a jam.

I have a multiplication to do however it only works partially:
T2 = 11 * (C6 +24) * (200 - TEMP) * (200 - TEMP) / 2^20

I have made a C Code box with the following lines:

long t2;
t2 = 11 * (FCV_C6 + 24); This parts is alwas 561
t2 = t2 * (200-FCV_TEMP); TEMP can be anywhere from -150 to 199
t2 = t2 * (200-FCV_TEMP);
t2 = t2 >> 20;
FCV_T2 = t2;

The problem is that the calculation works untill TEMP = -55. As soon as the TEMP is lower as -55 the T2 goes to 0

My question is; Can a PIC18F4685 actally handly such a calculation?
If it does, what am I doing wrong?

Thanks
Henk

DavidA
Matrix Staff
Posts: 1060
Joined: Fri Apr 23, 2010 2:18 pm
Location: Matrix Multimedia Ltd
Has thanked: 58 times
Been thanked: 275 times
Contact:

### Re: Large multiplication

Hello,

I would imagine that you are using a BYTE to store one of your variables, the maximum value a byte can hold is 255. Try changing them to integers. If this isnt the case, attach your fcf file to the post and ill take a look at it.
These users thanked the author DavidA for the post:
henker (Wed Jun 01, 2011 1:11 am)
Rating: 5.26%

Steve
Matrix Staff
Posts: 3080
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 88 times
Been thanked: 391 times
Contact:

### Re: Large multiplication

Code: Select all

`long t2;t2 = 11 * (FCV_C6 + 24);t2 = t2 * (long)(200-FCV_TEMP);t2 = t2 * (long)(200-FCV_TEMP); t2 = t2 >> 20;FCV_T2 = t2;`
These users thanked the author Steve for the post:
henker (Wed Jun 01, 2011 1:10 am)
Rating: 5.26%

henker
Posts: 39
Joined: Mon Dec 22, 2008 11:22 pm
Has thanked: 7 times
Been thanked: 2 times
Contact:

### Re: Large multiplication

Thanks Guys,

I'm using all INT so that shouldn't be a problem.

Thanks for the help.

Henk

henker
Posts: 39
Joined: Mon Dec 22, 2008 11:22 pm
Has thanked: 7 times
Been thanked: 2 times
Contact:

### Re: Large multiplication

OK, I got it working.
This is the solution:

t2 = 11 * (FCV_C6 + 24);
t2 = t2 * ((long) 200-FCV_TEMP);
t2 = t2 * ((long) 200-FCV_TEMP);
t2 = t2 >> 20;
FCV_T2 = t2;

Thanks for bringing me in the right direction.

Henk

Steve
Matrix Staff
Posts: 3080
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 88 times
Been thanked: 391 times
Contact:

### Re: Large multiplication

Hi Henk,

Glad you got it working. Instead of "long", you can probably get away with "int" or "signed int" (this may save a small amount of processing and variable space if that's important to you).