在什么样的背景发生在转换INT浮动 [英] what happens at background when convert int to float

查看:88
本文介绍了在什么样的背景发生在转换INT浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些没有关于一个人如何能一步投INT浮动,一步理解?假设我有一个签名整数是二进制格式。此外,我想投它用手工浮动。但是,我不能。因此,一个能告诉我该怎么做一步的转换步骤?

I have some no understanding about how one can cast int to float, step by step? Assume I have a signed integer number which is in binary format. Moreover, I want cast it to float by hand. However, I can't. Thus, CAn one show me how to do that conversion step by step?

我这样做转换在C中,多少次?等;

I do that conversion in c, many times ? like;

  int a = foo ( );
  float f = ( float ) a ;

不过,我还没有弄清楚在后台发生了什么。此外,要理解好了,我想这样做手工的转换。

But, I haven't figure out what happens at background. Moreover, To understand well, I want do that conversion by hand.

编辑:如果你很了解的转换,你也可以给有关浮到双转换信息。此外,浮到INT

If you know much about conversion, you can also give information about for float to double conversion. Moreover, for float to int

推荐答案

浮点值(IEEE754的人,无论如何)主要有三个组成部分:

Floating point values (IEEE754 ones, anyway) basically have three components:


  • 标志取值;

  • 一系列的指数位电子的;和

  • 一系列尾数位的 M

  • a sign s;
  • a series of exponent bits e; and
  • a series of mantissa bits m.

在precision决定有多少位用于指数和尾数。让我们来看一看单precision浮点值0.1:

The precision dictates how many bits are available for the exponent and mantissa. Let's examine the value 0.1 for single-precision floating point:

s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm    1/n
0 01111011 10011001100110011001101
           ||||||||||||||||||||||+- 8388608
           |||||||||||||||||||||+-- 4194304
           ||||||||||||||||||||+--- 2097152
           |||||||||||||||||||+---- 1048576
           ||||||||||||||||||+-----  524288
           |||||||||||||||||+------  262144
           ||||||||||||||||+-------  131072
           |||||||||||||||+--------   65536
           ||||||||||||||+---------   32768
           |||||||||||||+----------   16384
           ||||||||||||+-----------    8192
           |||||||||||+------------    4096
           ||||||||||+-------------    2048
           |||||||||+--------------    1024
           ||||||||+---------------     512
           |||||||+----------------     256
           ||||||+-----------------     128
           |||||+------------------      64
           ||||+-------------------      32
           |||+--------------------      16
           ||+---------------------       8
           |+----------------------       4
           +-----------------------       2

符号为正,这是pretty容易。

The sign is positive, that's pretty easy.

该指数是 64 + 32 + 16 + 8 + 2 + 1 = 123 - 127偏压= -4 ,所以乘数是2 -4 1/16 。偏置在那里,这样你可以得到真正的小数目(如10 -30 )以及路数。

The exponent is 64+32+16+8+2+1 = 123 - 127 bias = -4, so the multiplier is 2-4 or 1/16. The bias is there so that you can get really small numbers (like 10-30) as well as large ones.

尾数是矮胖。它由 1 的(隐式底座)加(与每个都是值得所有那些位1 /(2 N )为<$ C $ ç> N 开始于 1 键,向右增加), {1/2,1/16,1 / 32,1/256,1/512,1/4096,8192分之1,1/65536,一十三万一千零七十二分之一,一百〇四万八千五百七十六分之一,二百〇九万七千一百五十二分之一,八百三十八万八千六百○八分之一}

The mantissa is chunky. It consists of 1 (the implicit base) plus (for all those bits with each being worth 1/(2n) as n starts at 1 and increases to the right), {1/2, 1/16, 1/32, 1/256, 1/512, 1/4096, 1/8192, 1/65536, 1/131072, 1/1048576, 1/2097152, 1/8388608}.

当您添加所有这些,你得到 1.60000002384185791015625

When you add all these up, you get 1.60000002384185791015625.

当你乘上2 -4 乘数,你就会得到 0.100000001490116119384765625 ,这就是为什么他们说你不能再present 0.1 完全一样的IEEE754浮点。

When you multiply that by the 2-4 multiplier, you get 0.100000001490116119384765625, which is why they say you cannot represent 0.1 exactly as an IEEE754 float.

在方面的转换的整数花车,如果你有尾数的比特数(包括隐含1),你可以刚过转移整数位模式,并选择正确的指数。不会有precision损失。例如,一个双precision IEEE754(64位,这些是尾数52/53)已没有问题采取上的32位整数。

In terms of converting integers to floats, if you have as many bits in the mantissa (including the implicit 1), you can just transfer the integer bit pattern over and select the correct exponent. There will be no loss of precision. For example a double precision IEEE754 (64 bits, 52/53 of those being mantissa) has no problem taking on a 32-bit integer.

如果有更多的比特在整数(如32位整数,32位单precision浮子,其中只有具有尾数的23/24位)则需要缩放整数。

If there are more bits in your integer (such as a 32-bit integer and a 32-bit single precision float, which only has 23/24 bits of mantissa) then you need to scale the integer.

此涉及剥离的至少显著位(实际上四舍五入),这样它会装配到尾数位。这涉及当然precision损失但那是不可避免的。

This involves stripping off the least significant bits (rounding actually) so that it will fit into the mantissa bits. That involves loss of precision of course but that's unavoidable.

让我们看看在一个特定的值, 123456789 。下面的程序转储每个数据类型的位

Let's have a look at a specific value, 123456789. The following program dumps the bits of each data type.

#include <stdio.h>

static void dumpBits (char *desc, unsigned char *addr, size_t sz) {
    unsigned char mask;
    printf ("%s:\n  ", desc);
    while (sz-- != 0) {
        putchar (' ');
        for (mask = 0x80; mask > 0; mask >>= 1, addr++)
            if (((addr[sz]) & mask) == 0)
                putchar ('0');
            else
                putchar ('1');
    }
    putchar ('\n');
}

int main (void) {
    int intNum = 123456789;
    float fltNum = intNum;
    double dblNum = intNum;

    printf ("%d %f %f\n",intNum, fltNum, dblNum);
    dumpBits ("Integer", (unsigned char *)(&intNum), sizeof (int));
    dumpBits ("Float", (unsigned char *)(&fltNum), sizeof (float));
    dumpBits ("Double", (unsigned char *)(&dblNum), sizeof (double));

    return 0;
}

我的系统上的输出如下:

The output on my system is as follows:

123456789 123456792.000000 123456789.000000
integer:
   00000111 01011011 11001101 00010101
float:
   01001100 11101011 01111001 10100011
double:
   01000001 10011101 01101111 00110100 01010100 00000000 00000000 00000000

和我们将看看这些一次。首先整数,两个简单的权力:

And we'll look at these one at a time. First the integer, simple powers of two:

   00000111 01011011 11001101 00010101
        |||  | || || ||  || |    | | +->          1
        |||  | || || ||  || |    | +--->          4
        |||  | || || ||  || |    +----->         16
        |||  | || || ||  || +---------->        256
        |||  | || || ||  |+------------>       1024
        |||  | || || ||  +------------->       2048
        |||  | || || |+---------------->      16384
        |||  | || || +----------------->      32768
        |||  | || |+------------------->      65536
        |||  | || +-------------------->     131072
        |||  | |+---------------------->     524288
        |||  | +----------------------->    1048576
        |||  +------------------------->    4194304
        ||+---------------------------->   16777216
        |+----------------------------->   33554432
        +------------------------------>   67108864
                                         ==========
                                          123456789

现在让我们来看看单precision浮动。注意整数匹配一个近乎完美的比赛尾数的位模式:

Now let's look at the single precision float. Notice the bit pattern of the mantissa matching the integer as a near-perfect match:

mantissa:       11 01011011 11001101 00011    (spaced out).
integer:  00000111 01011011 11001101 00010101 (untouched).

有一个 1 位尾数的左边,它也一直在另一端圆润,这就是那损失precision来自(从更改值123456789 123456792 如从以上程序的输出)。

There's an implicit 1 bit to the left of the mantissa and it's also been rounded at the other end, which is where that loss of precision comes from (the value changing from 123456789 to 123456792 as in the output from that program above).

工作了值:

s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm    1/n
0 10011001 11010110111100110100011
           || | || ||||  || |   |+- 8388608
           || | || ||||  || |   +-- 4194304
           || | || ||||  || +------  262144
           || | || ||||  |+--------   65536
           || | || ||||  +---------   32768
           || | || |||+------------    4096
           || | || ||+-------------    2048
           || | || |+--------------    1024
           || | || +---------------     512
           || | |+-----------------     128
           || | +------------------      64
           || +--------------------      16
           |+----------------------       4
           +-----------------------       2

的符号为正。该指数是 128 + 16 + 8 + 1 = 153 - 127偏差= 26 ,所以乘数是2 26 67108864

尾数为 1 (隐式底座)加(如上所述), {1/2,1/4,1/16 ,1/64,1/128,1/512,1/1024,二千〇四十八分之一,1/4096,32768分之1,1/65536,262144分之1,四百一十九万四千三百零四分之一,八百三十八万八千六百○八分之一} 。当您添加所有这些,你得到 1.83964955806732177734375

The mantissa is 1 (the implicit base) plus (as explained above), {1/2, 1/4, 1/16, 1/64, 1/128, 1/512, 1/1024, 1/2048, 1/4096, 1/32768, 1/65536, 1/262144, 1/4194304, 1/8388608}. When you add all these up, you get 1.83964955806732177734375.

当你乘上2 26 乘数,你就会得到 123456792 ,一样的程序输出。

When you multiply that by the 226 multiplier, you get 123456792, the same as the program output.

双位掩码输出是:

s eeeeeeeeeee mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
0 10000011001 1101011011110011010001010100000000000000000000000000

我的的要经过搞清楚那兽:-)但是价值的过程中,我的将会的旁边展示整数格式尾数重新显示presentation公共位:

I am not going to go through the process of figuring out the value of that beast :-) However, I will show the mantissa next to the integer format to show the common bit representation:

mantissa:       11 01011011 11001101 00010101 000...000 (spaced out).
integer:  00000111 01011011 11001101 00010101           (untouched).

您可以再次见到共性与左边和隐含位右边的大得多位的可用性,这就是为什么有在这种情况下没有precision的损失。

You can once again see the commonality with the implicit bit on the left and the vastly greater bit availability on the right, which is why there's no loss of precision in this case.

在花车和双打之间的转换方面,这也是相当容易理解了。

In terms of converting between floats and doubles, that's also reasonably easy to understand.

您首先要检查的特殊的值,如NaN和无穷大。这些都是由专门的指数/尾数组合表示,其可能更容易检测这些前面ANG生成新格式的等效。

You first have to check the special values such as NaN and the infinities. These are indicated by special exponent/mantissa combinations and it's probably easier to detect these up front ang generate the equivalent in the new format.

然后,在你从双会浮动的情况下,你明明有较少的为您提供了一系列由于在指数少位。如果你的双是一个浮动的范围之外,则需要处理这个问题。

Then in the case where you're going from double to float, you obviously have less of a range available to you since there are less bits in the exponent. If your double is outside the range of a float, you need to handle that.

假设它会适合,你再需要:

Assuming it will fit, you then need to:


  • 变基指数(偏置是两类不同)。

  • 副本从尾数有多少位为适合(必要时四舍五入)。

  • 填充了目标尾数的其余部分(如果有的话)零位。

这篇关于在什么样的背景发生在转换INT浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆