How to use frexp() to get the mantissa and exponent of float in Python

1 Answer

0 votes
import math
 
print('{:^6} {:^6} {:^6}'.format('f', 'mant', 'exp'))     
print('{:-^6} {:-^6} {:-^6}'.format('', '', ''))     
 
for f in [0.1, 0.3, 0.5, 1.0, 5.0]:         
    mant, exp = math.frexp(f)         
    print('{:5.2f} {:6.2f} {:5d}'.format(f, mant, exp))



'''
run:

  f     mant   exp
------ ------ ------
 0.10   0.80    -3
 0.30   0.60    -1
 0.50   0.50     0
 1.00   0.50     1
 5.00   0.62     3

'''

 



answered Jun 30, 2019 by avibootz
...