Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   code efficiency (http://www.chiefdelphi.com/forums/showthread.php?t=34552)

Anton 13-02-2005 12:19

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

Alan Anderson 13-02-2005 12:31

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).

JJG13 13-02-2005 13:24

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.

Anton 13-02-2005 14:55

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 :p :p :p
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... )

Anton 13-02-2005 15:24

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...


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

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