Home > Software design >  Problem with implementation method Get IEEE 754 Format, when parameter is double and return type sho
Problem with implementation method Get IEEE 754 Format, when parameter is double and return type sho

Time:05-04

        public static string GetIEEE754Format(this double number)
        {
            return number.ToString();
        }

Is it an easy way to implement this method ? Can someone explain how to implement this function in C# code ?

CodePudding user response:

IEEE 754 is not related at all to the string representation of a floating point number, but rather to the binary representation. Therefore it doesn't make sense to convert a double to string in this context.

For example if your floating type is double (as in your example), the IEEE 754 standard specifies that the 8 bytes that are required to represent it have the following structure:

  1. The first bit holds the sign.
  2. Next 11 bits hold the exponent (offseted by -1023 to be precise).
  3. Next 52 bits hold the mantisa.

The value of such a variable is:

 (-1)^(sign) * (1.<mantisa>) * 2^(<exponent>-1023)

Altogether it's 64 bits that correspond to those 8 bytes. Any double variable has this structure already (in a system using IEEE 754, which is very common).

A similar defintion exists in the IEEE 754 standard for 32 bits float values.

You can see more info here: IEEE 754 - Wikipedia

CodePudding user response:

In docuentation You have method to convert data types to bytes. See: https://docs.microsoft.com/en-us/dotnet/api/system.bitconverter.doubletoint64bits?view=net-6.0

  • Related