[Python-ideas] Python Float Update

Stephen J. Turnbull stephen at xemacs.org
Tue Jun 2 06:21:48 CEST 2015

Joonas Liik writes:

 > Having some sort of decimal literal would have some advantages of its own,
 > for one it could help against this sillyness:

 > I'm not saying that the actual data type needs to be a decimal (
 > might well be a float but say shove the string repr next to it so
 > it can be accessed when needed)

That *would* be a different type from float.  You may as well go all
the way to Decimal.

 > ..but this is one really common pitfall for new users,

To fix it, you really need to change the parser, i.e., make Decimal
the default type for non-integral numbers.  "Decimal('1.3')" isn't
that much harder to remember than "1.3$" (although it's quite a bit
more to type).  But people are going to continue writing things like

    pennies = 13
    pennies_per_dollar = 100
    dollars = pennies / pennies_per_dollar
    # Much later ...
    future_value = dollars * Decimal('1.07')

And in real applications you're going to be using Decimal in code like

    def inputDecimals(file):
        for row, line in enumerate(file):
            for col, value in enumerate(line.strip().split()):
                matrix[row][col] = Decimal(value)


    def what_if():
        principal = Decimal(input("Principal ($): "))
        rate = Decimal(input("Interest rate (%): "))
        print("Future value is ",
              principal * (1 + rate/100),
              ".", sep="")

and the whole issue evaporates.

More information about the Python-ideas mailing list