简单的 C++ 代码:
#include "../../std_lib_facilities.h"
#include <float.h>
double ctok(double c){
const double koef_c_2_k = 274.15;
double k = c + koef_c_2_k;
if(k < -koef_c_2_k) error("Output abroad range of admissible values.");
return k;
}
int main(){
cout << "Max double value: " << DBL_MAX << endl;
double c = 0;
cout << "Celsius: ";
cin >> c;
double k = ctok(c);
cout << "Kelvin: " << k << endl;
return 0;
}
输出:
bush@host-nix:~/cpp/bs/5/5.2$ ./a.out
最大双精度值:1.79769e+308
摄氏度:0
开尔文:274.15
bush@host-nix:~/cpp/bs/5/5.2 $ ./a.out
最大双精度值:1.79769e+308
摄氏度:100
开尔文: 374.15
bush@host-nix:~/cpp/bs/5/5.2$ ./a.out
最大双精度值:1.79769e+308
摄氏度:-100
开尔文:174.15
bush@host-nix:~/cpp/bs/5/5.2$ ./a.out
最大双精度值:1.79769e+308
摄氏度:1.79769e+308
开尔文:1.79769e+308
bush@host -nix:~/cpp/bs/5/5.2$
在最后一种情况下,我设置了摄氏度的最大值,我等待了错误消息,但我得到了错误的开尔文值。为什么会这样?
谢谢你。