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

前端 未结 4 969
攒了一身酷
攒了一身酷 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

    I would use a Rational class. There are many out there - this one looks like it should work.

    One significant cost will be when the Rational is rendered into a float and one when the denominator is reduced to the gcd. The one I posted keeps the numerator and denominator in fully reduced state at all times which should be quite efficient if you are always adding or subtracting 1/10.

    This implementation holds the values normalised (i.e. with consistent sign) but unreduced.

    You should choose your implementation to best fit your usage.

提交回复
热议问题