Advertisement
jacknpoe

Validações de números ponto flutuante em Python

Jan 21st, 2024 (edited)
1,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.41 KB | Software | 0 0
  1. # validações de números ponto flutuante
  2. print(0.4 + 0.1 == 0.5)
  3. print(0.4 + 0.2 == 0.6)
  4. print("-------")
  5.  
  6. # validação usando decimal
  7. from decimal import Decimal
  8. print(Decimal('0.4') + Decimal('0.1') == Decimal('0.5'))
  9. print(Decimal('0.4') + Decimal('0.2') == Decimal('0.6'))
  10. print("-------")
  11.  
  12. precisao = 0.1 ** 15
  13. print(abs(0.4+0.1-0.5)<precisao)
  14. print(abs(0.4+0.2-0.6)<precisao)
  15. print("-------")
  16.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement