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

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

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

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


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

Python add suffix / add prefix to strings in a list

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

Python - Move item to the end of the list

Python - Print list vertically