SQRT sin cos exp function Taylor Formula and Mike laulin implementation

Source: Internet
Author: User
Tags sin cos
Sin cos exp is calculated using the Taylor Formula and mclawin formula. To prevent the power operation index from being too high, it is easy to cause overflow when a large input parameter is calculated. Considering that sin and cos both use 2 * PI as the cycle, therefore, set a threshold value in the function (which can be modified by yourself. Here, 2 * pi is used as the threshold value). When the real parameter is greater than the threshold value, calculate it to-2 * pI ~ The 2 * PI Quadrant not only prevents the overflow of large numbers, but also increases the computing speed. In addition, the error range can be self-controlled. SQRT uses an approximation. Algorithm Here, an array is set as the starting base value of the approximation algorithm. The calculation factor of exp has a large number overflow problem. Here we use the number of steps to limit infinite computation. The step value is an empirical value and cannot fully calculate all the values of this function. // Mathtest. c
# Include "stdio. H"
# Include "math. H" # define PI 3.14156.
Float SiNx (float X );
Float fun_sinx (float X, int m );
Float expx (float X );
Float fun_exp (float X, int N );
Float sqrtx (float t );
Float cosx (float X );
Float fun_cos (float X, int m );
Int main ()
{
Float x = PI/2;
Printf ("Sin (% F) = % F \ n", X, sin (x ));
Printf ("SiNx (% F) = % F \ n", X, SiNx (x ));
Printf ("cos (% F) = % F \ n", X, cos (x ));
Printf ("cosx (% F) = % F \ n", X, cosx (x ));
X = PI/1.3;
Printf ("Sin (% F) = % F \ n", X, sin (x ));
Printf ("SiNx (% F) = % F \ n", X, SiNx (x ));
Printf ("cos (% F) = % F \ n", X, cos (x ));
Printf ("cosx (% F) = % F \ n", X, cosx (x ));
X = PI/2.3;
Printf ("Sin (% F) = % F \ n", X, sin (x ));
Printf ("SiNx (% F) = % F \ n", X, SiNx (x ));
Printf ("cos (% F) = % F \ n", X, cos (x ));
Printf ("cosx (% F) = % F \ n", X, cosx (x ));
X = PI/0.3;
Printf ("Sin (% F) = % F \ n", X, sin (x ));
Printf ("SiNx (% F) = % F \ n", X, SiNx (x ));
Printf ("cos (% F) = % F \ n", X, cos (x ));
Printf ("cosx (% F) = % F \ n", X, cosx (x ));
X = 4.5 * PI;
Printf ("Sin (% F) = % F \ n", X, sin (x ));
Printf ("SiNx (% F) = % F \ n", X, SiNx (x ));
Printf ("cos (% F) = % F \ n", X, cos (x ));
Printf ("cosx (% F) = % F \ n", X, cosx (x ));
X = 1999;
Printf ("Sin (% F) = % F \ n", X, sin (x ));
Printf ("SiNx (% F) = % F \ n", X, SiNx (x ));
Printf ("cos (% F) = % F \ n", X, cos (x ));
Printf ("cosx (% F) = % F \ n", X, cosx (x ));
X = 1.0;
Printf ("exp (% F) = % F \ n", X, exp (x ));
Printf ("expx (% F) = % F \ n", X, expx (x); X = 2.0;
Printf ("exp (% F) = % F \ n", X, exp (x ));
Printf ("expx (% F) = % F \ n", X, expx (x); X =. 0014;
Printf ("SQRT (% F) = % F \ n", X, SQRT (x ));
Printf ("sqrtx (% F) = % F \ n", X, sqrtx (x ));
Printf ("Hello, math! \ N ");
Return 0;
} Float SiNx (float X)
{
Int M = 1;
Float tempret;
Float retval = 0.0; float Pi = 3.1415926; If (x> 2 * PI | x <-2 * PI)
{
X-= (INT) (X/(2 * PI) * (2 * PI );
} Do
{
Tempret = fun_sinx (x, M );
Retval + = tempret;
M ++;
} While (tempret <-. 0000005 | tempret> 0.0000005); Return retval;
} Float fun_sinx (float X, int m)
{
Float ret = 0.0;
Int I = 0; If (M % 2 = 0)
{
Ret =-1.0;
} Else
{
Ret = 1.0;
} For (I = 1; I <= m-1; I ++)
{
Ret = RET * x/I;
}

Return ret;
} Float expx (float X)
{
Float retvals = 1.0;
Float tempret;
Int n = 1; int step; If (x> 10) step = 20; else step = 40; do
{
Tempret = fun_exp (x, N );
Retval + = tempret;
N ++;
} While (tempret <-0.0000005 | tempret> 0.0000005) & (n <step); Return retval;
} Float fun_exp (float X, int N)
{
Float ret = 1.0;
Int I = 0; for (I = 1; I <= N; I ++)
{
Ret = RET * x/I;
} Return ret;
}
Float sqrt_array [] = {0.0, 1.0, 4.0, 3.0*3.0, 4.0*4.0, 5.0*5.0, 6.0*6.0, 7.0*7.0, 8.0*8.0, 9.0*9.0, 10.0*10.0, 100.0*100.0, 1000.0*1000.0 };
Float sqrtx (float T)
{
Float sqrt_base = 1.0;
Int I = 0;
Float sqrt_ret;
Float temp; while (I <= 12 & T> sqrt_array [I ++]); // search for a base range through traversal. if (I> = 1 & I <= 11) // 0.0 <t <100.0*100.0
{
Sqrt_base * = (i-1.0 );
}
Else if (I = 12)
{
Sqrt_base = 100.0;
} Else if (I = 13)
{
Sqrt_base = 1000.0;
} Sqrt_ret = sqrt_base;
Temp = (sqrt_ret * sqrt_ret-T)/T; while (temp> 0.00005 | temp <-0.00005)
{
Sqrt_ret = (sqrt_base + T/sqrt_base)/2.0;
Sqrt_base = sqrt_ret;
Temp = (sqrt_ret * sqrt_ret-T)/T;
}

Return sqrt_ret;

}

float fun_cos (float X, int m)
{< br> float ret_val;
int I; If (M % 2 = 0)
{< br> ret_val = 1.0;
}else
{< br> ret_val =-1.0;
}for (I = 1; I <= 2 * m; I ++)
{< br> ret_val = ret_val * x/I;
}return ret_val;
} float cosx (float X)
{< br> float ret_val = 1.0;
float temp_ret;
int M = 1;
float Pi = 3.1415926; If (x> 2 * PI | x <-2 * PI)
{
X = x-(INT) (x/(2 * PI) * (2 * PI);
}do
{< br> temp_ret = fun_cos (x, M ++ );
ret_val + = temp_ret;} while (temp_ret> 0.00005 | temp_ret <-0.00005); Return ret_val;
}< br> from: http://blog.edu.cn/user1/12168/archives/2007/1921714.shtml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.