How to calculate factorial 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:    
     # math.gamma(i) = (i - 1)!         
     print('{:2.1f} {:5.2f}'.format(i, math.gamma(i)))         
   except ValueError as err:             
     print('Error calculate gamma({}): {}'.format(i, err))



'''
run:

0.1  9.51
1.3  0.90
2.6  1.43
3.1  2.28
4.0  6.00
Error calculate gamma(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:    
     # math.gamma(i) = (i - 1)!         
     print('{:2.1f} {:5.2f}'.format(i, math.gamma(i + 1)))         
   except ValueError as err:             
     print('Error calculate gamma({}): {}'.format(i, err))



'''
run:

0.1  0.95
1.3  1.17
2.6  3.72
3.1  7.17
4.0 24.00
0.0  1.00

'''

 



answered Jul 13, 2019 by avibootz

Related questions

1 answer 188 views
2 answers 198 views
1 answer 249 views
1 answer 235 views
1 answer 159 views
...