Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Tests / Tutorial / Step6 / MathFunctions / mysqrt.cxx
blob33659b74086ac0e93b99ef2882c08d2c8c3ef3a4
1 #include <stdio.h>
2 #include "MathFunctions.h"
3 #include "TutorialConfig.h"
5 // include the generated table
6 #include "Table.h"
8 #include <math.h>
10 // a hack square root calculation using simple operations
11 double mysqrt(double x)
13 if (x <= 0)
15 return 0;
18 double result;
20 // if we have both log and exp then use them
21 double delta;
23 // use the table to help find an initial value
24 result = x;
25 if (x >= 1 && x < 10)
27 result = sqrtTable[static_cast<int>(x)];
30 // do ten iterations
31 int i;
32 for (i = 0; i < 10; ++i)
34 if (result <= 0)
36 result = 0.1;
38 delta = x - (result*result);
39 result = result + 0.5*delta/result;
40 fprintf(stdout,"Computing sqrt of %g to be %g\n",x,result);
43 return result;