Go to Post Even though others have had success, I will be honest, this method sort of creeps me out. - Joe Johnson [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-02-2005, 12:19
Anton's Avatar
Anton Anton is offline
Anton Povzner
#1581 (Element)
Team Role: Programmer
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Israel
Posts: 51
Anton can only hope to improve
code efficiency

I've got a little problem: as a part of the joystick program i need to use Pitagoras'es formula, which means square root/ each time iv'e tried using it I got an error, so I thought of a lookup table. the prolem is that the lookup table will b 65,000 values long.
whar will be better- using the function, the big tble, or mabe using some simple trigonometry which requires just one lookup table 90 values long?
help please.

also- if you could send me a picture of the three alibration colors- red, green and yellow- it'd be really wonderful for my teams- we're Israelis, and to buy a paint color we need to choose it manually- not by its colorants.
thanks in advanse
  #2   Spotlight this post!  
Unread 13-02-2005, 12:31
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: code efficiency

Quote:
Originally Posted by Anton
I've got a little problem: as a part of the joystick program i need to use Pitagoras'es formula, which means square root...
What do you need to do with the result? If you're just comparing it to something, you can instead compare to the square of that something and avoid taking the square root altogether.

If the result is going to be used as an integer, you can use a much smaller lookup table. Since many inputs will yield the same output, you can actually make a table of squares and search through it (using a binary search will keep it short).
  #3   Spotlight this post!  
Unread 13-02-2005, 13:24
JJG13's Avatar
JJG13 JJG13 is offline
Yoda
AKA: Joshua Graffman
None #0180 (SPAM)
Team Role: Alumni
 
Join Date: Dec 2002
Rookie Year: 2003
Location: Martin County, Florida
Posts: 105
JJG13 has a spectacular aura aboutJJG13 has a spectacular aura aboutJJG13 has a spectacular aura about
Re: code efficiency

If you need a square root function you can use this:

Code:
int intRoot(unsigned int value)
{
  unsigned int result = 0;
  unsigned int multiplier;
  unsigned int backup;
  int loopint;
  for (loopint = 7; loopint >= 0; loopint--)
  {
	 multiplier = 1 << loopint;
	 backup = result;
	 result |= multiplier;
	 if (result * result > value)
		result = backup;
  }
  if (value - result * result > abs(value - ((result + 1) * (result + 1))))
	 result++;
  return (int)result;
}
Note: this will only work with integers and will give you (the closest) integer aproximation of the square root of a number.
__________________
Behold the power of SPAM.
  #4   Spotlight this post!  
Unread 13-02-2005, 14:55
Anton's Avatar
Anton Anton is offline
Anton Povzner
#1581 (Element)
Team Role: Programmer
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Israel
Posts: 51
Anton can only hope to improve
Talking Re: code efficiency

hry thanks!
acualy, i've just replaced my 65,000 table with one just 520 valuse long, but it was still too much for my bot
thanks again
BTW- your code has just sved me from an nervous breakdown))))))))))))
my team has just discovered that our arm won't rise... and I don't seem to care at all... surely it's just all these sleepless nights trying to get me...(need sleepy smiely... )

Last edited by Anton : 13-02-2005 at 15:04.
  #5   Spotlight this post!  
Unread 13-02-2005, 15:24
Anton's Avatar
Anton Anton is offline
Anton Povzner
#1581 (Element)
Team Role: Programmer
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Israel
Posts: 51
Anton can only hope to improve
general question

another thing: what is better, generaly, a lookup table or just a function?
i know it's a bit too general, but still...
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
Example gyro code released. Kevin Watson Programming 60 17-03-2005 18:32
Updated: Serial Port Driver Code Kevin Watson Programming 4 05-02-2005 18:39
Team THRUST - Kevin's Code and Camera Code Combine Chris_Elston Programming 3 31-01-2005 22:28
Sourceforge for Code Repository and other stuff SilverStar Programming 9 15-01-2005 21:16
heres the code. y this not working omega Programming 16 31-03-2004 15:18


All times are GMT -5. The time now is 19:17.

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