Go to Post Practice. Don't leave home without it. - JesseK [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: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
  #2   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.
  #3   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