View previous topic :: View next topic |
Author |
Message |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
Multiplication operator |
Posted: Sun Mar 23, 2014 2:20 am |
|
|
hi
Code: | #include <16f84a.h>
#use delay(Xtal=4Mhz)
void main ()
{
int8 ton_m=100;
int16 tonm;
tonm=(ton_m*10);
while(true)
{
output_high(pin_b7);
delay_us(tonm);
output_low(pin_b7);
delay_ms(17);
}
} |
when int8 ton_m, Multiplication operation is not performed and tonm=ton_m=100!!!
but when int16 ton_m, Multiplication operation is performed and tonm=1000;
why??? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Re: Multiplication operator |
Posted: Sun Mar 23, 2014 2:42 am |
|
|
hamid9543 wrote: | hi
Code: | #include <16f84a.h>
#use delay(Xtal=4Mhz)
void main ()
{
int8 ton_m=100;
int16 tonm;
tonm=(ton_m*10);
while(true)
{
output_high(pin_b7);
delay_us(tonm);
output_low(pin_b7);
delay_ms(17);
}
} |
when int8 ton_m , Multiplication operation is not performed and tonm=ton_m=100!!!
but when int16 ton_m ,Multiplication operation is performed and tonm=1000;
why??? |
How do you KNOW what is happenng?
Mike |
|
|
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
mull |
Posted: Sun Mar 23, 2014 3:20 am |
|
|
Connect pin_b7 to oscilloscope and measure time. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Sun Mar 23, 2014 4:04 am |
|
|
The key is to understand 'types' in C.
ton_m, is an int8. Max 255. When you perform arithmetic in C, the type used for the arithmetic, is the _highest_ of the two values being used. You have an int8 (ton_m), and another int8 (10). So int8 arithmetic is used. 100*10, then overflows the arithmetic. You should get 232 as the result (0x3E8, but only 'E8' retained).
Now, you can handle this multiple ways:
Code: |
tonm=((int16)ton_m*10);
//This converts ton_m to an int16 _before_ the arithmetic, so int16 is used
tonm=(ton_m*10L);
//This says to treat the '10' as an int16
|
Now this is how 'C' originally defined arithmetic 'should' be done, but on most 'larger' modern C's, it is not how it happens.
Reasons are that first the 'default' internal integer is an int16 on most systems. So the '10' gets assumed to be an int16, unless you specifically 'say' this is an int8. Then second, on most systems there is hardware to perform a 16bit multiplication, and this is used automatically to do the int8 multiplication, so the overflow gets implicitly handled.
However the same lesson applies with all C's. As a programmer, you need to ensure that the maths size being used, is large enough to handle the sum.
The advantage of this approach, is that it keeps code size small, since when a smaller sum is all that if needed, extra code is not added.
Multiplication _is_ being performed, but only int8 multiplication. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Re: mull |
Posted: Tue Mar 25, 2014 2:45 am |
|
|
hamid9543 wrote: | Connect pin_b7 to oscilloscope and measure time. |
Which is what I guessed you might have done, hence the question.
However, you have not measured carefully enough.
When I tried your int8 version, the result of the multiply is the expected 232.
The multiplication does take place, just as Mr T explains.
Mike |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
Re: mull |
Posted: Thu Mar 27, 2014 4:37 am |
|
|
Mike Walne wrote: | just as Mr T explains. |
10*100 don't fit in no int8 Foo
|
|
|
|