是否可以将打印的值返回到变量?

时间:2019-07-30 08:09:42

标签: c++ c++11

我目前正在做一个涉及将十进制数字转换为IEEE 754浮点表示形式的项目。我正在使用GeeksforGeeks提供的代码进行转换,并对其进行了编辑以适合我的项目。

我不确定如何修改代码,以使其将适当的浮点表示形式返回到变量中,因为变量当前存储的是十进制值。

我遇到了一个问题,其中用于转换所述十进制值的函数只是简单地打印浮点表示。我将需要能够将打印的浮点表示值返回到变量中。那可能吗?否则,还有其他方法吗?

// C program to convert a real value
// to IEEE 754 floating point representaion

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;

int modifyBit(int n, int p, int b)
{
    int mask = 1 << p;
    return (n & ~mask) | ((b << p) & mask);
}


void printBinary(int n, int i)
{

    // Prints the binary representation
    // of a number n up to i-bits.
    int k;
    for (k = i - 1; k >= 0; k--) {

        if ((n >> k) & 1)
            printf("1");
        else
            printf("0");
    }
}

typedef union {

    float f;
    struct
    {

        // Order is important.
        // Here the members of the union data structure
        // use the same memory (32 bits).
        // The ordering is taken
        // from the LSB to the MSB.
        unsigned int mantissa : 23;
        unsigned int exponent : 8;
        unsigned int sign : 1;

    } raw;
} myfloat;

// Function to convert real value
// to IEEE foating point representation
void printIEEE(myfloat var)
{

    // Prints the IEEE 754 representation
    // of a float value (32 bits)

    printf("%d | ", var.raw.sign);
    printBinary(var.raw.exponent, 8);
    printf(" | ");
    printBinary(var.raw.mantissa, 23);
    printf("\n");
}

// Driver Code
int main()
{

    // Instantiate the union
    myfloat var, var2;
    int sub, sub2, mant, pos, finalMant;


    // Get the real value
    var.f = 1.25;
    var2.f = 23.5;


    // Get the IEEE floating point representation
    printf("IEEE 754 representation of %f is : \n",
           var.f);
    printIEEE(var);
    printf("No 2: %f : \n", var2.f);
    printIEEE(var2);
    printf("\n");

    //Get the exponent value for the respective variable
    //Need to compare which exponent is bigger
    printBinary(var2.raw.exponent, 8);
    printf("\n");
    printf("Exponent of Var in Decimal: %d: \n", var.raw.exponent);
    printf("Exponent of Var2 in Decimal: %d: \n", var2.raw.exponent);

    if (var.raw.exponent > var2.raw.exponent)
    {
        printf("1st is bigger than 2 \n");

        //Difference in exponent
        sub = var.raw.exponent - var2.raw.exponent;
        printf("Difference in exponent: %d \n", sub);

        //New mantissa with the new right shift
        mant = var2.raw.mantissa>>sub;

        //Modifying the hidden bit to be included into the mantissa
        pos = 23 - sub;
        finalMant = modifyBit(mant, pos, 1);

        //Print the final mantissa
        printf("New Binary : \n");
        printBinary(finalMant, 23);
    }
    else
    {
        printf("2nd bigger than 1 \n");

        //Difference in exponent
        sub = var2.raw.exponent - var.raw.exponent;
        printf("Difference in exponent: %d \n", sub);

        //New mantissa with the new right shift
        mant = var.raw.mantissa>>sub;

        //Modifying the hidden bit to be included into the mantissa
        pos = 23 - sub;
        finalMant = modifyBit(mant, pos, 1);

        //Print the final mantissa
        printf("New Binary : \n");
        printBinary(finalMant, 23);
    }
    return 0;
}

此代码将十进制值正确转换为预期的浮点表示形式。但是,如果我使用printf(finalMant)而不是printBinary来打印变量,它将显示为十进制值而不是浮点表示形式。这可能是由于缺少任何返回值。

1 个答案:

答案 0 :(得分:0)

基于澄清说明,您无需“返回”任何内容。只需声明类型为myfloat的变量,将float放入f成员中,就可以获取signexponentmantissa元素。即像这样的东西:

    myfloat cnv;
    cnv.f = someFloatVal;
    unsigned int mantissa = cnv.mantissa;

请记住,这仅对C有效,对C ++无效。 (尽管大多数编译器都支持此功能,可能与C兼容,但从标准上不能保证并且不鼓励这样做。)