| 1 | ======================================
|
|---|
| 2 | Python IEEE 754 floating point support
|
|---|
| 3 | ======================================
|
|---|
| 4 |
|
|---|
| 5 | >>> from sys import float_info as FI
|
|---|
| 6 | >>> from math import *
|
|---|
| 7 | >>> PI = pi
|
|---|
| 8 | >>> E = e
|
|---|
| 9 |
|
|---|
| 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
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 | NaNs and INFs
|
|---|
| 20 | =============
|
|---|
| 21 |
|
|---|
| 22 | In Python 2.6 and newer NaNs (not a number) and infinity can be constructed
|
|---|
| 23 | from the strings 'inf' and 'nan'.
|
|---|
| 24 |
|
|---|
| 25 | >>> INF = float('inf')
|
|---|
| 26 | >>> NINF = float('-inf')
|
|---|
| 27 | >>> NAN = float('nan')
|
|---|
| 28 |
|
|---|
| 29 | >>> INF
|
|---|
| 30 | inf
|
|---|
| 31 | >>> NINF
|
|---|
| 32 | -inf
|
|---|
| 33 | >>> NAN
|
|---|
| 34 | nan
|
|---|
| 35 |
|
|---|
| 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
|
|---|
| 42 |
|
|---|
| 43 | Infinity
|
|---|
| 44 | --------
|
|---|
| 45 |
|
|---|
| 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
|
|---|
| 53 |
|
|---|
| 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
|
|---|
| 63 |
|
|---|
| 64 | Not a Number
|
|---|
| 65 | ------------
|
|---|
| 66 |
|
|---|
| 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
|
|---|
| 74 |
|
|---|
| 75 | All operations involving a NaN return a NaN except for nan**0 and 1**nan.
|
|---|
| 76 | >>> 1 + NAN
|
|---|
| 77 | nan
|
|---|
| 78 | >>> 1 * NAN
|
|---|
| 79 | nan
|
|---|
| 80 | >>> 0 * NAN
|
|---|
| 81 | nan
|
|---|
| 82 | >>> 1 ** NAN
|
|---|
| 83 | 1.0
|
|---|
| 84 | >>> NAN ** 0
|
|---|
| 85 | 1.0
|
|---|
| 86 | >>> 0 ** NAN
|
|---|
| 87 | nan
|
|---|
| 88 | >>> (1.0 + FI.epsilon) * NAN
|
|---|
| 89 | nan
|
|---|
| 90 |
|
|---|
| 91 | Misc Functions
|
|---|
| 92 | ==============
|
|---|
| 93 |
|
|---|
| 94 | The power of 1 raised to x is always 1.0, even for special values like 0,
|
|---|
| 95 | infinity and NaN.
|
|---|
| 96 |
|
|---|
| 97 | >>> pow(1, 0)
|
|---|
| 98 | 1.0
|
|---|
| 99 | >>> pow(1, INF)
|
|---|
| 100 | 1.0
|
|---|
| 101 | >>> pow(1, -INF)
|
|---|
| 102 | 1.0
|
|---|
| 103 | >>> pow(1, NAN)
|
|---|
| 104 | 1.0
|
|---|
| 105 |
|
|---|
| 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
|
|---|
| 108 | silent NaN.
|
|---|
| 109 |
|
|---|
| 110 | >>> pow(0, 0)
|
|---|
| 111 | 1.0
|
|---|
| 112 | >>> pow(0, INF)
|
|---|
| 113 | 0.0
|
|---|
| 114 | >>> pow(0, -INF)
|
|---|
| 115 | Traceback (most recent call last):
|
|---|
| 116 | ...
|
|---|
| 117 | ValueError: math domain error
|
|---|
| 118 | >>> 0 ** -1
|
|---|
| 119 | Traceback (most recent call last):
|
|---|
| 120 | ...
|
|---|
| 121 | ZeroDivisionError: 0.0 cannot be raised to a negative power
|
|---|
| 122 | >>> pow(0, NAN)
|
|---|
| 123 | nan
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | Trigonometric Functions
|
|---|
| 127 | =======================
|
|---|
| 128 |
|
|---|
| 129 | >>> sin(INF)
|
|---|
| 130 | Traceback (most recent call last):
|
|---|
| 131 | ...
|
|---|
| 132 | ValueError: math domain error
|
|---|
| 133 | >>> sin(NINF)
|
|---|
| 134 | Traceback (most recent call last):
|
|---|
| 135 | ...
|
|---|
| 136 | ValueError: math domain error
|
|---|
| 137 | >>> sin(NAN)
|
|---|
| 138 | nan
|
|---|
| 139 | >>> cos(INF)
|
|---|
| 140 | Traceback (most recent call last):
|
|---|
| 141 | ...
|
|---|
| 142 | ValueError: math domain error
|
|---|
| 143 | >>> cos(NINF)
|
|---|
| 144 | Traceback (most recent call last):
|
|---|
| 145 | ...
|
|---|
| 146 | ValueError: math domain error
|
|---|
| 147 | >>> cos(NAN)
|
|---|
| 148 | nan
|
|---|
| 149 | >>> tan(INF)
|
|---|
| 150 | Traceback (most recent call last):
|
|---|
| 151 | ...
|
|---|
| 152 | ValueError: math domain error
|
|---|
| 153 | >>> tan(NINF)
|
|---|
| 154 | Traceback (most recent call last):
|
|---|
| 155 | ...
|
|---|
| 156 | ValueError: math domain error
|
|---|
| 157 | >>> tan(NAN)
|
|---|
| 158 | nan
|
|---|
| 159 |
|
|---|
| 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:
|
|---|
| 162 | >>> tan(PI/2) > 1E10
|
|---|
| 163 | True
|
|---|
| 164 | >>> -tan(-PI/2) > 1E10
|
|---|
| 165 | True
|
|---|
| 166 | >>> tan(PI) < 1E-15
|
|---|
| 167 | True
|
|---|
| 168 |
|
|---|
| 169 | >>> asin(NAN), acos(NAN), atan(NAN)
|
|---|
| 170 | (nan, nan, nan)
|
|---|
| 171 | >>> asin(INF), asin(NINF)
|
|---|
| 172 | Traceback (most recent call last):
|
|---|
| 173 | ...
|
|---|
| 174 | ValueError: math domain error
|
|---|
| 175 | >>> acos(INF), acos(NINF)
|
|---|
| 176 | Traceback (most recent call last):
|
|---|
| 177 | ...
|
|---|
| 178 | ValueError: math domain error
|
|---|
| 179 | >>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
|
|---|
| 180 | (True, True)
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 | Hyberbolic Functions
|
|---|
| 184 | ====================
|
|---|
| 185 |
|
|---|