Article Directory
1.cin reads one line of data
char str[100];
cin.getline(str,100);
string str;
getline(cin,str);
Double-data control accuracy 2.cout
Add the header file "iomanip"
double a = 3.1415926;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<a<<endl;
3. Comparison of floating point! ! !
Is equal to 1. Analyzing
In the floating-point store computer is not always accurate, such as 3.14, or the store may be 3.1399999 3.14000001, but the "==" will be completely equal is true, then the introduction of a very small amount of error correction eps
A falling number of a [+ eps b-eps.b] in the range of, say, a == b, taken generally eps 10 ^ (- 8) suitable
const double eps = 1e-8;
或者使用宏定义方式
#define Equ(a,b) ((fabs((a)-(b)))<(eps))
Examples
#include<stdio.h>
#include<math.h>
const double eps = 1e-8;
#define Equ(a,b) ((fabs((a)-(b)))<(eps))
int main(){
double db = 1.23;
if(Equ(db,1.23)){ //在这里 db == 1.23 也是可以的
printf("true");
}else{
printf("false");
}
return 0;
}
Usually there is no loss of accuracy is not easily through direct determination are OK, but easily lost through the calculation accuracy, it is not alright, such as the following
double db1 = 4 * asin(sqrt(2.0) / 2);
double db2 = 4 * asin(sqrt(3.0) / 2);
if(db1 == db2){
printf("true");
}else{
printf("false");
}
Obviously this is a large error calculation errors affected
2. greater than
#define More(a,b) (((a) - (b))>(eps))
3. less than
#define Less(a,b) (((a) - (b))<(-eps))
4 greater than or equal
#define MoreEqu(a,b) (((a) - (b))>(-eps))
The less
#define More(a,b) (((a) - (b))<(eps))
6. pi π
cos (π) = -1
const double Pi = acos(-1.0);
4. The time complexity of the spatial complexity of the coding complexity
General enough space, consider strategies to write code using space for time