Advertisement
Guest User

python numbers

a guest
Jan 30th, 2014
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. print '-'*60
  2.  
  3. n = 0.387951
  4.  
  5. print n                             # 0.387951
  6.  
  7. print round(n, 3)                   # 0.388
  8. print format(n, '.18f')             # 0.387950999999999990
  9. print n*0.000001                    # 3.87951e-07
  10. print format(n*0.000001, '.18f')    # 0.000000387951000000
  11. print '%.8f' % n                    # 0.38795100
  12. print '%.10f' % n                   # 0.3879510000
  13. print '%.12f' % n                   # 0.387951000000
  14. print '%.14f' % n                   # 0.38795100000000
  15. print '%.16f' % n                   # 0.3879510000000000    <- sechzehnte Nachkommastelle noch "in Orndung"
  16. print '%.17f' % n                   # 0.38795099999999999   <- ab siebzehnter Nachkommastelle faellt die Abweichung auf
  17. print '%.18f' % n                   # 0.387950999999999990
  18.  
  19.  
  20. print n                             # 0.387951 <- n ist nach wie vor unveraendert
  21.  
  22.  
  23.  
  24. print '-'*60
  25.  
  26. n = 0.1
  27.  
  28. print n                             # 0.1
  29.  
  30. print round(n, 3)                   # 0.1
  31. print format(n, '.18f')             # 0.100000000000000006
  32. print n*0.000001                    # 1e-07
  33. print format(n*0.000001, '.18f')    # 0.000000100000000000
  34. print '%.8f' % n                    # 0.10000000
  35. print '%.10f' % n                   # 0.1000000000
  36. print '%.12f' % n                   # 0.100000000000
  37. print '%.14f' % n                   # 0.10000000000000
  38. print '%.16f' % n                   # 0.1000000000000000    <- sechzehnte Nachkommastelle noch "in Orndung"
  39. print '%.17f' % n                   # 0.10000000000000001   <- ab siebzehnter Nachkommastelle faellt die Abweichung auf
  40. print '%.18f' % n                   # 0.100000000000000006
  41.  
  42.  
  43. print '-'*60
  44.  
  45. a, b, c = 0.1, 0.1, 0.1
  46.  
  47. print a + b + c                     # 0.3
  48. print a + b + c == 0.3              # False
  49. print '%.16f' % a                   # 0.1000000000000000
  50. print '%.17f' % a                   # 0.10000000000000001
  51. print '%.17f' % (a+b+c)             # 0.30000000000000004
  52.  
  53.  
  54.  
  55. print '-'*60
  56.  
  57. import decimal
  58. from decimal import Decimal, Context
  59.  
  60. print Decimal(0.1)                  # 0.1000000000000000055511151231257827021181583404541015625
  61. print Decimal('0.1')                # 0.1
  62.  
  63. a = Decimal('0.1')
  64. b = Decimal('0.1')
  65. c = Decimal('0.1')
  66.  
  67. print a + b + c == Decimal('0.3')   # True
  68. print a + b + c == 0.3              # False
  69.  
  70. print format(Decimal('0.1'), '.20f')    # 0.10000000000000000000
  71. print format(Decimal(0.1), '.20f')      # 0.10000000000000000555
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement