Not Yet Answered # Custom pow function

histrungalot 32 histrungalot 32 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

Greetings. I'm currently implementing small ANN on my microcontroller. I need to calculate sigmoid activation function. The problem is that I cannot use standard c libraries like math.h so I need custom written pow function.

This is how it will be used:pow(e,-(someFloat));

Did anyone have already implemented something similar ?

*Edited 4 Years Ago by Dr_Freeman*: n/a

1

You could use Maclaurin series expansion for e^x = Sum(x^n/n!,0..N) were you pick N to give the you precision want. The power here is always an integer so that is just a for loop. And the factorial is just another loop.

1

See if this helps. I don't think an overflow of the double should be a problem considering after t < -6, P(t) ~ 0 and t > 6, P(t) ~ 1. It is just if the precision is OK.

```
#include <stdio.h>
// Don't need math.h, I have it here to check my answers
// Comment it out in you app
#include <math.h>
double Pow(double x, double n) {
double ret=x;
int i;
if ( n )
for ( i=0; i <n-1; i++)
ret = x*ret;
else
ret = 1;
return ret;
}
double fac(int i) {
if ( !i )
return 1;
else
return(i*fac(--i));
}
double eToPow(double toPow){
double ret=0;
int i;
// Change the number of loops to
// increase precision. Large loop could overflow double
for (i=0; i<50; i++)
ret += Pow(toPow,i)/fac(i);
return ret;
}
double sigmoid(double t){
return (1.0/(1+eToPow(-t)));
}
int main()
{
double toPow=9.555333;
double ans=eToPow(toPow);
// Testing e^x function
printf("My func: e^%f = %.15f\n",toPow,ans);
// Comment out line below, they are just for testing
printf("Math func: exp(%f) = %.15f\n",toPow,exp(toPow));
printf("Diff %.15f\n",fabs(exp(toPow)-ans));
// Test sigmoid
printf("Sigmoid(-2) = %.15f\n",sigmoid(-2));
printf("Sigmoid(0) = %.15f\n",sigmoid(0));
printf("Sigmoid(2) = %.15f\n",sigmoid(2));
return 0;
}
```

Output:

```
$ ./a.out
My func: e^9.555333 = 14119.795105499684723
Math func: exp(9.555333) = 14119.795105499677447
Diff 0.000000000007276
Sigmoid(-2) = 0.119202922022118
Sigmoid(0) = 0.500000000000000
Sigmoid(2) = 0.880797077977882
$
```

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...