Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
2f55eb4c
Commit
2f55eb4c
authored
Jul 06, 2004
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Demonstrate how to round final result.
parent
0aeac107
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
17 deletions
+17
-17
Doc/lib/libdecimal.tex
Doc/lib/libdecimal.tex
+17
-17
No files found.
Doc/lib/libdecimal.tex
View file @
2f55eb4c
...
...
@@ -932,9 +932,9 @@ def pi():
"""Compute Pi to the current precision.
>>> print pi()
3.141592653589793238462643383
279502887
3.141592653589793238462643383
"""
getcontext().prec +=
9
# extra digits for intermediate steps
getcontext().prec +=
2
# extra digits for intermediate steps
three = Decimal(3) # substitute "three=3.0" for regular floats
lastc, t, c, n, na, d, da = 0, three, 3, 1, 0, 0, 24
while c != lastc:
...
...
@@ -943,22 +943,22 @@ def pi():
d, da = d+da, da+32
t = (t * n) / d
c += t
getcontext().prec -=
9
return c
getcontext().prec -=
2
return c
+ 0
def exp(x):
"""Return e raised to the power of x. Result type matches input type.
>>> print exp(Decimal(1))
2.718281828459045235360287471
352662498
2.718281828459045235360287471
>>> print exp(Decimal(2))
7.38905609893065022723042746
0575007813
7.38905609893065022723042746
1
>>> print exp(2.0)
7.38905609893
>>> print exp(2+0j)
(7.38905609893+0j)
"""
getcontext().prec +=
9
# extra digits for intermediate steps
getcontext().prec +=
2
# extra digits for intermediate steps
i, laste, e, fact, num = 0, 0, 1, 1, 1
while e != laste:
laste = e
...
...
@@ -966,20 +966,20 @@ def exp(x):
fact *= i
num *= x
e += num / fact
getcontext().prec -=
9
return e
getcontext().prec -=
2
return e
+ 0
def cos(x):
"""Return the cosine of x as measured in radians.
>>> print cos(Decimal('0.5'))
0.8775825618903727161162815826
038296521
0.8775825618903727161162815826
>>> print cos(0.5)
0.87758256189
>>> print cos(0.5+0j)
(0.87758256189+0j)
"""
getcontext().prec +=
9
# extra digits for intermediate steps
getcontext().prec +=
2
# extra digits for intermediate steps
i, laste, e, fact, num, sign = 0, 0, 1, 1, 1, 1
while e != laste:
laste = e
...
...
@@ -988,20 +988,20 @@ def cos(x):
num *= x * x
sign *= -1
e += num / fact * sign
getcontext().prec -=
9
return e
getcontext().prec -=
2
return e
+ 0
def sin(x):
"""Return the cosine of x as measured in radians.
>>> print sin(Decimal('0.5'))
0.4794255386042030002732879352
155713880
0.4794255386042030002732879352
>>> print sin(0.5)
0.479425538604
>>> print sin(0.5+0j)
(0.479425538604+0j)
"""
getcontext().prec +=
9
# extra digits for intermediate steps
getcontext().prec +=
2
# extra digits for intermediate steps
i, laste, e, fact, num, sign = 1, 0, x, 1, x, 1
while e != laste:
laste = e
...
...
@@ -1010,7 +1010,7 @@ def sin(x):
num *= x * x
sign *= -1
e += num / fact * sign
getcontext().prec -=
9
return e
getcontext().prec -=
2
return e
+ 0
\end{verbatim}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment