Go to Post Note to self: Go to IRI sometime. - [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 08-02-2007, 17:14
flippo flippo is offline
Registered User
FRC #1701
 
Join Date: Nov 2006
Location: Detroit, MI
Posts: 2
flippo is an unknown quantity at this point
Question Floating point math zero

I searched through the forum and could not find a similar topic, so:

I have timed my code and determined that I can use floating point math, however when I attempt to use the standard library:

float sqrt(float);

function it returns "zero" (the actual zero value not random numbers) randomly when my input is at a constant value. I am also recieving similar problems from other floating point trig functions. Can anyone help, or has anyone experienced similar problems?

Here is the actual function that is calling the sqrt function.

Quote:
void handleDriveJoystick(void) {
unsigned int radius;
signed int prettyJoyX, prettyJoyY;

//take care of the joysticks
prettyJoyX = (signed int)DRIVER_JOY_X - 127;
prettyJoyY = (signed int)DRIVER_JOY_Y - 127;

if (DRIVER_JOY_X == 127 && DRIVER_JOY_Y == 127)
theta = 0.;

else
theta = atan2((float)prettyJoyX, (float)prettyJoyY);

radius = (int) (10.0 * sqrt((float)(prettyJoyX*prettyJoyX + prettyJoyY*prettyJoyY)));

[...]

printf("Joy_Y: %u Radius %u ", p1_y, radius);
The omitted code does not reference radius at any point.

Thank you for any and all help.
  #2   Spotlight this post!  
Unread 09-02-2007, 00:08
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,078
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Floating point math zero

At first I suspected that the problem had to do with integer overflow at this step:

(float)(prettyJoyX*prettyJoyX + prettyJoyY*prettyJoyY)

However, assuming prettyJoyX and prettyJoyY are bounded at +/- 127, this wouldn't cause an overflow. Obviously, it also would never be negative. If it isn't the case that those variables are bounded, then it could be possible that you are overflowing, causing the data to wrap around to a negative number and invalidating "sqrt".

I will continue investigating.

For what values of prettyJoyX and prettyJoyY do you get zero?
  #3   Spotlight this post!  
Unread 09-02-2007, 00:32
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,078
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Floating point math zero

One other possibility:

I'm just going on a hunch, but do you get the problem when you are tethered, or only when on radio? Try it both ways. Dropped OI packets could screw up your calculation.
  #4   Spotlight this post!  
Unread 09-02-2007, 09:00
charrisTTI charrisTTI is offline
Ramblin' Wreck
AKA: Charles Harris
FRC #0623
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2003
Location: Vienna, VA
Posts: 106
charrisTTI has a spectacular aura aboutcharrisTTI has a spectacular aura about
Send a message via AIM to charrisTTI
Re: Floating point math zero

You commented that the problem is occuring with other math functions as well. Are you using any floating point math in an interrupt service routine? If so are you saving the math data context?

Below is exceprt from the C18 users guide:



If an interrupt service routine uses math library functions or calls a function that
returns 24- or 32-bit data, the math data section (which is named MATH_DATA) should
be saved using a save=section("MATH_DATA") qualifier on the interrupt pragma
directive. For example:
#pragma interrupt high_interrupt_service_routine save=section("MATH_DATA")



If this is not done then an interrupt could be trashing the results of floating point operations in the main line code.
__________________
FRC 623 2003,2004,2005,2006,2007,2008, 2009, 2010, 2011
FRC 1900 2007
FVC 60 and 193 2006
FVC 3271 2007
FTC 226 and 369 2008, 2009, 2010, 2011
FTC 3806 2010
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
Floating point math in PBASIC John Gutmann Programming 2 26-12-2005 20:07
Converting floating point to integers logicalhippo Programming 3 29-01-2005 12:10
OI and analog: Zero value Astronouth7303 Programming 1 02-02-2004 16:00
Community Zero archiver 2001 1 24-06-2002 04:08
Zero Hour archiver 2000 4 23-06-2002 21:59


All times are GMT -5. The time now is 02:58.

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