|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Multiplication in EasyC returns insane values!
Does anyone else get crazy values when you try to multiply in EasyC?
46 * 10 = -52 466 * 100 = -18936 Any idea's why this is happening? I tried these math operations on both FRC and Vex controllers. TIA, Nathan |
|
#2
|
||||
|
||||
|
Re: Multiplication in EasyC returns insane values!
It sounds like you're running into variable overflow. If you're using a signed char as a data type, you only have eight bits to work with, so your value can't exceed 127 or -128. As such, when you multiply 46 * 10, you get 460, which is too large, so it wraps around. Wikipedia explains it more fully than I can:
http://en.wikipedia.org/wiki/Integer_overflow |
|
#3
|
|||||
|
|||||
|
Re: Multiplication in EasyC returns insane values!
jgannon beat me to it because I had to go and verify, but yes. -52 is exactly what you get if you try to stuff 460 into a signed char. Your output variable needs to be a signed int to handle that kind of number. the inputs could still be signed chars, I think, as the PIC has an 8 x 8 hardware multiplier that returns a 16-bit answer.
|
|
#4
|
||||
|
||||
|
Re: Multiplication in EasyC returns insane values!
That's what I thought I did at first, but I already had it as an INT. I tried doing the math operations using DOUBLEs as well, but no difference...
Still stuck here, Nathan |
|
#5
|
||||||
|
||||||
|
Re: Multiplication in EasyC returns insane values!
Quote:
Quote:
I don't use easyC, so I don't know if it possible to cast or change compiler options, but hopefully that gives you an idea where to look. For the second example, use an unsigned int (if unsigned is OK) or long if you need to maintain signs, since you are overflowing a signed int. |
|
#6
|
||||
|
||||
|
Re: Multiplication in EasyC returns insane values!
Thanks for the reply!! I casted my first example like so:
Code:
int temp = 0; temp = (int)46 * (int)10; PrintToScreen ( "%d\n" , (int)temp ); I tried this code however: Code:
long temp = 0; temp = (long)466 * (long)100; PrintToScreen ( "%ld\n" , (long)temp ) ; Thanks for all your help, Nathan |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| pic: Pat returns to ref | petek | Extra Discussion | 2 | 19-03-2006 16:33 |
| 696 Returns to Phoenix! | sanddrag | Regional Competitions | 9 | 24-02-2006 13:40 |
| HyperRules Returns! | Nate Smith | Announcements | 0 | 04-02-2003 12:19 |
| Large Multiplication.. | randomperson | Programming | 7 | 31-12-2002 19:29 |
| Are you InSaNe? | SuperJake | Chit-Chat | 6 | 16-01-2002 19:09 |