angle and radian. accuracy appr. 4xe-4, but good speed.

Started by konan, May 04, 2011, 08:04:46 AM

Previous topic - Next topic

konan

   Re: sine approx. using sse2
« Reply #7 on: May 16, 2010, 11:19:41 pm »
   Reply with quote
Quote from: jj2007 on May 16, 2010, 11:04:30 pm
... speed vs accuracy.
well, I simply need it because I'm currently working on my easy-to-use-math-macros with SSE2 support. Using fpu instruction in interaction with sse2 instruction would simply take the speed advantage. Also in in many situation (especialy when using SIMD) high precision is not needed.
http://www.masm32.com/board/index.php?topic=14012.0
**********************************************************
One month ago i have derived formula without Taylor function. You needed only cathet and hypothenus and you will have radian and angle. (actually, this is cosine, but sine you easily can have, see attachments.). very fast and you don't need a lot of memory, as with Taylor...
In attachments you will have all (almost) sequences.
adress is here: http://www.nkj.ru/forum/forum25/topic15698/messages/?PAGEN_1=2
That is in Russian (picture in English), but understandable (formulas i mean). :-)
If it's can be helpful for anybody, i will be glad. :-)

alpha = 120*sqrt{(0,559-0,111*k/h)*(1-k/h)}

k - adjacent cathet
h - hypotenuse

Would be interested any opinions. preferably good one, if possible. :-) (do not tear too much, if any...)
second attachment is the picture in exel. (from site).

qWord

hi,
I've found a very good book about this topic:
Math Toolkit for Real-Time Development, Jack W. Crenshaw, CMP Books

The fact that you can get the arccos and arcsin from the arctan, you can just use this algo (from the book). It is the (nearly) same algo, but it limits the input values:
/* Rational approximation for atan(x)
* This polynomial uses a minimaxed 5th-order
* ratio of polynomials. Input angles are restricted
* to values no greater than 15 degrees. Accuracy is
* better than 4e-8 over the full range of inputs.
*/
double arctan(double x){

// constants for segmentation
static const double b = pi/6;
static const double k = tan(b);
static const double b0 = pi/12;
static const double k0 = tan(b0);

// constants for rational polynomial
static const double A = 0.999999020228907;
static const double B = 0.257977658811405;
static const double C = 0.59120450521312;
double ang;
double x2;
int comp = FALSE;
int hi_seg = FALSE;

// make argument positive
int sign = (x < 0);
x = abs(x);

// limit argument to 0..1
if(x > 1){
    comp = TRUE;
    x = 1/x;
}

// determine segmentation
if(x > k0){
    hi_seg = TRUE;
    x = (x - k)/(1 + k*x);
}
/* argument is now < tan(15 degrees)
* approximate the function
*/
x2 = x * x;
ang = x*(A + B*x2)/(1 + C*x2);

// now restore offset if needed
if(hi_seg)
    ang += b;

// restore complement if needed
if(comp)
    ang = halfpi - ang;

// restore sign if needed
if(sign)
    return -ang;
else
    return ang;
}


EDIT: a quick compare:
k = 1
h = 2
120*sqrt{(0,559-0,111*k/h)*(1-k/h)}   ==> 60.209633°
acos(k/h)                             ==> 60°

FPU in a trice: SmplMath
It's that simple!

konan

Quote from: qWord
hi,
I've found a very good book about this topic:
Math Toolkit for Real-Time Development, Jack W. Crenshaw, CMP Books

The fact that you can get the arccos and arcsin from the arctan, you can just use this algo (from the book). It is the (nearly) same algo, but it limits the input values:
* Rational approximation for atan(x)
* This polynomial uses a minimaxed 5th-order
* ratio of polynomials. Input angles are restricted
* to values no greater than 15 degrees
. Accuracy is
* better than 4e-8 over the full range of inputs.
*/

EDIT: a quick compare:
k = 1
h = 2
120*sqrt{(0,559-0,111*k/h)*(1-k/h)}   ==> 60.209633°
acos(k/h)                             ==> 60°


Hi.
That is right. But if you make restriction with 15 degrees (as your example), then you need just to "adjust" the bold one here -  0,559. This formula from 0 to 90 degrees (and 0,26  - this is minimal accuracy (your example 0,209633 - but why you did not take 30 degrees ;-))) in all region.
With narrowed angle the accuracy is about e-5 (can be more, for sure). (and in this case you wining the speed, and memory (at least for small processors), and enough accuracy. (in case if you needed only 15 degree or even 30 degree limitations).
And all constant what you needed is only one - pi.
But any way, this formula has been done only because i wanted to find radian and angle by simple calculation with adjacent cathet and hypotenuse only. Other words - you needed only two lines and you know already the radian and angle. (one month ago the simple formula like this was not existing. :-)).

ps: and this was the proposition only. (but useful one. :-)))