0

I need help with finding algorithm converting hex to IEEE-754 single-precision floating point number (without coding). I couldn't find any on the internet :(

For example, given input string 0xB9CD542, we need to return string 0x1.39aa84p-104 where last number is in hexadecimal exponential form (single precision floating point number) rounded to 0

regina
  • 27
  • 3
  • 2
    What sort of hex? Can you provide an example input and output – Alex K Mar 09 '24 at 22:20
  • 1
    What does "hex" mean? Does it mean you are looking at a bit pattern like 0x3f800000 and want to determine its numeric value, 1.0 in this case? If so, there are tools for that, e.g. this randomly selected website. There are also numerous websites explaining the format, e.g. this randomly selected one – njuffa Mar 09 '24 at 22:21
  • @AlexK for example, given input string B9CD54, we need to return string 0x1.39aa84p-104 where last number is in hexadecimal exponential form (single precision floating point number) rounded to 0 – regina Mar 09 '24 at 22:24
  • @njuffa I checked these websites, but they didn't provide algorithms... first one uses built-in functions – regina Mar 09 '24 at 22:26
  • 1
    @regina Converting 0xB9CD54 to 0x1.39aa84p-104 is not a math problem, so this looks like a programming question suitable for Stackoverflow. There should be no rounding (including truncation = round-toward-zero) involved when representing an IEEE-754 operand as a hex float. FWIW, as someone who does these kind of representation changes and conversions almost every day, I do not see how 0xB9CD54 maps to 0x1.39aa84p-104. Did you mean 0xb9cd542? – njuffa Mar 09 '24 at 22:41
  • 1
    A single-precision floating-point number is generally eight hex digits in length. So your 6-digit example is not typical! – TonyK Mar 09 '24 at 22:44
  • 1
    You might get different responses at the Computational Science stack. – A rural reader Mar 09 '24 at 22:45
  • @njuffa I'm sorry, yes, I meant that :( – regina Mar 09 '24 at 23:09
  • Does the conversion example from Wikipedia answer your question? – peterwhy Mar 09 '24 at 23:20
  • @regina Since the desired representation change is from raw hex to hexfloat, this boils down to a few logical operations for bit-field extraction, a few shifts, and the correct use of format specifiers: worked example. I do not consider this a mathematical problem. – njuffa Mar 10 '24 at 00:14

1 Answers1

1

OK, I'll bite:

If 0xB9CD54 (binary 00000000101110011100110101010100) is an IEEE-754 single-precision number, then it can be decomposed as follows:

Sign bit = 0, so number is positive
Biased exponent = 00000001, so unbiased exponent is -126
Mantissa (including implied leading 1) = 101110011100110101010100 = 0xB9CD54

Shifting the mantissa left by 1 so that it starts with 1. gives 1.739AA8.

So we end up with 0x1.739AA8p-126.

Conversely, the decimal form of 0x1.39aa84p-104 would seem to be 0xb9CD542 according to this site. So it looks like you need to check your sources!

TonyK
  • 64,559