0

I am currently trying to convert a decimal value into IEEE 754 Half Precision Floating Point format but ran into some issues with the conversion. Currently this code only works in converting into Single Precision format.

If I am not wrong, it defaults converts decimal values into Single Precision format. Otherwise, which line of the code is doing that?

Otherwise, is there an alternative way to solve my issue?

#include <limits.h>
#include <iostream>
#include <math.h>
#include <bitset>

// Get 32-bit IEEE 754 format of the decimal value
std::string GetBinary32( float value )
{
    union
    {
         float input;   // assumes sizeof(float) == sizeof(int)
         int   output;
    }    data;

    data.input = value;

    std::bitset<sizeof(float) * CHAR_BIT>   bits(data.output);

    std::string mystring = bits.to_string<char, 
                                          std::char_traits<char>,
                                          std::allocator<char> >();

    return mystring;
}


int main()
{
    // Convert 19.5 into IEEE 754 binary format..
    std::string str = GetBinary32( (float) 19.5 );
    std::cout << "Binary equivalent of 19.5:" << std::endl;
    std::cout << str << std::endl << std::endl;

    return 0;
}

The output of the above code would give 01000001100111000000000000000000

However, I would want to convert it into a 16 bit format.

EDIT: NOT a duplicate since this is not relating to precision error.

Kai
  • 31
  • 4
  • 1
    @user207421: How do you figure a question about converting a value to a 16-bit floating-point representation is a duplicate about converting a 16-bit floating-point representation to a value? – Eric Postpischil Aug 06 '19 at 10:38
  • Although the question asks to convert a “decimal” to half precision, the declaration of `GetBinary32` suggests you want to convert a `float` to half precision. `float` most often uses a binary representation. “Decimal” is not a proper term to use to mean real numbers generally or the number representable with `float`. If you do want to convert IEEE-754 binary32 to binary16, there appears to be a complete solution [here](https://stackoverflow.com/questions/1659440/32-bit-to-16-bit-floating-point-conversion). It returns the result in a `uint16_t`, which you can easily display in binary. – Eric Postpischil Aug 07 '19 at 01:10

1 Answers1

1

There is no built-in type for half precision (16 bit) floating point in c++. You have to calculate the value yourself.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
MofX
  • 1,569
  • 12
  • 22