Go to Post You can learn just as much from building a small robot as you can from a large bot because all of the key concepts are the same. - Greg Needel [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 17-02-2005, 23:20
Unsung FIRST Hero
Matt Leese Matt Leese is offline
Been-In-FIRST-Too-Long
FRC #1438 (The Aztechs)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1998
Location: Long Beach, CA
Posts: 937
Matt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond repute
Send a message via AIM to Matt Leese
Nasty Compiler Bug

So the past couple weeks we've been working on our autonomous code on a test robot. Over this time we've been having problems and wracking our brains about outputs that just don't seem to make sense. I managed to track down at least one of the problems today and it appears to be a compiler bug. This is not a request for help but just trying to inform everyone else of the issue.

If you use the following statement:
Code:
long coeff_1 = 90/2*40;
coeff_1 will equal 8. Now this is obviously not the correct value (it should be 1800). For some reason, the compiler appears to do math in initalization statements (for long's at least) in a 8-bit space (1800 = 8 in an 8-bit space).

If you use this code:
Code:
long coeff_1 = 1800;
it will work correctly.

The long and the short of it is to always do any math out before putting it in the source code. While it's more work for you, it results in correctly generated code.

I'm not sure where to report this bug to Microchip so if anyone knows, please tell me.

Matt
  #2   Spotlight this post!  
Unread 18-02-2005, 00:31
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: Nasty Compiler Bug

I've noticed in general that the C18 compiler handles casting in interesting and often unexpected ways. If you explicitly cast one of those numbers as a long, it should work correctly. However, your point that you should work out as much math as you can before putting it into the code is absolutely correct, in that you'll always get the right answer, and it takes fewer processor cycles.
__________________
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 18-02-2005, 01:05
Unsung FIRST Hero
Matt Leese Matt Leese is offline
Been-In-FIRST-Too-Long
FRC #1438 (The Aztechs)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1998
Location: Long Beach, CA
Posts: 937
Matt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond repute
Send a message via AIM to Matt Leese
Re: Nasty Compiler Bug

I don't think this is a casting error. Assuming there's some optimization in the compiler, the compiler will do the math before hand (because it can) and save the cycles on the processor. However, the compiler is incorrectly calculating the output. I don't have the compiled version handy to check the assembly but I may check that tomorrow afternoon.

Matt
  #4   Spotlight this post!  
Unread 18-02-2005, 01:07
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: Nasty Compiler Bug

Quote:
Originally Posted by Matt Leese
I don't think this is a casting error. Assuming there's some optimization in the compiler, the compiler will do the math before hand (because it can) and save the cycles on the processor. However, the compiler is incorrectly calculating the output. I don't have the compiled version handy to check the assembly but I may check that tomorrow afternoon.

Matt
I wouldn't call it an error on your part, but rather an eccentricity of the compiler. I too have noticed any number of weird losses of precision or other unexpected results. I sure can't read assembly, but I'll be interested to hear what you turn up tomorrow.
__________________
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

_
  #5   Spotlight this post!  
Unread 18-02-2005, 06:29
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Re: Nasty Compiler Bug

Matt,

I believe what happens is that because 90, 2, and 45 all fit in a char (it may only look at the 90), the compiler will use that size for all its intermediate results. (I'm not saying it's right. Just that's the way it is.)

90L/2*40 should work as you expect. (90L tells the compiler that the 90 is a long constant.)
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me
  #6   Spotlight this post!  
Unread 18-02-2005, 18:44
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,580
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: Nasty Compiler Bug

It's not a bug, it's a feature

There is a compiler option to tell it to promote for intermediate steps, which is what you are expecting. Otherwise, it saves stack space and processing power by doing the calculations as chars.

AFAIK, this behavior is in violation of the ANSI standard, but is very common for embedded processors.
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
IMPORTANT regarding the new C Compiler dlavery General Forum 12 14-01-2005 05:05
Makefile and stripped down compiler Venkatesh Programming 0 09-01-2005 18:55
multiple definition error in MCC/Linker (possible bug?) wun Programming 5 01-04-2004 17:49
printf bug - beware! WizardOfAz Programming 8 10-03-2004 09:50
C compiler for 2004 Tim Skloss Technical Discussion 11 05-11-2003 22:02


All times are GMT -5. The time now is 06:16.

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