Home > Back-end >  C language for cosine function approximation has high accuracy in calculating factorial problem dire
C language for cosine function approximation has high accuracy in calculating factorial problem dire

Time:04-03

Problems in the blog https://blog.csdn.net/qq_42120843/article/details/115391196? SPM=1001.2014.3001.5502, write here may length is too big, big trouble point a, the selfless help the younger brother thank you first

CodePudding user response:

The factorial function, int n type wrong, change of type double, int type for maximum: 2147483647, when accuracy take 0.01, n * fact (n - 1)=12! 14, 0.001! 16, 0.0001! When 13! When is beyond the scope of the n

CodePudding user response:

Fyi:
 # include & lt; Iostream> 
#include
#include
using namespace std;
The inline int the COMPARE (string str1, string str2) {//return equal 0, is greater than the return 1, less than the return 1
If (str1. The size () & gt; Str2. The size ()) return 1;//the length of the long integer greater than the length of the integer
Else if (str1. The size () & lt; Str2. The size ()) return 1;
The else pare said return str1.com (str2);//if equal length, head to tail according to the comparison of
}
String PLUS (string number1, string number2) {
int i;
Int length1=number1. The size ();
Int length2=number2. The size ();

The string result="";

Reverse (number1. The begin (), number1. The end ());
Reverse (number2. The begin (), number2. The end ());

for(i=0; I & lt; Length1 & amp; & I & lt; Length2. I++) {
Char c=(char) (number1 number2 [I] + [I] - 48).
Result=result + c;
}

While (I & lt; Length1) {
Result=result + number1 [I];
++i;
}

While (I & lt; Length2) {
Result=result + number2 [I];
++i;
}

Int carry=0;
for(i=0; I & lt; (int) result. The size (); + + I) {
Int [I] value=https://bbs.csdn.net/topics/result - 48 + carry;
The result [I]=(char) (value % 10 + 48);
Carry=value/10;
}

If (carry!=0) {
Result=result + (char) + 48 (carry);
}

For (I=result. The size () - 1; I & gt;=0; I -) {
If (result [I]!='0') break;
}

Result=result. The substr (0, I + 1);

Reverse (result. The begin (), the result. The end ());
If (result. The length ()==0) result="0";
return result;
}
String MULTIPLY (string number1, string number2) {
int i, j;
Int * iresult;
Int length1=number1. The size ();
Int length2=number2. The size ();
The string result="";

Reverse (number1. The begin (), number1. The end ());
Reverse (number2. The begin (), number2. The end ());

Iresult=(int *) malloc (sizeof (int) * (length1 + length2 + 1));
Memset (iresult, 0, sizeof (int) * (length1 + length2 + 1));

for(i=0; I & lt; Length1; I++) {
For (j=0; J & lt; Length2. J++) {
Iresult [I + j] +=((number1 [I] - 48) * (number2 [j] - 48));
}
}

Int carry=0;
for(i=0; I & lt; Length1 + length2; I++) {
The int value=https://bbs.csdn.net/topics/iresult [I] + carry;
Iresult=[I] value % 10;
Carry=value/10;
}

For (I=length1 + length2-1; I & gt;=0; I -) {
If (iresult [I]!=0) break;
}

for(; I & gt;=0; I -) {
Result=result + (char) (iresult [I] + 48);
}

Free (iresult);

If (result=="") result=" 0 ";
return result;
}
The string of the factorial (string n) {
String temp="1";
The string I;
For (I="1"; COMPARE (I, n) & lt;=0; I=PLUS (I, "1")) {
Temp=MULTIPLY (temp, I);
}
return temp;
}
Int main (void) {
Cout & lt; return 0;
}
//93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
//

CodePudding user response:

Again for reference:
 # include & lt; Iostream> 
#include
using namespace std;
The inline int the compare (string str1, string str2) {//return equal 0, is greater than the return 1, less than the return 1
If (str1. The size () & gt; Str2. The size ()) return 1;//the length of the long integer greater than the length of the integer
Else if (str1. The size () & lt; Str2. The size ()) return 1;
The else pare said return str1.com (str2);//if equal length, head to tail according to the comparison of
}
String SUB_INT (string str1, string str2);
String ADD_INT (string str1, string str2) {//high precision addition
Int sign=1;//sign for the sign bit
String STR.
If (str1 [0]=='-') {
If (str2 [0]=='-') {
Sign=1;
STR=ADD_INT (str1. Erase (0, 1), str2. Erase (0, 1));
} else {
STR=SUB_INT (str2, str1. Erase (0, 1));
}
} else {
If (str2 [0]=='-') {
STR=SUB_INT (str1, str2. Erase (0, 1));
} else {//alignment, the two integers up short integer adding 0
String: : size_type L1, L2,
int i;
L1=str1. The size ();
L2=str2. The size ();
If (L1 & lt; L2) {
for (i=1; i<=L2 - L1. I++) str1="0" + str1;
} else {
for (i=1; i<=L1, L2, I++) str2="0" + str2;
}
Int int1=0, int2=0;//record int2 carry
For (I=str1. The size () - 1; I>=0; I -) {
Int1=(int (str1 [I]) - '0' + int (str2 [I]) - '0' + int2) % 10;
Int2=(int (str1 [I]) - '0' + int (str2 [I]) - '0' + int2)/10;
STR=char (int1 + '0') + STR.
}
If (int2! STR==0) char (int2 + '0') + STR;
}
}
//operation post-processing the sign bit
If ((sign==1) & amp; & (STR [0]! STR='0'))="-" + STR;
Return the STR;
}
String SUB_INT (string str1, string str2) {//high precision subtraction
Int sign=1;//sign for the sign bit
String STR.
Int I, j;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related