#include <stdio.h> #include <limits.h> //整數限制 #include <float.h> //浮點數限制 void main() { printf("the FLOAT_MIN number is : %f\n",FLT_MIN); //float的最小值 printf("the FLOAT_MAX number is : %f\n",FLT_MAX); //float的最大值 printf("the FLOAT_MAX number is : %e\n",FLT_MAX); //float的最大值 getchar(); }
#include <stdio.h> #include <limits.h> //整數限制 #include <float.h> //浮點數限制 void main() { printf("the FLOAT_MIN number is : %f\n",FLT_MIN); //float的最小值 printf("the FLOAT_MAX number is : %f\n",FLT_MAX); //float的最大值 printf("the FLOAT_MAX number is : %e\n",FLT_MAX); //float的最大值 printf("the INT_MAX number is : %d\n",INT_MAX); printf("the INT_MIN number is : %d\n",INT_MIN); printf("the CHAR_MAX number is : %d\n",CHAR_MAX); printf("the CHAR_MIN number is : %d\n",CHAR_MIN); printf("the SHORT_MAX number is : %d\n",SHRT_MAX); printf("the SHORT_MIN number is : %d\n",SHRT_MIN); printf("the LONG_MAX number is : %d\n",LONG_MAX); printf("the LONG_MIN number is : %d\n",LONG_MIN); printf("the DOUBLE_MAX number is : %d\n",DBL_MAX); printf("the DOUBLE_MIN number is : %d\n",DBL_MIN); getchar(); }
float:
1bit(符號位) 8bits(指數位) 23bits(尾數位)
double:
1bit(符號位) 11bits(指數位) 52bits(尾數位)
於是,float的指數范圍為-127~+128,而double的指數范圍為-1023~+1024,並且指數位是按補碼的形式來划分的。
其中負指數決定了浮點數所能表達的絕對值最小的非零數;而正指數決定了浮點數所能表達的絕對值最大的數,也即決定了浮點數的取值范圍。
float的范圍為-2^128 ~ +2^128,也即-3.40E+38 ~ +3.40E+38;double的范圍為-2^1024 ~ +2^1024,也即-1.79E+308 ~ +1.79E+308。