Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / decimal-faq.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="decimal-recipes.html" />
<link rel="parent" href="module-decimal.html" />
<link rel="next" href="module-math.html" />
<meta name='aesop' content='information' />
<title>5.6.8 Decimal FAQ </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.6.7 Recipes"
href="decimal-recipes.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5.6 decimal "
href="module-decimal.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.7 math "
href="module-math.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="decimal-recipes.html">5.6.7 Recipes</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-decimal.html">5.6 decimal </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-math.html">5.7 math </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION007680000000000000000"></A><A NAME="decimal-faq"></A>
<BR>
5.6.8 Decimal FAQ
</H2>
<P>
Q. It is cumbersome to type <code>decimal.Decimal('1234.5')</code>. Is there a way
to minimize typing when using the interactive interpreter?
<P>
A. Some users abbreviate the constructor to just a single letter:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; D = decimal.Decimal
&gt;&gt;&gt; D('1.23') + D('3.45')
Decimal("4.68")
</pre></div>
<P>
Q. In a fixed-point application with two decimal places, some inputs
have many places and need to be rounded. Others are not supposed to have
excess digits and need to be validated. What methods should be used?
<P>
A. The <tt class="method">quantize()</tt> method rounds to a fixed number of decimal places.
If the <tt class="constant">Inexact</tt> trap is set, it is also useful for validation:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01')
&gt;&gt;&gt; # Round to two places
&gt;&gt;&gt; Decimal("3.214").quantize(TWOPLACES)
Decimal("3.21")
&gt;&gt;&gt; # Validate that a number does not exceed two places
&gt;&gt;&gt; Decimal("3.21").quantize(TWOPLACES, context=Context(traps=[Inexact]))
Decimal("3.21")
&gt;&gt;&gt; Decimal("3.214").quantize(TWOPLACES, context=Context(traps=[Inexact]))
Traceback (most recent call last):
...
Inexact: Changed in rounding
</pre></div>
<P>
Q. Once I have valid two place inputs, how do I maintain that invariant
throughout an application?
<P>
A. Some operations like addition and subtraction automatically preserve fixed
point. Others, like multiplication and division, change the number of decimal
places and need to be followed-up with a <tt class="method">quantize()</tt> step.
<P>
Q. There are many ways to express the same value. The numbers
<tt class="constant">200</tt>, <tt class="constant">200.000</tt>, <tt class="constant">2E2</tt>, and <tt class="constant">.02E+4</tt> all
have the same value at various precisions. Is there a way to transform them to
a single recognizable canonical value?
<P>
A. The <tt class="method">normalize()</tt> method maps all equivalent values to a single
representive:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; values = map(Decimal, '200 200.000 2E2 .02E+4'.split())
&gt;&gt;&gt; [v.normalize() for v in values]
[Decimal("2E+2"), Decimal("2E+2"), Decimal("2E+2"), Decimal("2E+2")]
</pre></div>
<P>
Q. Some decimal values always print with exponential notation. Is there
a way to get a non-exponential representation?
<P>
A. For some values, exponential notation is the only way to express
the number of significant places in the coefficient. For example,
expressing <tt class="constant">5.0E+3</tt> as <tt class="constant">5000</tt> keeps the value
constant but cannot show the original's two-place significance.
<P>
Q. Is there a way to convert a regular float to a <tt class="class">Decimal</tt>?
<P>
A. Yes, all binary floating point numbers can be exactly expressed as a
Decimal. An exact conversion may take more precision than intuition would
suggest, so trapping <tt class="constant">Inexact</tt> will signal a need for more precision:
<P>
<div class="verbatim"><pre>
def floatToDecimal(f):
"Convert a floating point number to a Decimal with no loss of information"
# Transform (exactly) a float to a mantissa (0.5 &lt;= abs(m) &lt; 1.0) and an
# exponent. Double the mantissa until it is an integer. Use the integer
# mantissa and exponent to compute an equivalent Decimal. If this cannot
# be done exactly, then retry with more precision.
mantissa, exponent = math.frexp(f)
while mantissa != int(mantissa):
mantissa *= 2.0
exponent -= 1
mantissa = int(mantissa)
oldcontext = getcontext()
setcontext(Context(traps=[Inexact]))
try:
while True:
try:
return mantissa * Decimal(2) ** exponent
except Inexact:
getcontext().prec += 1
finally:
setcontext(oldcontext)
</pre></div>
<P>
Q. Why isn't the <tt class="function">floatToDecimal()</tt> routine included in the module?
<P>
A. There is some question about whether it is advisable to mix binary and
decimal floating point. Also, its use requires some care to avoid the
representation issues associated with binary floating point:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; floatToDecimal(1.1)
Decimal("1.100000000000000088817841970012523233890533447265625")
</pre></div>
<P>
Q. Within a complex calculation, how can I make sure that I haven't gotten a
spurious result because of insufficient precision or rounding anomalies.
<P>
A. The decimal module makes it easy to test results. A best practice is to
re-run calculations using greater precision and with various rounding modes.
Widely differing results indicate insufficient precision, rounding mode
issues, ill-conditioned inputs, or a numerically unstable algorithm.
<P>
Q. I noticed that context precision is applied to the results of operations
but not to the inputs. Is there anything to watch out for when mixing
values of different precisions?
<P>
A. Yes. The principle is that all values are considered to be exact and so
is the arithmetic on those values. Only the results are rounded. The
advantage for inputs is that ``what you type is what you get''. A
disadvantage is that the results can look odd if you forget that the inputs
haven't been rounded:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; getcontext().prec = 3
&gt;&gt;&gt; Decimal('3.104') + D('2.104')
Decimal("5.21")
&gt;&gt;&gt; Decimal('3.104') + D('0.000') + D('2.104')
Decimal("5.20")
</pre></div>
<P>
The solution is either to increase precision or to force rounding of inputs
using the unary plus operation:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; getcontext().prec = 3
&gt;&gt;&gt; +Decimal('1.23456789') # unary plus triggers rounding
Decimal("1.23")
</pre></div>
<P>
Alternatively, inputs can be rounded upon creation using the
<tt class="method">Context.create_decimal()</tt> method:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; Context(prec=5, rounding=ROUND_DOWN).create_decimal('1.2345678')
Decimal("1.2345")
</pre></div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.6.7 Recipes"
href="decimal-recipes.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5.6 decimal "
href="module-decimal.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.7 math "
href="module-math.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="decimal-recipes.html">5.6.7 Recipes</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-decimal.html">5.6 decimal </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-math.html">5.7 math </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>