Change language

# IEEE 754 Hexadecimal Floating Number Python Program

Given a floating point number, the challenge is to find the hexadecimal representation for the IEEE 754 number.

IEEE Standard for Floating Point Arithmetic (IEEE 754) — it is a technical standard for floating point computing that was established in 1985 by the Institute of Electrical and Electronic Engineers (IEEE). The standard addresses many of the problems found in various floating point implementations that make them difficult to use reliably and reduces portability. The IEEE 754 floating point standard is today the most common representation for real numbers on computers, including Intel-based PCs, Macs and most Unix platforms.

Examples:

`  Input:  -6744.90  Output:  C5D2C733  Input:  -263.3  Output:  C383A666 `

Suitable:

• Check if the number is positive or negative. Store the sign as 0 for positive and 1 for negative, then convert the number to positive if negative.
• Convert the number with floating point to binary .
• Separate the decimal part and the integer part.
• Calculate the exponent ( E) and convert it to binary.
• Find the mantissa.
• Concatenate the sign of the mantissa, degree and mantissa.
• Convert this to hex.

Let’s write a Python program to represent a floating number in IEEE 754 hexadecimal format.

 ` # Function to convert decimal to binary ` ` def ` ` float_bin (number, places ` ` = ` ` 3 ` `): ` ` ` ` whole, dec ` ` = ` ` str ` ` (number) .split (` ` "." ` `) ` ` whole ` ` = ` ` int ` ` (whole) ` ` dec ` ` = ` ` int ` ` (dec) ` ` res ` ` = ` ` bin ` ` (whole) .lstrip (` ` "0b" ` `) ` ` + ` `". "` ` `  ` for ` ` x ` ` in ` ` range ` ` (places): ` ` whole, dec ` ` = ` ` str ` ` ((decimal_converter (dec)) ` ` * ` ` 2 ` `). Split (` ` "." ` `) ` ` dec ` ` = ` ` int ` ` (dec) ` ` ` ` res ` ` + ` ` = ` ` whole ` ` return ` ` res `   ` def ` ` decimal_converter (num): ` ` ` ` while ` ` num" ` ` 1 ` `: ` ` num ` ` / ` ` = ` ` 10 ` ` return ` ` num `   ` def ` ` IEEE754 (n) : `   ` # determine if the number is ` ` ` ` # is positive or negative ` ` sign ` ` = ` ` 0 ` ` if ` ` n & lt; ` ` 0 ` `: ` ` sign ` ` = ` ` 1 ` ` n ` ` = ` ` n ` ` * ` ` (` ` - ` ` 1 ` `) ` ` p ` ` = ` ` 30 `   ` # convert float to binary ` ` dec ` ` = ` ` float_bin (n, places ` ` = ` ` p) ` < / p>   ` # separate decimal part ` ` # and integer part ` ` whole, dec ` ` = ` ` str ` ` (dec) .split (` ` "." ` `) ` ` whole ` ` = ` ` int ` ` (whole) ` ` `  ` # calculating the exponent (E) ` ` exponent ` ` = ` ` len ` ` ( < / code> str (whole)) - 1 ``   exponent_bits = 127 + exponent   # convert degree from # decimal to binary   exponent_bits = bin (exponent_bits) .lstrip ( " 0b " )   # find the mantissa mantissa = str (whole) [ 1 : exponent + 1 ] mantissa = mantissa + dec mantissa = mantissa [ 0 : 23 ]   # IEEE754 notation in binary   final = str (sign) + str (exponent_bits) + mantissa   # convert binary to hex hstr = ’% 0 * X’ % (( len (final ) + 3 ) / / 4 , int (final, 2 ))   # return response to driver code return (hstr)   Driver code if __ name__ = = "__ main__" : print (IEEE754 ( 263.3 )) print (IEEE754 ( - 263.3 )) `

Exit :

` 4383A666 C383A666 `

## Shop

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best laptop for development

\$499+

Best laptop for Cricut Maker

\$299+

Best laptop for hacking

\$890

Best laptop for Machine Learning

\$699+

Raspberry Pi robot kit

\$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries