什麼是浮點數

#include<stdio.h>
#include<limits.h>  // 包含int的極限
#include<float.h>  //包含float的極限

void main(){
	printf("The bytes of int is %d, the bytes of float is %d \n", sizeof(int),sizeof(float));
	printf("The max of int is %d, the min of int is %d\n",INT_MAX,INT_MIN);
	printf("The max of Float  is %f, the min of Float is %.100f\n", FLT_MAX, FLT_MIN); //%.100f 顯示小數點後100位  %f默認是小數點後6位
	getchar();
	/* Output
	The bytes of int is 4, the bytes of float is 4
	The max of int is 2147483647, the min of int is -2147483648
	The max of Float is 340282346638528860000000000000000000000.000000, the min of Float is 0.0000000000000000000000000000000000000117549435082228750000000000000000000000000000000000000000000000
	對於以上結果的分析,爲何int和flaot一樣是4個字節,表示的返回會不同呢?
			由於int的每一位都是數據位,4個字節,32位,一位當作符號位,其餘都是有效的數據位,有限的32位只能表示2147483647。
			而float 4個字節,32位,一個是符號位,其餘31位中,有一部分是有效數據位,一部分是指數位,一部分是基數, 因爲指數的緣由,表示的範圍就會更大
			FLT_MAX是最大正負數,FLT_MIN是最小正負數
			c語言默認是double
			通常用浮點數表示。這種表達方式利用科學計數法來表達實數,即用一個尾數(Mantissa),一個基數(Base),一個指數(Exponent)以及一個表示正負的符號來表達實數
			定點數包含整數和定點小數(0.123443  無整數部分)
	*/
}

 浮點數的精度偏差blog

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

void main(){
	float f = 0.1234567890;//默認只能精確到小數點後6位,6位之外可能正確,可能不正確
	double lf = 0.12345678901234567890;  //默認只能精確到小數點後15位
	long double Lf = 0.12345678901234567890; 
	printf("%f\n",f); 
	printf("%.20lf\n", lf);
	printf("%.20f\n", Lf);
	//%f, %lf,%Lf 默認只能輸出小數點後六位
	system("pause");

}
相關文章
相關標籤/搜索