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_exp: Implement exp function using series #131

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/math/p_exp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,66 @@
/**
*
* Calculate exponent (e^a), where e is the base of the natural logarithm
* (2.71828.)
* (2.71828.) Using series expansion of function
*
* @param a Pointer to input vector
*
* @param c Pointer to output vector
*
* @param n Size of 'a' and 'c' vector.
*
* @param p Number of processor to use (task parallelism)
*
* @param team Team to work with
*
* @return None
*
*/
#include <math.h>
static const float precision = 0.1e-6;

float inverse(float a) //function to calucalte inverse without division operator using Newton Raphson method
{
float result = precision;
while(result * a > 1 + precision || result * a < 1 - precision)
{
result *= (2 - a * result);
}
return result;
}

float single_exp(float b)
{
float a = b;
if(b < 0) //if exponent is negative then calculate e^b and return 1/e^b
{
a = -a;
}
float result = 1;
float counter = 1;
float next_term = 1;
float old_result;
float error;
do
{
old_result = result;
next_term = next_term * a * inverse(counter);
result += next_term;
counter += 1;
error = (old_result - result) / result;
if(error < 0)
{
error = -error;
}
}while(error > precision);
if(b < 0)
return inverse(result);
return result;
}
void p_exp_f32(const float *a, float *c, int n)
{

int i;
for (i = 0; i < n; i++) {
*(c + i) = expf(*(a + i));
*(c + i) = single_exp(*(a + i));
}
}