Skip to content

Converting float to Decimal by default is harmful #1625

@sqlalchemy-bot

Description

@sqlalchemy-bot

Migrated issue, originally created by Anonymous

(original reporter: ged) This is probably not new but I just got bitten by it... I think that having "asdecimal" defaults to True in Numeric is both (usually) useless and harmful because if the DBAPI is returning floats, you already lost precision, and converting them to Decimal won't help with the precision, so it does not do any good. Furthermore, it is harmful in two ways:

  • it will slow down the application quite a bit (decimal.Decimal is pure Python for now so it's at least 10x slower than floats). It's usually worth it for the sake of accuracy, but when you have already lost the accuracy, it only makes sense in very few cases.
  • more importantly, it hides the lost precision to the user: the user defines a NUMERIC on the DB side and works with Decimal in his Python code, so he thinks he's on the safe side for accuracy? Wrong! He gets the rounding errors anyway behind his back.

Now, there are a few ways to fix this problem:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions