Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,961 questions

51,903 answers

573 users

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 162 views
2 answers 167 views
1 answer 233 views
1 answer 195 views
1 answer 140 views
...