Lo que todo programador debería saber sobre aritmética de punto flotante
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Formatting corrections

+7 -9
+7 -9
content/languages/python.html
··· 7 7 -------- 8 8 Almost all platforms map Python floats to [IEEE 754](/formats/fp/) 9 9 double precision. 10 - 11 - f = 0.1 10 + f = 0.1 12 11 13 12 Decimal Types 14 13 ------------- 15 14 Python has an [arbitrary-precision](/formats/exact/) decimal type named <code>Decimal</code> in the <code>decimal</code> module, which 16 15 also allows to choose the [rounding mode](/errors/rounding/). 17 - a = Decimal('0.1') 18 - b = Decimal('0.2') 19 - c = a + b # returns a Decimal representing exactly 0.3 20 - 16 + a = Decimal('0.1') 17 + b = Decimal('0.2') 18 + c = a + b # returns a Decimal representing exactly 0.3 21 19 22 20 How to Round 23 21 ------------ 24 22 To get a string: 25 - "%.2f" % 1.2399 # returns "1.24" 23 + "%.2f" % 1.2399 # returns "1.24" 26 24 "%.3f" % 1.2399 # returns "1.240" 27 25 "%.2f" % 1.2 # returns "1.20" 28 26 To print to standard output: 29 27 print "%.2f" % 1.2399 # just use print and string formatting 30 - If you need a specific [rounding mode](/errors/rounding/) and other parameters can be defined in a Context object: 31 - getcontext().prec = 7 28 + Specific [rounding modes](/errors/rounding/) and other parameters can be defined in a Context object: 29 + getcontext().prec = 7 32 30 33 31 Resources 34 32 ---------