Rounding floats so that they sum to precisely 1

后端 未结 5 1963
臣服心动
臣服心动 2020-12-02 00:51

I have a rather gnarly bit of code that must more-or-less randomly generate a bunch of percentages, stored as decimal floats. That is, it decides that material one makes up

5条回答
  •  温柔的废话
    2020-12-02 01:34

    Since floats are stored in the machine in a binary representation, there are always numbers that can not be precisely represented. If you need to work around this limitation, you must use some math library, that uses custom defined datatypes.

提交回复
热议问题