-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (53 loc) · 1.58 KB
/
Copy pathutils.py
File metadata and controls
65 lines (53 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Math library
# Author: Sébastien Combéfis
# Version: February 2, 2016
from math import*
def fact(n):
"""Computes the factorial of a natural number.
Pre: -
Post: Returns the factorial of 'n'.
Throws: ValueError if n < 0
"""
try:
if n > 0:
x = 1
for i in range(2, n+1):
x *= i
return x
else:
raise ValueError
except ValueError:
print("Entier négatif")
def roots(a, b, c):
"""Computes the roots of the ax^2 + bx + x = 0 polynomial.
Pre: -
Post: Returns a tuple with zero, one or two elements corresponding
to the roots of the ax^2 + bx + c polynomial.
"""
delta = b*b - 4 * a * c
if delta == 0:
return (0,0,0)
if delta < 0:
return ("None", "None", delta)
if delta > 0:
x1 = (-b + sqrt(delta))/(2*a)
x2 = (-b - sqrt(delta))/(2*a)
return (x1, x2, delta)
def integrate(function, lower, upper):
"""Approximates the integral of a fonction between two bounds
Pre: 'function' is a valid Python expression with x as a variable,
'lower' <== 'upper',
'function' continuous and integrable between 'lower‘ and 'upper'.
Post: Returns an approximation of the integral from 'lower' to 'upper'
of the specified 'function'.
"""
x = lower
som = 0
while x <= upper:
som += eval(function) * 0.01
x += 0.01
return som
if __name__ == '__main__':
print(fact(-1))
print(roots(1, 6, 5))
print(integrate('x ** 2 - 1', -1, 1))