由于类型转换,浮点值转换的二进制值无效

时间:2014-04-25 05:38:20

标签: c type-conversion

这里我从我的代码中添加函数,用于从给定的二进制数生成浮点数。

代码:

double binary_float(double f) /* Function to convert binary to float.*/
{
   long     integral = 0, floatInt = 0, i = 1, temp1 = 0, k = 1;
   double   floatFract = 0, fractional = 0, floatTotal = 0;

   //Separating the integral value from the floating point variable
   integral = (long)f;

   //Separating the fractional value from the variable
   fractional = f - (long)f;

   //Converting binary to decimal
   floatInt = binary_decimal(integral);

   //Loop for converting binary to Fractional value
   while( k < 10000000 && fractional != (double)0 )
   {
      k = k * 10;
      i = i * 2;
      temp1 = (long)(fractional * k);

printf("temp: %ld, r: %lf\n", temp1, (fractional * k));

      floatFract = floatFract + (double)temp1/(double)i;

printf("fact: %lf, r: %lf\n", floatFract, ((double)temp1/(double)i));

      fractional = fractional - (double)temp1/(double)k;

printf("frac: %lf, r: %lf\n", fractional, ((double)temp1/(double)k));

   }

   //Combining both the integral and fractional binary value.
   floatTotal = floatInt + floatFract;

   return floatTotal;
}

long binary_decimal(long n)
{
   long decimal=0, i=0, rem;
   while (n!=0)
   {
      rem = n%10;
      n/=10;
      decimal += rem*pow(2,i);
      ++i;
   }
   return decimal;
}

输出:

Enter a binary number: 1010.001100
temp: 0, r: 0.011000
fact: 0.000000, r: 0.000000
frac: 0.001100, r: 0.000000
temp: 0, r: 0.110000
fact: 0.000000, r: 0.000000
frac: 0.001100, r: 0.000000
temp: 1, r: 1.100000
fact: 0.125000, r: 0.125000
frac: 0.000100, r: 0.001000

temp: 0, r: 1.000000

fact: 0.125000, r: 0.000000
frac: 0.000100, r: 0.000000
temp: 9, r: 10.000000
fact: 0.406250, r: 0.281250
frac: 0.000010, r: 0.000090
temp: 9, r: 10.000000
fact: 0.546875, r: 0.140625
frac: 0.000001, r: 0.000009
temp: 9, r: 10.000000
fact: 0.617188, r: 0.070312
frac: 0.000000, r: 0.000001
1010.001100 in binary = 10.617188 in float

在输出中你可以看到 temp:0,r:1.000000 ,它表示temp1 = 0,这是长的1.000000的类型转换。

任何人都可以解释为什么这种类型转换不起作用吗?

3 个答案:

答案 0 :(得分:1)

OP:&#34; temp:0,r:1.000000 ,表示temp1 = 0,长期为1.000000的类型转换。&#34;
答:doublelong的转换有效,因为double小于1.0。

代码正在打印double舍入值,而不是其精确值,略小于1.0。 (long) some_number_slightly_less_than_10。尝试更精确地double而不是"%.20le"打印"%lf"

给定典型的IEEE binary64浮点数,当代码以double 1010.001100开头时,代码实际上以1010.00109999999995125108....开头。

至于10.617188的错误答案,我怀疑未发布的代码binary_decimal();


代码有错误:

// floatFract = floatFract + (double) temp1 / (double) i;
floatFract = floatFract + (double) temp1 / (double) k;
...
// wrong result of 10.61718750000000000000
// correct result follows
10.00109989999999982047....
// or to 6 decimal places
10.001100

轻微:请注意,以下代码仅适用于double fLONG_MIN范围内的LONG_MAX

integral = (long)f;

答案 1 :(得分:0)

temp1使用%ld标记打印long intr使用%lf标记打印double,因此结尾{ {1}}秒。

答案 2 :(得分:0)

在这里,我想到它并没有完全发挥作用。长数据类型存在限制,因此我们无法在此程序中输入更大的数字,然后该程序才能正常工作。

代码:

double binary_float(double f) /* Function to convert binary to float.*/
{
   long     integral = 0, floatInt = 0;
   double   floatFract = 0, fractional = 0, floatTotal = 0;

   //Separating the integral value from the floating point variable
   integral = (long)f;
   //Separating the fractional value from the variable
   fractional = f - (long)f;

   //Converting binary to decimal
   floatInt = binary_decimal(integral);
   //Converting float value from binary to float value
   floatFract = binary_float_float(fractional);

   //Combining both the integral and fractional binary value.
   floatTotal = floatInt + floatFract;

   return floatTotal;
}

long binary_decimal(long n) /* Function to convert binary to decimal.*/
{
   long decimal=0, i=0, rem;
   while (n!=0)
   {
      rem = n%10;
      n/=10;
      decimal += rem*pow(2,i);
      ++i;
   }
   return decimal;
}

double binary_float_float(double fractional)
{
   long     temp1 = 0, k = 1, i = 1;
   double   floatFract = 0;

   //Loop for converting binary to Fractional value
   while( k < 1000000000 && fractional != (double)0 )
   {
      k = k * 10;
      i = i * 2;
      temp1 = (long)(fractional * k);
      //If value is larger then 1 it means .001100 is represented as .00109994....
      //So it's last digit and so we can break loop from that point
      if (temp1 > 1)
      {
         temp1 = 1;
         floatFract = floatFract + (double)temp1/(double)i;
         break;
      }

      floatFract = floatFract + (double)temp1/(double)i;
      fractional = fractional - (double)temp1/(double)k;
   }

   return floatFract;
}

任何人都有更好的想法和更好的方法从二进制数中找到浮动值然后请分享。