#include
using namespace std;
//sin(M_PI_4) = 0,70710678118654752440084436210485
#define M_PI_4 0.78539816339744830962
#define top 100
#define numsize 400
string msin( long double x )
{
mpf_t ret, temp, num;
mp_exp_t exp;
int i, j;
string str;
mpf_init_set_d( num, x );
mpf_init2( ret, numsize );
mpf_init2( temp, numsize );
for( i = 1 ; i <= top ; i += 2 )
{
mpf_pow_ui( temp, num, i );
for( j = 1 ; j <= i ; ++j )
mpf_div_ui( temp, temp, j );
if ( (i-1)%4 )
mpf_sub( ret, ret, temp );
else
mpf_add( ret, ret, temp );
}
str = mpf_get_str( NULL, &exp, 10, 100, ret );
str.insert( exp, "." );
if ( str[0] == '.' )
str.insert( 0, "0" );
mpf_clear( ret );
mpf_clear( temp );
mpf_clear( num );
return str;
}
string mcos( long double x )
{
mpf_t ret, temp, num;
mp_exp_t exp;
int i, j;
string str;
mpf_init_set_d( num, x );
mpf_init2( ret, numsize );
mpf_set_ui( ret, 1 );
mpf_init2( temp, numsize );
for( i = 2 ; i <= top ; i += 2 )
{
mpf_pow_ui( temp, num, i );
for( j = 1 ; j <= i ; ++j )
mpf_div_ui( temp, temp, j );
if ( i%4 )
mpf_sub( ret, ret, temp );
else
mpf_add( ret, ret, temp );
}
str = mpf_get_str( NULL, &exp, 10, 100, ret );
str.insert( exp, "." );
if ( str[0] == '.' )
str.insert( 0, "0" );
mpf_clear( ret );
mpf_clear( temp );
mpf_clear( num );
return str;
}
int main( int argc, char **argv )
{
cout << msin(M_PI_4) << "\n" << mcos(M_PI_4) << endl;
return 0;
}
link
Комментариев нет:
Отправить комментарий