Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math: p_atan: Implement atan Function #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ FUNCTION | NOTES
[p_acos()](src/math/p_acos.c) | arc cosine
[p_acosh()](src/math/p_acosh.c) | arc hyperbolic cosine
[p_asin()](src/math/p_asin.c) | arc sine
[p_asinh()](src/math/p_asinh.c) | arc hyperbolic sine
[p_asinh()](src/math/p_asinh.c) | arc hyperbolic sine
[p_atan()](src/math/p_atan.c) | arc tan
[p_cbrt()](src/math/p_cbrt.c) | cubic root
[p_cos()](src/math/p_cos.c) | cosine
[p_cosh()](src/math/p_cosh.c) | hyperbolic cosine
Expand Down
53 changes: 28 additions & 25 deletions src/math/p_atan.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
#include <pal.h>

/*
* -1 <= x <= 1
* atan x = a1 * x + a3 * x^3 + ... + a9 * x^9 + e(x)
* |e(x)| <= 10^-5
*/
static inline float _p_atan(const float x)
{
const float a1 = 0.9998660f;
const float a3 = -0.3302995f;
const float a5 = 0.1801410f;
const float a7 = -0.0851330f;
const float a9 = 0.0208351f;
float x2 = x * x;
return x * (a1 + x2 * (a3 + x2 * (a5 + x2 * (a7 + x2 * a9))));
}

/**
*
* Calculates inverse tangent (arc tangent) of the input value. The function
* returns a value between -pi/2 to pi/2 but does not check for illegal input
* values.
* Calculates inverse tangent (arc tangent).
*
* @param a Pointer to input vector
*
Expand All @@ -31,11 +11,34 @@ static inline float _p_atan(const float x)
* @return None
*
*/

void p_atan_f32(const float *a, float *c, int n)
{
int i;int sign; float ff; float x;
float r,r2,r4,r6,r8;
float pi4 = 0.78539816339f; // constant=pi/4

for ( i = 0; i < n; i++)
{
const float *pa = (a+i);
float *pc = (c+i);
ff = *pa;
if (ff >= 0) sign = 1.0; else sign = -1.0; // store sign of input variable
if (ff < 0) x = ff * (-1.0f); else x = ff; // this is equivalent of x=abs(a)

r = (x-1)/(x+1);
r2 = r * r ; r4 = r2*r2 ; r6 = r4*r2 ; r8 = r4*r4 ;

float z = pi4
+ (+0.9999993329f*r
+r2 *(-0.3332985605f*r
+r *(+0.1994653599f*r2
+ (-0.1390853351f*r4
+ (+0.0964200441f*r6
+ (-0.0559098861f*r8
+r4 *(+0.0218612288f*r6
+ (-0.0040540580f*r8))))))));

int i;
for (i = 0; i < n; i++) {
c[i] = _p_atan(a[i]);
}
*pc = sign*z;
}
}
200 changes: 100 additions & 100 deletions tests/math/gold/p_atan_f32.dat
Original file line number Diff line number Diff line change
@@ -1,100 +1,100 @@
-0.107358,-0.049577,0.000000,-0.106948
0.051781,-0.265425,0.000000,0.051735
0.941475,-0.975603,0.000000,0.755263
-0.770979,-0.360797,0.000000,-0.656793
0.738044,-0.380252,0.000000,0.635805
0.608158,-0.123753,0.000000,0.546396
-0.066886,0.380368,0.000000,-0.066787
0.465099,0.210297,0.000000,0.435339
0.968926,0.910675,0.000000,0.769617
-0.207846,-0.573708,0.000000,-0.204928
-0.971535,-0.123731,0.000000,-0.770961
-0.782373,-0.992383,0.000000,-0.663900
0.682603,0.521601,0.000000,0.598954
-0.536897,0.615399,0.000000,-0.492728
-0.076406,-0.549893,0.000000,-0.076258
-0.683411,0.816236,0.000000,-0.599505
0.400530,0.368370,0.000000,0.380963
-0.449189,0.342005,0.000000,-0.422179
0.392768,-0.220168,0.000000,0.374256
0.981208,0.130812,0.000000,0.775913
0.399579,0.589365,0.000000,0.380143
-0.992941,-0.667307,0.000000,-0.781856
-0.030266,0.472158,0.000000,-0.030257
0.542991,-0.061340,0.000000,0.497446
0.382832,-0.664855,0.000000,0.365619
0.364952,0.411298,0.000000,0.349932
0.211414,0.582579,0.000000,0.208346
0.418914,-0.105983,0.000000,0.396704
0.104180,0.882017,0.000000,0.103806
-0.490584,-0.972226,0.000000,-0.456086
-0.667876,-0.173995,0.000000,-0.588839
0.844010,0.732653,0.000000,0.701006
-0.805625,-0.605179,0.000000,-0.678161
0.074658,0.587143,0.000000,0.074520
0.174653,0.055866,0.000000,0.172909
-0.282045,-0.425768,0.000000,-0.274904
-0.354768,-0.274987,0.000000,-0.340916
-0.093074,0.614965,0.000000,-0.092807
-0.802829,-0.550084,0.000000,-0.676464
-0.446374,0.580004,0.000000,-0.419834
-0.214939,0.918578,0.000000,-0.211718
-0.008699,0.996475,0.000000,-0.008699
0.501157,-0.589785,0.000000,0.464573
-0.109508,-0.394662,0.000000,-0.109073
-0.707768,0.399907,0.000000,-0.615920
-0.366888,-0.375644,0.000000,-0.351640
-0.774088,-0.522878,0.000000,-0.658740
-0.642991,-0.579713,0.000000,-0.571432
-0.128057,0.431668,0.000000,-0.127364
-0.992570,-0.953404,0.000000,-0.781669
-0.512466,-0.274615,0.000000,-0.473571
-0.379171,0.132765,0.000000,-0.362422
0.450398,0.527754,0.000000,0.423185
-0.252269,0.647569,0.000000,-0.247113
0.977671,0.301357,0.000000,0.774108
0.227573,-0.237268,0.000000,0.223762
0.219935,-0.781126,0.000000,0.216488
-0.240793,-0.278908,0.000000,-0.236295
-0.370910,0.649699,0.000000,-0.355180
0.326430,-0.078678,0.000000,0.315525
0.049606,0.959542,0.000000,0.049565
0.545678,0.275518,0.000000,0.499519
-0.563335,0.902687,0.000000,-0.513024
0.695806,0.308608,0.000000,0.607906
0.334355,0.703236,0.000000,0.322670
0.355204,0.821889,0.000000,0.341303
-0.571379,0.976033,0.000000,-0.519109
-0.045346,0.879019,0.000000,-0.045315
0.503788,0.702385,0.000000,0.466673
0.526589,0.481458,0.000000,0.484692
0.003741,-0.245838,0.000000,0.003741
-0.755809,-0.776324,0.000000,-0.647209
-0.026964,0.003398,0.000000,-0.026957
-0.055232,0.602126,0.000000,-0.055176
-0.346903,-0.728802,0.000000,-0.333913
-0.476552,0.702704,0.000000,-0.444714
-0.769260,-0.930874,0.000000,-0.655714
-0.021778,-0.332595,0.000000,-0.021775
0.971813,-0.325972,0.000000,0.771104
0.976013,0.306168,0.000000,0.773260
-0.622736,0.331217,0.000000,-0.556970
0.128056,-0.194115,0.000000,0.127363
0.307250,-0.917290,0.000000,0.298095
-0.315096,-0.188962,0.000000,-0.305248
0.785095,-0.788507,0.000000,0.665586
-0.707504,-0.211164,0.000000,-0.615744
-0.034345,-0.463313,0.000000,-0.034332
0.012512,0.938691,0.000000,0.012511
0.540085,0.957280,0.000000,0.495199
0.540816,-0.806818,0.000000,0.495765
-0.771522,-0.935736,0.000000,-0.657134
0.895886,-0.540781,0.000000,0.730538
-0.866610,-0.125892,0.000000,-0.714058
0.126623,-0.894797,0.000000,0.125953
0.548136,0.102636,0.000000,0.501411
0.411370,0.925400,0.000000,0.390270
-0.566147,-0.460574,0.000000,-0.515156
-0.268715,0.741103,0.000000,-0.262514
-0.377863,0.416189,0.000000,-0.361278
-0.447859,-0.592769,0.000000,-0.421072
4.963864,0.000000,0.000000,1.372001
0.913008,0.000000,0.000000,0.739956
2.429646,0.000000,0.000000,1.180345
3.647594,0.000000,0.000000,1.303217
5.567312,0.000000,0.000000,1.393072
6.265069,0.000000,0.000000,1.412516
0.715971,0.000000,0.000000,0.621365
5.052592,0.000000,0.000000,1.375403
5.577939,0.000000,0.000000,1.393403
0.907919,0.000000,0.000000,0.737173
0.379105,0.000000,0.000000,0.362365
0.720684,0.000000,0.000000,0.624473
0.674235,0.000000,0.000000,0.593224
0.520856,0.000000,0.000000,0.480193
0.345032,0.000000,0.000000,0.332242
0.361974,0.000000,0.000000,0.347302
0.361479,0.000000,0.000000,0.346864
0.455790,0.000000,0.000000,0.427658
0.582766,0.000000,0.000000,0.527651
0.511593,0.000000,0.000000,0.472879
0.022779,0.000000,0.000000,0.022775
0.045248,0.000000,0.000000,0.045217
0.093859,0.000000,0.000000,0.093585
0.080974,0.000000,0.000000,0.080798
0.092150,0.000000,0.000000,0.091890
0.022109,0.000000,0.000000,0.022105
0.000462,0.000000,0.000000,0.000462
0.046636,0.000000,0.000000,0.046602
0.040523,0.000000,0.000000,0.040501
0.097185,0.000000,0.000000,0.096881
0.009579,0.000000,0.000000,0.009579
0.000739,0.000000,0.000000,0.000739
0.006546,0.000000,0.000000,0.006546
0.009225,0.000000,0.000000,0.009225
0.008334,0.000000,0.000000,0.008334
0.003858,0.000000,0.000000,0.003858
0.000647,0.000000,0.000000,0.000647
0.000657,0.000000,0.000000,0.000657
0.002802,0.000000,0.000000,0.002802
0.008586,0.000000,0.000000,0.008586
0.000576,0.000000,0.000000,0.000576
0.000907,0.000000,0.000000,0.000907
0.000270,0.000000,0.000000,0.000270
0.000811,0.000000,0.000000,0.000811
0.000115,0.000000,0.000000,0.000115
0.000302,0.000000,0.000000,0.000302
0.000785,0.000000,0.000000,0.000785
0.000594,0.000000,0.000000,0.000594
0.000445,0.000000,0.000000,0.000445
0.000903,0.000000,0.000000,0.000903
-0.952188,0.000000,0.000000,-0.760912
-1.959224,0.000000,0.000000,-1.098858
-2.589151,0.000000,0.000000,-1.202219
-2.562399,0.000000,0.000000,-1.198715
-0.256550,0.000000,0.000000,-0.251134
-1.581301,0.000000,0.000000,-1.006900
-3.784508,0.000000,0.000000,-1.312465
-4.273364,0.000000,0.000000,-1.340925
-4.727937,0.000000,0.000000,-1.362360
-2.341383,0.000000,0.000000,-1.167150
-0.370549,0.000000,0.000000,-0.354863
-0.337516,0.000000,0.000000,-0.325510
-0.862123,0.000000,0.000000,-0.711490
-0.413447,0.000000,0.000000,-0.392045
-0.893094,0.000000,0.000000,-0.728987
-0.030457,0.000000,0.000000,-0.030448
-0.507305,0.000000,0.000000,-0.469475
-0.713741,0.000000,0.000000,-0.619889
-0.431114,0.000000,0.000000,-0.407038
-0.170107,0.000000,0.000000,-0.168494
-0.028679,0.000000,0.000000,-0.028671
-0.056042,0.000000,0.000000,-0.055983
-0.028366,0.000000,0.000000,-0.028358
-0.047950,0.000000,0.000000,-0.047913
-0.055205,0.000000,0.000000,-0.055149
-0.023834,0.000000,0.000000,-0.023829
-0.028604,0.000000,0.000000,-0.028596
-0.016342,0.000000,0.000000,-0.016341
-0.064428,0.000000,0.000000,-0.064339
-0.081401,0.000000,0.000000,-0.081222
-0.000246,0.000000,0.000000,-0.000246
-0.002968,0.000000,0.000000,-0.002968
-0.006977,0.000000,0.000000,-0.006977
-0.009397,0.000000,0.000000,-0.009397
-0.005367,0.000000,0.000000,-0.005367
-0.003527,0.000000,0.000000,-0.003527
-0.000699,0.000000,0.000000,-0.000699
-0.006228,0.000000,0.000000,-0.006228
-0.006891,0.000000,0.000000,-0.006891
-0.004988,0.000000,0.000000,-0.004988
-0.000611,0.000000,0.000000,-0.000611
-0.000793,0.000000,0.000000,-0.000793
-0.000857,0.000000,0.000000,-0.000857
-0.000734,0.000000,0.000000,-0.000734
-0.000240,0.000000,0.000000,-0.000240
-0.000951,0.000000,0.000000,-0.000951
-0.000543,0.000000,0.000000,-0.000543
-0.000546,0.000000,0.000000,-0.000546
-0.000044,0.000000,0.000000,-0.000044
-0.000009,0.000000,0.000000,-0.000009