Rounding floats with representation error to the closest and correct result

前端 未结 2 867
温柔的废话
温柔的废话 2021-01-12 12:27

I have a situation when the classic representation error in Python started to be a problem: I need them to be used for Matrix operations in Numpy and the decimal

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 13:03

    The real issue here is to identify how the representation error could start to be a problem in the first place.

    You should not be using the representation for human-readable printing of floats, you should be using for example str.format or some other presentation method.

    Keep your numbers as numbers for all calculations (whether you do that with floats or Decimals or whatever...) and only round or truncate things at presentation.

提交回复
热议问题