Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66bb6e6

Browse files
committed
SF bug 996392: math and cmath docs don't specify radians
Major rewrite of the math module docs. Slapped in "radians" where appropriate; grouped the functions into reasonable categories; supplied many more words to address common confusions about some of the subtler issues.
1 parent 5253da1 commit 66bb6e6

2 files changed

Lines changed: 101 additions & 65 deletions

File tree

Doc/lib/libmath.tex

Lines changed: 99 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,124 +18,159 @@ \section{\module{math} ---
1818
and why it was generated in the first place.
1919

2020
The following functions are provided by this module. Except
21-
when explicitly noted otherwise, all return values are floats:
21+
when explicitly noted otherwise, all return values are floats.
2222

23-
\begin{funcdesc}{acos}{x}
24-
Return the arc cosine of \var{x}.
25-
\end{funcdesc}
26-
27-
\begin{funcdesc}{asin}{x}
28-
Return the arc sine of \var{x}.
29-
\end{funcdesc}
30-
31-
\begin{funcdesc}{atan}{x}
32-
Return the arc tangent of \var{x}.
33-
\end{funcdesc}
34-
35-
\begin{funcdesc}{atan2}{y, x}
36-
Return \code{atan(\var{y} / \var{x})}.
37-
\end{funcdesc}
23+
Number-theoretic and representation functions:
3824

3925
\begin{funcdesc}{ceil}{x}
40-
Return the ceiling of \var{x} as a float.
41-
\end{funcdesc}
42-
43-
\begin{funcdesc}{cos}{x}
44-
Return the cosine of \var{x}.
45-
\end{funcdesc}
46-
47-
\begin{funcdesc}{cosh}{x}
48-
Return the hyperbolic cosine of \var{x}.
49-
\end{funcdesc}
50-
51-
\begin{funcdesc}{degrees}{x}
52-
Converts angle \var{x} from radians to degrees.
53-
\end{funcdesc}
54-
55-
\begin{funcdesc}{exp}{x}
56-
Return \code{e**\var{x}}.
26+
Return the ceiling of \var{x} as a float, the smallest integer value
27+
greater than or equal to \var{x}.
5728
\end{funcdesc}
5829

5930
\begin{funcdesc}{fabs}{x}
6031
Return the absolute value of \var{x}.
6132
\end{funcdesc}
6233

6334
\begin{funcdesc}{floor}{x}
64-
Return the floor of \var{x} as a float.
35+
Return the floor of \var{x} as a float, the largest integer value
36+
less than or equal to \var{x}.
6537
\end{funcdesc}
6638

6739
\begin{funcdesc}{fmod}{x, y}
6840
Return \code{fmod(\var{x}, \var{y})}, as defined by the platform C library.
6941
Note that the Python expression \code{\var{x} \%\ \var{y}} may not return
70-
the same result.
42+
the same result. The intent of the C standard is that
43+
\code{fmod(\var{x}, \var{y})} be exactly (mathematically; to infinite
44+
precision) equal to \code{\var{x} - \var{n}*\var{y}} for some integer
45+
\var{n} such that the result has the same sign as \var{x} and
46+
magnitude less than \code{abs(\var{y})}. Python's
47+
\code{\var{x} \%\ \var{y}} returns a result with the sign of
48+
\var{y} instead, and may not be exactly computable for float arguments.
49+
For example, \code{fmod(-1e-100, 1e100)} is \code{-1e-100}, but the
50+
result of Python's \code{-1e-100 \%\ 1e100} is \code{1e100-1e-100}, which
51+
cannot be represented exactly as a float, and rounds to the surprising
52+
\code{1e100}. For this reason, function \function{fmod()} is generally
53+
preferred when working with floats, while Python's
54+
\code{\var{x} \%\ \var{y}} is preferred when working with integers.
7155
\end{funcdesc}
7256

7357
\begin{funcdesc}{frexp}{x}
74-
% Blessed by Tim.
7558
Return the mantissa and exponent of \var{x} as the pair
7659
\code{(\var{m}, \var{e})}. \var{m} is a float and \var{e} is an
77-
integer such that \code{\var{x} == \var{m} * 2**\var{e}}.
60+
integer such that \code{\var{x} == \var{m} * 2**\var{e}} exactly.
7861
If \var{x} is zero, returns \code{(0.0, 0)}, otherwise
79-
\code{0.5 <= abs(\var{m}) < 1}.
62+
\code{0.5 <= abs(\var{m}) < 1}. This is used to "pick apart" the
63+
internal representation of a float in a portable way.
8064
\end{funcdesc}
8165

82-
\begin{funcdesc}{hypot}{x, y}
83-
Return the Euclidean distance, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}.
66+
\begin{funcdesc}{ldexp}{x, i}
67+
Return \code{\var{x} * (2**\var{i})}. This is essentially the inverse of
68+
function \function{frexp()}.
8469
\end{funcdesc}
8570

86-
\begin{funcdesc}{ldexp}{x, i}
87-
Return \code{\var{x} * (2**\var{i})}.
71+
\begin{funcdesc}{modf}{x}
72+
Return the fractional and integer parts of \var{x}. Both results
73+
carry the sign of \var{x}, and both are floats.
74+
\end{funcdesc}
75+
76+
Note that \function{frexp()} and \function{modf()} have a different
77+
call/return pattern than their C equivalents: they take a single
78+
argument and return a pair of values, rather than returning their
79+
second return value through an `output parameter' (there is no such
80+
thing in Python).
81+
82+
Power and logarithmic functions:
83+
84+
\begin{funcdesc}{exp}{x}
85+
Return \code{e**\var{x}}.
8886
\end{funcdesc}
8987

9088
\begin{funcdesc}{log}{x\optional{, base}}
91-
Returns the logarithm of \var{x} to the given \var{base}.
92-
If the \var{base} is not specified, returns the natural logarithm of \var{x}.
89+
Return the logarithm of \var{x} to the given \var{base}.
90+
If the \var{base} is not specified, return the natural logarithm of \var{x}
91+
(that is, the logarithm to base \emph{e}).
9392
\versionchanged[\var{base} argument added]{2.3}
9493
\end{funcdesc}
9594

9695
\begin{funcdesc}{log10}{x}
9796
Return the base-10 logarithm of \var{x}.
9897
\end{funcdesc}
9998

100-
\begin{funcdesc}{modf}{x}
101-
Return the fractional and integer parts of \var{x}. Both results
102-
carry the sign of \var{x}. The integer part is returned as a float.
103-
\end{funcdesc}
104-
10599
\begin{funcdesc}{pow}{x, y}
106100
Return \code{\var{x}**\var{y}}.
107101
\end{funcdesc}
108102

109-
\begin{funcdesc}{radians}{x}
110-
Converts angle \var{x} from degrees to radians.
103+
\begin{funcdesc}{sqrt}{x}
104+
Return the square root of \var{x}.
111105
\end{funcdesc}
112106

113-
\begin{funcdesc}{sin}{x}
114-
Return the sine of \var{x}.
107+
Trigonometric functions:
108+
109+
\begin{funcdesc}{acos}{x}
110+
Return the arc cosine of \var{x}, in radians.
115111
\end{funcdesc}
116112

117-
\begin{funcdesc}{sinh}{x}
118-
Return the hyperbolic sine of \var{x}.
113+
\begin{funcdesc}{asin}{x}
114+
Return the arc sine of \var{x}, in radians.
119115
\end{funcdesc}
120116

121-
\begin{funcdesc}{sqrt}{x}
122-
Return the square root of \var{x}.
117+
\begin{funcdesc}{atan}{x}
118+
Return the arc tangent of \var{x}, in radians.
119+
\end{funcdesc}
120+
121+
\begin{funcdesc}{atan2}{y, x}
122+
Return \code{atan(\var{y} / \var{x})}, in radians.
123+
The result is between \code{-pi} and \code{pi}.
124+
The vector in the plane from the origin to point \code{(\var{x}, \var{y})}
125+
makes this angle with the positive X axis.
126+
The point of \function{atan2()} is that the signs of both inputs are
127+
known to it, so it can compute the correct quadrant for the angle.
128+
For example, \code{atan(1}) and \code{atan2(1, 1)} are both \code{pi/4},
129+
but \code{atan2(-1, -1)} is \code{-3*pi/4}.
130+
\end{funcdesc}
131+
132+
\begin{funcdesc}{cos}{x}
133+
Return the cosine of \var{x} radians.
134+
\end{funcdesc}
135+
136+
\begin{funcdesc}{hypot}{x, y}
137+
Return the Euclidean norm, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}.
138+
This is the length of the vector from the origin to point
139+
\code{(\var{x}, \var{y})}.
140+
\end{funcdesc}
141+
142+
\begin{funcdesc}{sin}{x}
143+
Return the sine of \var{x} radians.
123144
\end{funcdesc}
124145

125146
\begin{funcdesc}{tan}{x}
126-
Return the tangent of \var{x}.
147+
Return the tangent of \var{x} radians.
148+
\end{funcdesc}
149+
150+
Angular conversion:
151+
152+
\begin{funcdesc}{degrees}{x}
153+
Converts angle \var{x} from radians to degrees.
154+
\end{funcdesc}
155+
156+
\begin{funcdesc}{radians}{x}
157+
Converts angle \var{x} from degrees to radians.
158+
\end{funcdesc}
159+
160+
Hyerbolic functions:
161+
162+
\begin{funcdesc}{cosh}{x}
163+
Return the hyperbolic cosine of \var{x}.
164+
\end{funcdesc}
165+
166+
\begin{funcdesc}{sinh}{x}
167+
Return the hyperbolic sine of \var{x}.
127168
\end{funcdesc}
128169

129170
\begin{funcdesc}{tanh}{x}
130171
Return the hyperbolic tangent of \var{x}.
131172
\end{funcdesc}
132173

133-
Note that \function{frexp()} and \function{modf()} have a different
134-
call/return pattern than their C equivalents: they take a single
135-
argument and return a pair of values, rather than returning their
136-
second return value through an `output parameter' (there is no such
137-
thing in Python).
138-
139174
The module also defines two mathematical constants:
140175

141176
\begin{datadesc}{pi}

Misc/NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ C API
139139
Documentation
140140
-------------
141141

142-
Improved the tutorial on creating types in C.
142+
- Improved the tutorial on creating types in C.
143143

144144
- point out the importance of reassigning data members before
145145
assigning thier values
@@ -148,6 +148,7 @@ Improved the tutorial on creating types in C.
148148

149149
- mention the labor saving Py_VISIT and Py_CLEAR macros.
150150

151+
- Major rewrite of the math module docs, to address common confusions.
151152

152153
New platforms
153154
-------------

0 commit comments

Comments
 (0)