How to calculate logarithm for float numbers in Python

2 Answers

0 votes
import math 
  
for i in [0.1, 1.3, 2.6, 3.14, 4, 0]:         
   try:    
     print('{:2.1f} {:.18f}'.format(i, math.lgamma(i)))            
   except ValueError as err:                
     print('Error calculate lgamma({}): {}'.format(i, err))



'''
run:

0.1 2.252712651734205540
1.3 -0.108174809507860292
2.6 0.357411863548979358
3.1 0.826138704777028998
4.0 1.791759469228055401
Error calculate lgamma(0): math domain error

'''

 



answered Jul 13, 2019 by avibootz
0 votes
import math 
  
for i in [0.1, 1.3, 2.6, 3.14, 4, 0]:         
   try:    
     print('{:2.1f} {:.18f}'.format(i, math.log(math.gamma(i))))            
   except ValueError as err:                
     print('Error calculate lgamma({}): {}'.format(i, err))



'''
run:

0.1 2.252712651734205984
1.3 -0.108174809507860237
2.6 0.357411863548979802
3.1 0.826138704777028332
4.0 1.791759469228054957
Error calculate lgamma(0): math domain error

'''

 



answered Jul 13, 2019 by avibootz

Related questions

2 answers 232 views
1 answer 148 views
1 answer 231 views
1 answer 218 views
1 answer 184 views
...