十进制浮点数转换成IEEE754
由于double精度问题,我只保留了15位小数
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() 5 { 6 double g = 0 ; 7 int v = 0 ; 8 int arr[100]={} ; 9 int aee[100]={} ; 10 double a = 0 ;//输入的实数 11 scanf("%lf",&a); 12 if (a > 0 ){ 13 v = 0 ; 14 }else{ 15 v = 1 ; 16 } 17 a = fabs(a) ; 18 int b = fabs(a) ; 19 double c = a - b ; 20 int i = 0 ; 21 double f = 0 ; 22 int k = 0 ; 23 double p = 0 ; 24 double u = 0 ; 25 for ( i = 0; b > 0; i++) 26 { 27 arr[i] = b%2 ; 28 b = b / 2 ; 29 } 30 for ( int j = 0; j <=i ; j++) 31 { 32 f=pow(10,j); 33 f = f*arr[j] ; 34 g = g + f ; 35 } 36 for ( k = 0; c>0 && k<=50 ; k++) 37 { 38 aee[k]= c * 2 ; 39 c = c * 2 ; 40 c = c - (int)c; 41 } 42 for ( int o = 0; o <=32 ; o++) 43 { 44 p =pow(0.1,o+1); 45 p = p*aee[o]; 46 u = u + p ; 47 } 48 double w = u +g ; 49 int s = 0 ; 50 double e = w ; 51 if (w > 1){ 52 for ( s = 0; w > 1 ; s++) 53 { 54 w = w /10 ; 55 56 } 57 w = w *10 ; 58 59 60 }else{ 61 for ( s = 0; e < 1; s--) 62 { 63 e = e * 10 ; 64 } 65 } 66 int n = 0 ; 67 if (w>1) 68 { 69 n = s -1 +127 ; 70 }else{ 71 n = s+127 ; 72 } 73 74 int result[100]={} ; 75 printf("%d\n",v) ; 76 printf("%d\n",n) ; 77 if (w>1) 78 { 79 printf("%.15lf\n",w) ; 80 }else{ 81 printf("%.15lf\n",e) ; 82 } 83 84 85 return 0 ; 86 }