mardi 30 juillet 2019

Is it possible to return a printed value into a variable?

I am currently doing a project involving the conversion of decimal numbers into IEEE 754 Floating Point representation. I am making use of a code provided by GeeksforGeeks for the conversion process and edited it to suit my project.

I am unsure as to how to modify the code so that it returns the appropriate Floating Point representation into the variables as the variables currently stores the decimal values instead.

I ran into the issue where the function being used to convert said decimal values is just simply printing the Floating Point representation. I would need to be able to return the printed Floating Point representation value into a variable. Is that possible? Else, is there an alternative method?

// 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;
}

This code correctly converts the decimal value into the intended Floating Point representation. However, if I were to print the variable as it is using printf(finalMant) instead of printBinary, it would display as a decimal value instead of a Floating Point representation. This might be due to the lack of any return values.

Aucun commentaire:

Enregistrer un commentaire