Add acceptance of long ints to test_memoryio.py
[python.git] / Lib / test / ieee754.txt
blob5a41c8fe5077a7460752c809662e098dfeada50b
1 ======================================
2 Python IEEE 754 floating point support
3 ======================================
5 >>> from sys import float_info as FI
6 >>> from math import *
7 >>> PI = pi
8 >>> E = e
10 You must never compare two floats with == because you are not going to get
11 what you expect. We treat two floats as equal if the difference between them
12 is small than epsilon.
13 >>> EPS = 1E-15
14 >>> def equal(x, y):
15 ...     """Almost equal helper for floats"""
16 ...     return abs(x - y) < EPS
19 NaNs and INFs
20 =============
22 In Python 2.6 and newer NaNs (not a number) and infinity can be constructed
23 from the strings 'inf' and 'nan'.
25 >>> INF = float('inf')
26 >>> NINF = float('-inf')
27 >>> NAN = float('nan')
29 >>> INF
30 inf
31 >>> NINF
32 -inf
33 >>> NAN
34 nan
36 The math module's ``isnan`` and ``isinf`` functions can be used to detect INF
37 and NAN:
38 >>> isinf(INF), isinf(NINF), isnan(NAN)
39 (True, True, True)
40 >>> INF == -NINF
41 True
43 Infinity
44 --------
46 Ambiguous operations like ``0 * inf`` or ``inf - inf`` result in NaN.
47 >>> INF * 0
48 nan
49 >>> INF - INF
50 nan
51 >>> INF / INF
52 nan
54 However unambigous operations with inf return inf:
55 >>> INF * INF
56 inf
57 >>> 1.5 * INF
58 inf
59 >>> 0.5 * INF
60 inf
61 >>> INF / 1000
62 inf
64 Not a Number
65 ------------
67 NaNs are never equal to another number, even itself
68 >>> NAN == NAN
69 False
70 >>> NAN < 0
71 False
72 >>> NAN >= 0
73 False
75 All operations involving a NaN return a NaN except for the power of *0* and *1*.
76 >>> 1 + NAN
77 nan
78 >>> 1 * NAN
79 nan
80 >>> 0 * NAN
81 nan
82 >>> 1 ** NAN
83 1.0
84 >>> 0 ** NAN
85 0.0
86 >>> (1.0 + FI.epsilon) * NAN
87 nan
89 Misc Functions
90 ==============
92 The power of 1 raised to x is always 1.0, even for special values like 0,
93 infinity and NaN.
95 >>> pow(1, 0)
96 1.0
97 >>> pow(1, INF)
98 1.0
99 >>> pow(1, -INF)
101 >>> pow(1, NAN)
104 The power of 0 raised to x is defined as 0, if x is positive. Negative
105 values are a domain error or zero division error and NaN result in a
106 silent NaN.
108 >>> pow(0, 0)
110 >>> pow(0, INF)
112 >>> pow(0, -INF)
113 Traceback (most recent call last):
115 ValueError: math domain error
116 >>> 0 ** -1
117 Traceback (most recent call last):
119 ZeroDivisionError: 0.0 cannot be raised to a negative power
120 >>> pow(0, NAN)
124 Trigonometric Functions
125 =======================
127 >>> sin(INF)
128 Traceback (most recent call last):
130 ValueError: math domain error
131 >>> sin(NINF)
132 Traceback (most recent call last):
134 ValueError: math domain error
135 >>> sin(NAN)
137 >>> cos(INF)
138 Traceback (most recent call last):
140 ValueError: math domain error
141 >>> cos(NINF)
142 Traceback (most recent call last):
144 ValueError: math domain error
145 >>> cos(NAN)
147 >>> tan(INF)
148 Traceback (most recent call last):
150 ValueError: math domain error
151 >>> tan(NINF)
152 Traceback (most recent call last):
154 ValueError: math domain error
155 >>> tan(NAN)
158 Neither pi nor tan are exact, but you can assume that tan(pi/2) is a large value
159 and tan(pi) is a very small value:
160 >>> tan(PI/2) > 1E10
161 True
162 >>> -tan(-PI/2) > 1E10
163 True
164 >>> tan(PI) < 1E-15
165 True
167 >>> asin(NAN), acos(NAN), atan(NAN)
168 (nan, nan, nan)
169 >>> asin(INF), asin(NINF)
170 Traceback (most recent call last):
172 ValueError: math domain error
173 >>> acos(INF), acos(NINF)
174 Traceback (most recent call last):
176 ValueError: math domain error
177 >>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
178 (True, True)
181 Hyberbolic Functions
182 ====================