Go to Post I think I speak to everyone who ever attended nationals there when I say...say it with me..."Please stand clear of the doors. Por Favor Metengan se alijados de las puertas." - Justin [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 13-01-2008, 14:07
Nathan's Avatar
Nathan Nathan is offline
Registered User
FRC #1501 (Team T.H.R.U.S.T.)
Team Role: Alumni
 
Join Date: Sep 2006
Rookie Year: 2007
Location: United States
Posts: 149
Nathan has a spectacular aura aboutNathan has a spectacular aura aboutNathan has a spectacular aura about
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
__________________
Save money with PriceWombat, a shopping comparison and price tracking tool (that I made!).

Check out my hobby electronics business!

My former robotics team: Huntington County 4-H Robotics
  #2   Spotlight this post!  
Unread 13-01-2008, 14:13
jgannon's Avatar
jgannon jgannon is offline
I ᐸ3 Robots
AKA: Joey Gannon
no team
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Pittsburgh, PA
Posts: 1,467
jgannon has a reputation beyond reputejgannon has a reputation beyond reputejgannon has a reputation beyond reputejgannon has a reputation beyond reputejgannon has a reputation beyond reputejgannon has a reputation beyond reputejgannon has a reputation beyond reputejgannon has a reputation beyond reputejgannon has a reputation beyond reputejgannon has a reputation beyond reputejgannon has a reputation beyond repute
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
__________________
Team 1743 - The Short Circuits
2010 Pittsburgh Excellence in Design & Team Spirit Awards
2009 Pittsburgh Regional Champions (thanks to 222 and 1218)
2007 Pittsburgh Website Award
2006 Pittsburgh Regional Champions (thanks to 395 and 1038)
2006 Pittsburgh Rookie Inspiration & Highest Rookie Seed

Team 1388 - Eagle Robotics
2005 Sacramento Engineering Inspiration
2004 Curie Division Champions (thanks to 1038 and 175)
2004 Sacramento Rookie All-Star

_
  #3   Spotlight this post!  
Unread 13-01-2008, 14:17
Kevin Sevcik's Avatar
Kevin Sevcik Kevin Sevcik is offline
(Insert witty comment here)
FRC #0057 (The Leopards)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Houston, Texas
Posts: 3,673
Kevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond repute
Send a message via AIM to Kevin Sevcik Send a message via Yahoo to Kevin Sevcik
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.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.

Lone Star Regional Troubleshooter
  #4   Spotlight this post!  
Unread 13-01-2008, 14:18
Nathan's Avatar
Nathan Nathan is offline
Registered User
FRC #1501 (Team T.H.R.U.S.T.)
Team Role: Alumni
 
Join Date: Sep 2006
Rookie Year: 2007
Location: United States
Posts: 149
Nathan has a spectacular aura aboutNathan has a spectacular aura aboutNathan has a spectacular aura about
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
__________________
Save money with PriceWombat, a shopping comparison and price tracking tool (that I made!).

Check out my hobby electronics business!

My former robotics team: Huntington County 4-H Robotics
  #5   Spotlight this post!  
Unread 13-01-2008, 14:50
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,567
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Multiplication in EasyC returns insane values!

Quote:
Originally Posted by Nathan View Post
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
This statement would work in Visual Studio or another ISO/ANSI compliant compiler. The Microchip C18 compiler (which easy C uses at the back end) is not ANSI compliant. The particular deviation you are running into is described in the C18 compiler user's guide:

Quote:
2.7.1 Integer Promotions
ISO mandates that all arithmetic be performed at int precision or greater. By default,
MPLAB C18 will perform arithmetic at the size of the largest operand, even if both
operands are smaller than an int. The ISO mandated behavior can be instated via the
-Oi command-line option.

For example:

Code:
unsigned char a, b;
unsigned i;
a = b = 0x80;
i = a + b; /* ISO requires that i == 0x100, but in C18 i == 0 */
Note that this divergence also applies to constant literals. The chosen type for constant
literals is the first one from the appropriate group that can represent the value of the
constant without overflow.

For example:

Code:
#define A 0x10 /* A will be considered a char unless -Oi
                  specified */
#define B 0x10 /* B will be considered a char unless -Oi
                  specified */
#define C (A) * (B)
unsigned i;
i = C; /* ISO requires that i == 0x100, but in C18 i == 0 */
This explains the problem with your first example because even though you put the final value as an integer, C18 calculates the result as an 8 bit value, since both operands are 8 bit values. One way to fix this is to cast one of the operands as an int, or to use the -Oi compiler option

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   Spotlight this post!  
Unread 13-01-2008, 16:48
Nathan's Avatar
Nathan Nathan is offline
Registered User
FRC #1501 (Team T.H.R.U.S.T.)
Team Role: Alumni
 
Join Date: Sep 2006
Rookie Year: 2007
Location: United States
Posts: 149
Nathan has a spectacular aura aboutNathan has a spectacular aura aboutNathan has a spectacular aura about
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 );
The printed value is 460, so it works!

I tried this code however:
Code:
long temp = 0;
temp = (long)466 * (long)100;
PrintToScreen ( "%ld\n" , (long)temp ) ;
And "0b608" prints out. I'm assuming the value is in hex, which is kind of annoying. Did I write the print to screen parameters incorrectly?

Thanks for all your help,
Nathan
__________________
Save money with PriceWombat, a shopping comparison and price tracking tool (that I made!).

Check out my hobby electronics business!

My former robotics team: Huntington County 4-H Robotics
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 23:12.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi