浮点数 11 Jan 2014 | c/c++ 浮点数比较 #include <stdlib.h> #include <stdio.h> #include <math.h> #include <float.h> int isDoubleBiger(double doubleA, double doubleB, double dblEpsilon) { double value = doubleA - doubleB; if( value > dblEpsilon ) { printf("%.50lf biger than %.50lf\n", doubleA, doubleB); return 0; }else { printf("%.50lf less or equal to %.50lf\n", doubleA, doubleB); return -1; } } int isZero(double doubleA, double dblEpsilon) { if( doubleA > -dblEpsilon && doubleA < dblEpsilon ) { printf("%.50f equal 0\n", doubleA); return 0; }else { printf("%.50f not equal 0\n", doubleA); return -1; } } int main() { printf("%.50lf DBL_EPSILON\n", DBL_EPSILON); printf("%.50lf FLT_EPSILON\n", FLT_EPSILON); double i = 0.01; double j = 0.0000000000000001; isDoubleBiger(i, 0, DBL_EPSILON); isZero(j, DBL_EPSILON); } /*result 0.00000000000000022204460492503130808472633361816406 DBL_EPSILON 0.00000011920928955078125000000000000000000000000000 FLT_EPSILON 0.01000000000000000020816681711721685132943093776703 biger than 0.00000000000000000000000000000000000000000000000000 0.00000000000000009999999999999999790977867240346036 equal 0 */ 浮点数四舍五入 %.2f 保留两位小数自动四舍五入,不四舍五入可以对保留位后一位减5 printf("%.2f\n", 1.235);//四舍五入保留2位 printf("%.2f\n", 1.235 - 0.005);//不四舍五入保留2位 http://www.csdn123.com/html/topnews201408/56/8456.htm