 |  |  |  | | | Hmm.. is tis present in Hacker's delight |
|  | | | I'm intrigued... How did you come about that formula? |
|  | | made them from scratch.
the first one, was derived from "abs(x)+abs(y)"
to simply come up with something that is more accurate, on an integer based system, with limited isntruction set(eg. z80, C64, etc.)
the second is similarly derived from the first, but using some complex instructions (multiply/divide)..
the idea for them basically stemed form the desire to do complex math functions, on weak processors, and then rezlizing, that the results of such functions, need not be exact.
eg. the length of (3,4) is exactly 5.
but the length of say.. (7,9) will be 11: which actually has an error of 3.65% in itself, just due to the raw nature of rounding error in using integers.
then with doing the same types of things on complex systems(eg. physics for a game), optimization remained a concern, but more complex instructions were available. and accuracy became more important. |
|  | | | The second function is quite a bit more complicated than the first. How did you derive it? |
|  | | a=a+0.42f*b*b/a;
is similar to the original a=a+0.5*b
but 0.41..(sqrt(2)-1) yields the correct result for the limit of a=b
then b*b/a isntead of plin b: matches the curve much more closely
and then the last step:
return (a+(x*x+y*y)/a)/2;
simply average's the value, with it's recipricol,
ie. using the fact that |sqrt(n)-a| >=
|sqrt(n)-(a+n/a)/2|
clearly neither of these functions are fully optimized, or even provide the optimal accuracy within their formula.
but they are simple to read, and easy to modify.
I've done similar for sin, tan, log, other roots, random number generators, etc.
for performance, the best gains are arrived, not by refining the formula, but by changing the formula to come up with results that are similar enough to do what you want. |
|  | | |  | | That's clever!
I'm not sure how to adapt this method to other functions, but I'm seriously impressed, nonetheless. |
|  | | if there's any particular function you'd like to see something like this for, just let me know.
as long as you can describe what the function is mathematically(even if you don't have the actual formula), and have a specific example of it's intended use, I can probably come up with something that should be fast enough and/or accurate enough. |
|  | | | I wouldn't mind seeing what other functions you've come up with. I'd be interested to see how you came up with log approximation. |
|  | | |  |  |  |  | Anonymously add a comment: (or register here) (registration is really fast and we send you no spam)
|