【问题标题】:Is it possible to return a printed value into a variable?是否可以将打印值返回到变量中?
【发布时间】:2019-07-30 08:09:42
【问题描述】:

我目前正在做一个项目,涉及将十进制数转换为 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 打印变量,它将显示为十进制值而不是浮点表示。这可能是由于缺少任何返回值。

【问题讨论】:

标签: c++ c++11


【解决方案1】:

根据 cmets 中的说明,您无需“返回”任何内容。只需声明myfloat类型的变量,将float放入f成员,就可以得到signexponentmantissa元素。 IE。像这样:

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

请记住,这仅对 C 有效,对 C++ 无效。 (尽管大多数编译器都支持这一点,可能是为了与 C 兼容,但标准不保证也不鼓励这样做。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2013-04-19
  • 2017-08-02
  • 2022-11-04
  • 1970-01-01
  • 2010-11-15
相关资源
最近更新 更多