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
0fff62f9
Commit
0fff62f9
authored
Jul 01, 2004
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move Decimal from the sandbox into production.
parent
75cc1cb7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
Doc/whatsnew/whatsnew24.tex
Doc/whatsnew/whatsnew24.tex
+62
-0
No files found.
Doc/whatsnew/whatsnew24.tex
View file @
0fff62f9
...
...
@@ -200,6 +200,68 @@ root:*:0:0:System Administrator:/var/root:/bin/tcsh
\end{seealso}
%======================================================================
\section
{
PEP 327: Decimal Data Type
}
A new module,
\module
{
decimal
}
, offers a
\class
{
Decimal
}
data type for
decimal floating point arithmetic. Compared to the built-in
\class
{
float
}
type implemented with binary floating point, the new class is especially
useful for financial applications and other uses which require exact
decimal representation, control over precision, control over rounding
to meet legal or regulatory requirements, tracking of significant
decimal places, or for applications where the user expects the results
to match hand calculations done the way they were taught in school.
For example, calculating a 5
% tax on a 70 cent phone charge gives
different results in decimal floating point and binary floating point
with the difference being significant when rounding to the nearest
cent:
\begin{verbatim}
>>> from decimal import *
>>> Decimal('0.70') * Decimal('1.05')
Decimal("0.7350")
>>> .70 * 1.05
0.73499999999999999
\end{verbatim}
Note that the
\class
{
Decimal
}
result keeps a trailing zero, automatically
inferring four place significance from two digit mulitiplicands. A key
goal is to reproduce the mathematics we do by hand and avoid the tricky
issues that arise when decimal numbers cannot be represented exactly in
binary floating point.
Exact representation enables the
\class
{
Decimal
}
class to perform
modulo calculations and equality tests that would fail in binary
floating point:
\begin{verbatim}
>>> Decimal('1.00')
% Decimal('.10')
Decimal("0.00")
>>> 1.00
% 0.10
0.09999999999999995
>>> sum([Decimal('0.1')]*10) == Decimal('1.0')
True
>>> sum([0.1]*10) == 1.0
False
\end{verbatim}
The
\module
{
decimal
}
module also allows arbitrarily large precisions to be
set for calculation:
\begin{verbatim}
>>> getcontext().prec = 24
>>> Decimal(1) / Decimal(7)
Decimal("0.142857142857142857142857")
\end{verbatim}
\begin{seealso}
\seepep
{
327
}{
Decimal Data Type
}{
Written by Facundo Batista and implemented
by Eric Price, Facundo Bastista, Raymond Hettinger, Aahz, and Tim Peters.
}
\end{seealso}
%======================================================================
\section
{
Other Language Changes
}
...
...
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