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

Skip to content

Commit 0ad20f1

Browse files
committed
Update Decimal section to match the current module
1 parent 65a3332 commit 0ad20f1

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

Doc/whatsnew/whatsnew24.tex

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ \subsection{Why is Decimal needed?}
268268
\end{verbatim}
269269

270270
The inaccuracy isn't always visible when you print the number because
271-
the FP-to-decimal-string conversion is provided by the C library, and
271+
the FP-to-decimal-string conversion is provided by the C library and
272272
most C libraries try to produce sensible output, but the inaccuracy is
273273
still there and subsequent operations can magnify the error.
274274

@@ -319,8 +319,8 @@ \subsection{The \class{Decimal} type}
319319
>>> f = 1.1
320320
>>> decimal.Decimal(str(f))
321321
Decimal("1.1")
322-
>>> decimal.Decimal(repr(f))
323-
Decimal("1.1000000000000001")
322+
>>> decimal.Decimal('%.12f' % f)
323+
Decimal("1.100000000000")
324324
\end{verbatim}
325325

326326
Once you have \class{Decimal} instances, you can perform the usual
@@ -337,11 +337,13 @@ \subsection{The \class{Decimal} type}
337337
>>> a*b
338338
Decimal("61.7956")
339339
>>> a/b
340-
Decimal("20.6473988")
340+
Decimal("20.64739884393063583815028902")
341341
>>> a ** 2
342342
Decimal("1275.9184")
343-
>>> a ** b
344-
Decimal("NaN")
343+
>>> a**b
344+
Traceback (most recent call last):
345+
...
346+
decimal.InvalidOperation: x ** (non-integer)
345347
\end{verbatim}
346348

347349
You can combine \class{Decimal} instances with integers, but not with
@@ -358,8 +360,10 @@ \subsection{The \class{Decimal} type}
358360
\end{verbatim}
359361

360362
\class{Decimal} numbers can be used with the \module{math} and
361-
\module{cmath} modules, though you'll get back a regular
362-
floating-point number and not a \class{Decimal}. Instances also have a \method{sqrt()} method:
363+
\module{cmath} modules, but note that they'll be immediately converted to
364+
floating-point numbers before the operation is performed, resulting in
365+
a possible loss of precision and accuracy. You'll also get back a
366+
regular floating-point number and not a \class{Decimal}.
363367

364368
\begin{verbatim}
365369
>>> import math, cmath
@@ -368,6 +372,13 @@ \subsection{The \class{Decimal} type}
368372
351364.18288201344
369373
>>> cmath.sqrt(-d)
370374
351364.18288201344j
375+
\end{verbatim}
376+
377+
Instances also have a \method{sqrt()} method that returns a
378+
\class{Decimal}, but if you need other things such as trigonometric
379+
functions you'll have to implement them.
380+
381+
\begin{verbatim}
371382
>>> d.sqrt()
372383
Decimal("351364.1828820134592177245001")
373384
\end{verbatim}
@@ -383,7 +394,7 @@ \subsection{The \class{Context} type}
383394
\item \member{rounding} specifies the rounding mode. The \module{decimal}
384395
module has constants for the various possibilities:
385396
\constant{ROUND_DOWN}, \constant{ROUND_CEILING}, \constant{ROUND_HALF_EVEN}, and various others.
386-
\item \member{trap_enablers} is a dictionary specifying what happens on
397+
\item \member{traps} is a dictionary specifying what happens on
387398
encountering certain error conditions: either an exception is raised or
388399
a value is returned. Some examples of error conditions are
389400
division by zero, loss of precision, and overflow.
@@ -403,25 +414,27 @@ \subsection{The \class{Context} type}
403414
Decimal("0.142857143")
404415
\end{verbatim}
405416

406-
The default action for error conditions is to return a special value
407-
such as infinity or not-a-number, but you can request that exceptions
408-
be raised:
417+
The default action for error conditions is selectable; the module can
418+
either return a special value such as infinity or not-a-number, or
419+
exceptions can be raised:
409420

410421
\begin{verbatim}
411422
>>> decimal.Decimal(1) / decimal.Decimal(0)
412-
Decimal("Infinity")
413-
>>> decimal.getcontext().trap_enablers[decimal.DivisionByZero] = True
414-
>>> decimal.Decimal(1) / decimal.Decimal(0)
415423
Traceback (most recent call last):
416424
...
417425
decimal.DivisionByZero: x / 0
426+
>>> decimal.getcontext().traps[decimal.DivisionByZero] = False
427+
>>> decimal.Decimal(1) / decimal.Decimal(0)
428+
Decimal("Infinity")
418429
>>>
419430
\end{verbatim}
420431

421432
The \class{Context} instance also has various methods for formatting
422433
numbers such as \method{to_eng_string()} and \method{to_sci_string()}.
423434

424-
435+
For more information, see the documentation for the \module{decimal}
436+
module, which includes a quick-start tutorial and a reference.
437+
425438
\begin{seealso}
426439
\seepep{327}{Decimal Data Type}{Written by Facundo Batista and implemented
427440
by Facundo Batista, Eric Price, Raymond Hettinger, Aahz, and Tim Peters.}

0 commit comments

Comments
 (0)