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
|
import math
valeur = math.log(125) / math.log(5)
print("valeur = math.log(125) / math.log(5)", valeur) # affiche 3.0000000000000004
print("valeur.is_integer()", valeur.is_integer()) # retourne False
print()
def est_entier_avec_tolerance(nombre, tolerance) :
arrondi = round(nombre)
return abs(nombre - arrondi) < tolerance
tolerance = 10 ** -8
print("round(valeur)", round(valeur)) # affiche 3
print(
"est_entier_avec_tolerance(valeur, tolerance)"
, est_entier_avec_tolerance(valeur, tolerance) # retourne True
)
print(
"est_entier_avec_tolerance(2.00000001, tolerance)"
, est_entier_avec_tolerance(2.00000001, tolerance) # retourne True
)
print(
"est_entier_avec_tolerance(2.0000001, tolerance)"
, est_entier_avec_tolerance(2.0000001, tolerance) # retourne False
) |
Partager