~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/test/ieee754.txt

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 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
 
88
 
 
89
Misc Functions
 
90
==============
 
91
 
 
92
The power of 1 raised to x is always 1.0, even for special values like 0,
 
93
infinity and NaN.
 
94
 
 
95
>>> pow(1, 0)
 
96
1.0
 
97
>>> pow(1, INF)
 
98
1.0
 
99
>>> pow(1, -INF)
 
100
1.0
 
101
>>> pow(1, NAN)
 
102
1.0
 
103
 
 
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.
 
107
 
 
108
>>> pow(0, 0)
 
109
1.0
 
110
>>> pow(0, INF)
 
111
0.0
 
112
>>> pow(0, -INF)
 
113
Traceback (most recent call last):
 
114
...
 
115
ValueError: math domain error
 
116
>>> 0 ** -1
 
117
Traceback (most recent call last):
 
118
...
 
119
ZeroDivisionError: 0.0 cannot be raised to a negative power
 
120
>>> pow(0, NAN)
 
121
nan
 
122
 
 
123
 
 
124
Trigonometric Functions
 
125
=======================
 
126
 
 
127
>>> sin(INF)
 
128
Traceback (most recent call last):
 
129
...
 
130
ValueError: math domain error
 
131
>>> sin(NINF)
 
132
Traceback (most recent call last):
 
133
...
 
134
ValueError: math domain error
 
135
>>> sin(NAN)
 
136
nan
 
137
>>> cos(INF)
 
138
Traceback (most recent call last):
 
139
...
 
140
ValueError: math domain error
 
141
>>> cos(NINF)
 
142
Traceback (most recent call last):
 
143
...
 
144
ValueError: math domain error
 
145
>>> cos(NAN)
 
146
nan
 
147
>>> tan(INF)
 
148
Traceback (most recent call last):
 
149
...
 
150
ValueError: math domain error
 
151
>>> tan(NINF)
 
152
Traceback (most recent call last):
 
153
...
 
154
ValueError: math domain error
 
155
>>> tan(NAN)
 
156
nan
 
157
 
 
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
 
166
 
 
167
>>> asin(NAN), acos(NAN), atan(NAN)
 
168
(nan, nan, nan)
 
169
>>> asin(INF), asin(NINF)
 
170
Traceback (most recent call last):
 
171
...
 
172
ValueError: math domain error
 
173
>>> acos(INF), acos(NINF)
 
174
Traceback (most recent call last):
 
175
...
 
176
ValueError: math domain error
 
177
>>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
 
178
(True, True)
 
179
 
 
180
 
 
181
Hyberbolic Functions
 
182
====================
 
183