How to actually avoid floating point errors when you need to use float?

前端 未结 4 922
攒了一身酷
攒了一身酷 2021-01-13 18:11

I am trying to affect the translation of a 3D model using some UI buttons to shift the position by 0.1 or -0.1.

My model position is a three dimensional float so sim

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-13 18:49

    If you stick with floats: The easiest way to avoid the error is using floats which are exact, but near the desired value which is

    round(2^n * value) * 1/2^n.

    n is the number of bits, value the number to use (in your case 0.1)

    In your case with increasing precision:

    n = 4 => 0.125
    n = 8 (byte) => 0.9765625
    n = 16 (short)=> 0.100006103516....

    The long number chains are artefacts of the binary conversion, the real number has much less bits.

    As the floats are exact, addition and subtraction will not introduce offset errors, but will always be predictable as long as the number of bits is not longer than the float value holds.

    If you fear that your display will be compromised by using this solution (because they are odd floats), use and store only integers (step increase -1/1). The final value which is internally set is

    x = value * step.

    As the step increases or decreases by an amount of 1, precision will be retained.

提交回复
热议问题