在将double交换为long double值后,为什么会得到nan?

问题描述

我必须为我的课程编写一个程序,该程序交换一些长double值的二进制表示形式的2组位。我知道在IEEE-754标准中,当整个指数都充满时,会出现nan。但就我而言,它还没有完全填满,所以我认为没有理由放弃。我不知道为什么会这样。我调试了很多次代码,它似乎可以正常工作。我认为与char数组的对话并返回至十进制是正确的,因为当我从main中删除ld_switch_chunks函数时,程序将打印与开始时相同的数字。

#include <stdio.h>

typedef union b_long_double
{
    char bin[16];
    long double num;
}b_long_double;



// char array to long double conversation
void transform_to_ld(const char * in)
{
    b_long_double tmp;
    tmp.num=0;
    for(int i=0; i<16; ++i)
        tmp.bin[i] = 0;
    for(int i=0; i<16; ++i)
    {
        char total = 0;
        for(int j=0; j<8;++j)
        {
            char current = 0;
            if(in[(i*8)+j] == 1)
            {
                current += 1;
                current <<= j;
                total |= current;
            }
        }
        tmp.bin[i] = total;
    }
    printf("%Lf\n",tmp.num);
}


//splits long double value to array of chars 
void split_ld_to_bin(const char* in,char* out)
{
    for(int i=0; i<16; ++i)
    {
        char current = in[i];
        for(int j=0; j<8;++j)
        {
            int index = i*8+j;
            out[index] = current & 1;
            current >>= 1;
        }
    }

}


//swaps 2 chunks of bits
void ld_switch_chunks(char * in)
{
    int first_bit=77; //lead bit of first chunk
    int second_bit=63; //lead bit of second chunk
    int length=6; // length of chunk

    if(first_bit < 0 || first_bit > 79)
    {
        printf("Invalid input\n");
        return;
    }

    if(second_bit < 0 || second_bit > 79 || first_bit == second_bit)
    {
        printf("Invalid input\n");
        return;
    }

    if(length <= 0 || length > 40 || (second_bit-length) < 0 || (first_bit-length) < 0
       || ( second_bit>first_bit && (second_bit-length) <= first_bit) || (first_bit>second_bit && (first_bit-length) <= second_bit))
    {
        printf("Invalid input\n");
        return;
    }


    char tmp = 0;
    if(first_bit > second_bit)
        for(int i=0; i<length; ++i)
        {
            tmp = in[second_bit-i];
            in[second_bit-i] = in[first_bit-i];
            in[first_bit-i] = tmp;
        }
    else
        for(int i=0; i<length; ++i)
        {
            tmp = in[first_bit-i];
            in[first_bit-i] = in[second_bit-i];
            in[second_bit-i] = tmp;
        }
}


void print_ld(char* ld_array)
{
    for(int i=79; i>=0; --i)
        printf("%d",ld_array[i]);
    printf("\n");
}


int main()
{
    b_long_double input;
    input.num = 15.375; // input number
    char binary[128] = {};
    split_ld_to_bin(input.bin,binary);
    print_ld(binary);


    ld_switch_chunks(binary);
    print_ld(binary);


    transform_to_ld(binary);
}

输出为:

01000000000000101111011000000000000000000000000000000000000000000000000000000000
01111101000000100000001000000000000000000000000000000000000000000000000000000000
nan

,其中第一行是初始数据的二进制,第二行是具有交换集的二进制,第三行必须是新的long double值(由位交换引起)。 我想知道发生这种情况的原因。

解决方法

在Wikipedia上浏览有关x86 80-bit extended precision format的页面(我认为这是OP所困扰的页面),我们可以找到一张有关“ x86 Extended Precision值的字段解释”的表。

它说,大约 63 位为零(如OP的示例):

不正常。仅在8087和80287上生成。80387及更高版本将其视为无效的操作数。
...
与单精度和双精度格式相比,此格式不使用隐式/隐藏位。相反,位63包含有效数的整数部分,而位62-0保留小数部分。在所有归一化的数字上,第63位将为1。

这应该解释NaN。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...