double 显示与大小比较

精度:

float: 3e38

double: 1.7e308

 

显示

cout<<fixed<<a<<endl;      一般显示

cout<<scientific<<b<<endl;    科学计数法

cout<<setprecision(c)<<endl;      设置显示精度

 1 #include <iomanip>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int main() {
 7     // Creating a decimal double type variable
 8     double a = 3.912348239293;
 9 
10     // Creating an exponential double type variable
11     double ex1 = 325e+2;
12 
13     // Creating a float type variable
14     float b = 3.912348239293f;
15 
16     // Creating an exponential float type variable
17     float ex2 = 325e+2f;
18 
19     // Displaying output with fixed
20     cout << "Displaying Output With fixed:" << endl;
21     cout << "Double Type Number 1 = " << fixed << a << endl;
22     cout << "Double Type Number 2 = " << fixed << ex1 << endl;
23     cout << "Float Type Number 1  = " << fixed << b << endl;
24     cout << "Float Type Number 2  = " << fixed << ex2 << endl;
25 
26     // Displaying output with scientific
27     cout << "\nDisplaying Output With scientific:" << endl;
28     cout << "Double Type Number 1 = " << scientific << a << endl;
29     cout << "Double Type Number 2 = " << scientific << ex1 << endl;
30     cout << "Float Type Number 1  = " << scientific << b << endl;
31     cout << "Float Type Number 2  = " << scientific << ex2 << endl;
32     return 0;
33 }

 

 

比较大小

predicate 翻译过来叫 谓词

A predicate is the part of a sentence that contains the verb and tells you something about the subject. 也就是显示主语的状态. 而在计算机领域, 可能这个状态就更多地true和false来表示. 

1 class pig
2 {
3   bool is_eating(){};      
4 }

在上面, dog.is_barking()就是谓词, 这可能代表着我们 是否应该喂猪

 

double 类型不能直接用>=  <=  ==来比较大小, 需要一个类似predicate的东西, 也就是要设置一个容差

 

1 #include <math.h>
2 const double EPS = 1e-8;  
3 
4 if(fabs(a-b) < EPS)  //绝对值小于容差, 就认为两个double是相等的了
5 
6 if(a > b+EPS)   // 判断a是否大于b,因为大的肯定大,所以即使你小的加上,还是会更大 

 

 
posted @ 2023-03-28 10:34  无形深空  阅读(52)  评论(0编辑  收藏  举报