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

Skip to content

Commit 71260b8

Browse files
committed
Added math.rint() -- round according to current IEEE754 mode
1 parent dab6cb8 commit 71260b8

5 files changed

Lines changed: 16 additions & 0 deletions

File tree

Doc/lib/libmath.tex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ \section{\module{math} ---
9393
Return \code{\var{x}**\var{y}}.
9494
\end{funcdesc}
9595

96+
\begin{funcdesc}{rint}{x, y}
97+
Return the integer nearest to \var{x} as a real.
98+
\end{funcdesc}
99+
96100
\begin{funcdesc}{sin}{x}
97101
Return the sine of \var{x}.
98102
\end{funcdesc}

Include/mymath.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern double hypot Py_PROTO((double, double));
4848
#undef log
4949
#undef log10
5050
#undef pow
51+
#undef rint
5152
#undef sin
5253
#undef sinh
5354
#undef sqrt
@@ -67,6 +68,7 @@ extern double hypot Py_PROTO((double, double));
6768
#define log logd
6869
#define log10 log10d
6970
#define pow powd
71+
#define rint rintd
7072
#define sin sind
7173
#define sinh sinhd
7274
#define sqrt sqrtd

Lib/test/output/test_math

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ log
1919
log10
2020
modf
2121
pow
22+
rint
2223
sin
2324
sinh
2425
sqrt

Lib/test/test_math.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ def testmodf(name, (v1, v2), (e1, e2)):
129129
testit('pow(2,1)', math.pow(2,1), 2)
130130
testit('pow(2,-1)', math.pow(2,-1), 0.5)
131131

132+
print 'rint'
133+
testit('rint(0.7)', math.rint(0.7), 1)
134+
testit('rint(-0.3)', math.rint(-0.3), 0)
135+
testit('rint(2.5)', math.rint(2.5), 2)
136+
testit('rint(3.5)', math.rint(3.5), 4)
137+
132138
print 'sin'
133139
testit('sin(0)', math.sin(0), 0)
134140
testit('sin(pi/2)', math.sin(math.pi/2), 1)

Modules/mathmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ FUNC2(math_pow, power, math_pow_doc,
156156
FUNC2(math_pow, pow, math_pow_doc,
157157
"pow(x,y)\n\nReturn x**y.")
158158
#endif
159+
FUNC1(math_rint, rint, math_rint_doc,
160+
"rint(x)\n\nReturn the integer nearest to x as a real.")
159161
FUNC1(math_sin, sin, math_sin_doc,
160162
"sin(x)\n\nReturn the sine of x.")
161163
FUNC1(math_sinh, sinh, math_sinh_doc,
@@ -267,6 +269,7 @@ static PyMethodDef math_methods[] = {
267269
{"log10", math_log10, 0, math_log10_doc},
268270
{"modf", math_modf, 0, math_modf_doc},
269271
{"pow", math_pow, 0, math_pow_doc},
272+
{"rint", math_rint, 0, math_rint_doc},
270273
{"sin", math_sin, 0, math_sin_doc},
271274
{"sinh", math_sinh, 0, math_sinh_doc},
272275
{"sqrt", math_sqrt, 0, math_sqrt_doc},

0 commit comments

Comments
 (0)