/Lib/test/ieee754.txt

http://unladen-swallow.googlecode.com/ · Plain Text · 183 lines · 156 code · 27 blank · 0 comment · 0 complexity · 0a312a4d1b352b81437355f3cc9ac212 MD5 · raw file

  1. ======================================
  2. Python IEEE 754 floating point support
  3. ======================================
  4. >>> from sys import float_info as FI
  5. >>> from math import *
  6. >>> PI = pi
  7. >>> E = e
  8. You must never compare two floats with == because you are not going to get
  9. what you expect. We treat two floats as equal if the difference between them
  10. is small than epsilon.
  11. >>> EPS = 1E-15
  12. >>> def equal(x, y):
  13. ... """Almost equal helper for floats"""
  14. ... return abs(x - y) < EPS
  15. NaNs and INFs
  16. =============
  17. In Python 2.6 and newer NaNs (not a number) and infinity can be constructed
  18. from the strings 'inf' and 'nan'.
  19. >>> INF = float('inf')
  20. >>> NINF = float('-inf')
  21. >>> NAN = float('nan')
  22. >>> INF
  23. inf
  24. >>> NINF
  25. -inf
  26. >>> NAN
  27. nan
  28. The math module's ``isnan`` and ``isinf`` functions can be used to detect INF
  29. and NAN:
  30. >>> isinf(INF), isinf(NINF), isnan(NAN)
  31. (True, True, True)
  32. >>> INF == -NINF
  33. True
  34. Infinity
  35. --------
  36. Ambiguous operations like ``0 * inf`` or ``inf - inf`` result in NaN.
  37. >>> INF * 0
  38. nan
  39. >>> INF - INF
  40. nan
  41. >>> INF / INF
  42. nan
  43. However unambigous operations with inf return inf:
  44. >>> INF * INF
  45. inf
  46. >>> 1.5 * INF
  47. inf
  48. >>> 0.5 * INF
  49. inf
  50. >>> INF / 1000
  51. inf
  52. Not a Number
  53. ------------
  54. NaNs are never equal to another number, even itself
  55. >>> NAN == NAN
  56. False
  57. >>> NAN < 0
  58. False
  59. >>> NAN >= 0
  60. False
  61. All operations involving a NaN return a NaN except for the power of *0* and *1*.
  62. >>> 1 + NAN
  63. nan
  64. >>> 1 * NAN
  65. nan
  66. >>> 0 * NAN
  67. nan
  68. >>> 1 ** NAN
  69. 1.0
  70. >>> 0 ** NAN
  71. 0.0
  72. >>> (1.0 + FI.epsilon) * NAN
  73. nan
  74. Misc Functions
  75. ==============
  76. The power of 1 raised to x is always 1.0, even for special values like 0,
  77. infinity and NaN.
  78. >>> pow(1, 0)
  79. 1.0
  80. >>> pow(1, INF)
  81. 1.0
  82. >>> pow(1, -INF)
  83. 1.0
  84. >>> pow(1, NAN)
  85. 1.0
  86. The power of 0 raised to x is defined as 0, if x is positive. Negative
  87. values are a domain error or zero division error and NaN result in a
  88. silent NaN.
  89. >>> pow(0, 0)
  90. 1.0
  91. >>> pow(0, INF)
  92. 0.0
  93. >>> pow(0, -INF)
  94. Traceback (most recent call last):
  95. ...
  96. ValueError: math domain error
  97. >>> 0 ** -1
  98. Traceback (most recent call last):
  99. ...
  100. ZeroDivisionError: 0.0 cannot be raised to a negative power
  101. >>> pow(0, NAN)
  102. nan
  103. Trigonometric Functions
  104. =======================
  105. >>> sin(INF)
  106. Traceback (most recent call last):
  107. ...
  108. ValueError: math domain error
  109. >>> sin(NINF)
  110. Traceback (most recent call last):
  111. ...
  112. ValueError: math domain error
  113. >>> sin(NAN)
  114. nan
  115. >>> cos(INF)
  116. Traceback (most recent call last):
  117. ...
  118. ValueError: math domain error
  119. >>> cos(NINF)
  120. Traceback (most recent call last):
  121. ...
  122. ValueError: math domain error
  123. >>> cos(NAN)
  124. nan
  125. >>> tan(INF)
  126. Traceback (most recent call last):
  127. ...
  128. ValueError: math domain error
  129. >>> tan(NINF)
  130. Traceback (most recent call last):
  131. ...
  132. ValueError: math domain error
  133. >>> tan(NAN)
  134. nan
  135. Neither pi nor tan are exact, but you can assume that tan(pi/2) is a large value
  136. and tan(pi) is a very small value:
  137. >>> tan(PI/2) > 1E10
  138. True
  139. >>> -tan(-PI/2) > 1E10
  140. True
  141. >>> tan(PI) < 1E-15
  142. True
  143. >>> asin(NAN), acos(NAN), atan(NAN)
  144. (nan, nan, nan)
  145. >>> asin(INF), asin(NINF)
  146. Traceback (most recent call last):
  147. ...
  148. ValueError: math domain error
  149. >>> acos(INF), acos(NINF)
  150. Traceback (most recent call last):
  151. ...
  152. ValueError: math domain error
  153. >>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
  154. (True, True)
  155. Hyberbolic Functions
  156. ====================