double sqrt_x(double x)
{
double x0, xn, xn1, diff;
double pricision = 1e-10;
x0 = x;
*((char *)&x0 + 7) &= 0x7f; // x0 = fabs_x(x0);
xn = x0;
do
{
xn1 = xn;
xn = (xn + x0 / xn) / 2;
diff = xn1 - xn;
*((char *)&diff + 7) &= 0x7f; // diff = fabs_x(diff);
}
while(diff > pricision);
return xn;
}
double exp_x(double x)
{
double sum = 1.0;
int step = 100;
while(step)
{
sum /= step--;
sum *= x;
sum += 1;
}
return sum;
/*
// e ^ x = 1 + x + (x ^ 2 / 2!) + (x ^ 3 / 3!) + ... + (x ^ n / n!)
double sum = 1, exp = 1, fact = 1;
int step = 0;
while(step++ < 100)
{
exp *= x;
fact *= step;
sum += exp / fact;
}
return sum;
*/
}
double ln_x(double x)
{
// ln(x)=2arctanh((x-1)/(x+1))
// arctanh(x)= x + x^3/3 + x^5/5 + ... (x <= 1)
x = (x - 1) / (x + 1);
int step = 100;
double sum = x, up = x, xsqr = x * x, down = 1.0;
while(step--)
{
up *= xsqr;
down += 2.0;
sum += up / down;
}
return 2 * sum;
}
double pow_x(double b, double e)
{
return exp_x(e * ln_x(b));
}
double sin_x(double x)
{
// sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
int step = 0;
double up, down, xsqr, flag, sum;
sum = x;
xsqr = x * x;
up = x;
down = 1.0;
flag = 1.0;
int s2 = 1;
while(step++ < 100)
{
flag = 0 - flag;
up *= xsqr;
s2 += 2;
down *= s2 * (s2 - 1);
sum += flag * up / down;
}
return sum;
}