#include <iostream> //#include <algorithm>//std::min std::max #include <stdint.h> #include <Windows.h> using namespace std; //包含在c++标准库中头文件<algorithm>中,在头文件<windows.h>中定义了min,max的宏, //若在包含<algorithm>的同时包含<windows.h>会导致函数无法使用 int main() { double d1 =(double)(INT64_MIN); double d2 =(double)(INT64_MAX); double dmin1 = min(0,d1); double dmin2 = min(d1,0); double dmax1 = max(0,d2); double dmax2 = max(d2,0); return 0; }
//double 无效值表示方法 double Nan() { UINT64 u = 0x7fffffffffffffff; return *(double*)&u; } int main() { double d = Nan(); double dmax3 = max(d,-1); double dmax4 = max(d,0); cout << "dmax3: " << dmax3 << endl; cout << "dmax4: " << dmax4 << endl; return 0; }
注:#define max(a,b) (((a) > (b)) ? (a) : (b))
无效值和有效值比较,问号前面的表达式永远是假,所以返回b