1 ======================================
2 Python IEEE 754 floating point support
3 ======================================
5 >>> from sys import float_info as FI
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.
15 ... """Almost equal helper for floats"""
16 ... return abs(x - y) < EPS
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')
36 The math module's ``isnan`` and ``isinf`` functions can be used to detect INF
38 >>> isinf(INF), isinf(NINF), isnan(NAN)
46 Ambiguous operations like ``0 * inf`` or ``inf - inf`` result in NaN.
54 However unambigous operations with inf return inf:
67 NaNs are never equal to another number, even itself
75 All operations involving a NaN return a NaN except for nan**0 and 1**nan.
88 >>> (1.0 + FI.epsilon) * NAN
94 The power of 1 raised to x is always 1.0, even for special values like 0,
106 The power of 0 raised to x is defined as 0, if x is positive. Negative
107 values are a domain error or zero division error and NaN result in a
115 Traceback (most recent call last):
117 ValueError: math domain error
119 Traceback (most recent call last):
121 ZeroDivisionError: 0.0 cannot be raised to a negative power
126 Trigonometric Functions
127 =======================
130 Traceback (most recent call last):
132 ValueError: math domain error
134 Traceback (most recent call last):
136 ValueError: math domain error
140 Traceback (most recent call last):
142 ValueError: math domain error
144 Traceback (most recent call last):
146 ValueError: math domain error
150 Traceback (most recent call last):
152 ValueError: math domain error
154 Traceback (most recent call last):
156 ValueError: math domain error
160 Neither pi nor tan are exact, but you can assume that tan(pi/2) is a large value
161 and tan(pi) is a very small value:
164 >>> -tan(-PI/2) > 1E10
169 >>> asin(NAN), acos(NAN), atan(NAN)
171 >>> asin(INF), asin(NINF)
172 Traceback (most recent call last):
174 ValueError: math domain error
175 >>> acos(INF), acos(NINF)
176 Traceback (most recent call last):
178 ValueError: math domain error
179 >>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)