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
d68bf028
Commit
d68bf028
authored
Feb 14, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Show how to remove exponents.
parent
27a90d98
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
0 deletions
+11
-0
Doc/library/decimal.rst
Doc/library/decimal.rst
+11
-0
No files found.
Doc/library/decimal.rst
View file @
d68bf028
...
@@ -1590,6 +1590,7 @@ to handle the :meth:`quantize` step::
...
@@ -1590,6 +1590,7 @@ to handle the :meth:`quantize` step::
... return (x * y).quantize(fp)
... return (x * y).quantize(fp)
>>> def div(x, y, fp=TWOPLACES):
>>> def div(x, y, fp=TWOPLACES):
... return (x / y).quantize(fp)
... return (x / y).quantize(fp)
>>> mul(a, b) # Automatically preserve fixed-point
>>> mul(a, b) # Automatically preserve fixed-point
Decimal('325.62')
Decimal('325.62')
>>> div(b, a)
>>> div(b, a)
...
@@ -1615,6 +1616,16 @@ of significant places in the coefficient. For example, expressing
...
@@ -1615,6 +1616,16 @@ of significant places in the coefficient. For example, expressing
:const:`5.0E+3` as :const:`5000` keeps the value constant but cannot show the
:const:`5.0E+3` as :const:`5000` keeps the value constant but cannot show the
original's two-place significance.
original's two-place significance.
If an application does not care about tracking significance, it is easy to
remove the exponent and trailing zeroes, losing signficance, but keeping the
value unchanged::
>>> def remove_exponent(d):
... return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()
>>> remove_exponent(Decimal('5E+3'))
Decimal('5000')
Q. Is there a way to convert a regular float to a :class:`Decimal`?
Q. Is there a way to convert a regular float to a :class:`Decimal`?
A. Yes, all binary floating point numbers can be exactly expressed as a
A. Yes, all binary floating point numbers can be exactly expressed as a
...
...
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