Preservation of the greatest possible accuracy during float interpolation

We know that points X1 and X2 have corresponding points Y1 and Y2, so we can calculate Y for any X with:

 X - X1    Y - Y1
------- = -------
X2 - X1   Y2 - Y1

From simple formula (A) you can get:

Y = (X - X1) * (Y2 - Y1) / (X2 - X1) + Y1;

This should be mathematically equivalent (B):

Y = (X - X1) / (X2 - X1) * (Y2 - Y1) + Y1;

For an integer mathematical formula, A works better as long as the result of the multiplication (X - X1) * (Y2 - Y1)remains in the range of type. Formula B will not work, because if X1 <= X <= X2, then division will always be equal 0.

For floating point, both should work, but I think B will offer better accuracy since the result of multiplication will be less.

  • Is my assumption about floating point accuracy correct?

  • Is there some kind of floating point quirk that I don't take into account?

, IEEE 754.

1: , .

2: FP , NaN Infs .

+4
3

, . , , . 2e100/3e100 2/3 . (/ ).

, , , , .

. "" (.. /), . , , , .

, , (A) (B), (A) (C):

Y = (X - X1) * (Y2 - Y1) / (X2 - X1) + Y1; (A)
Y = (X - X2) * (Y2 - Y1) / (X2 - X1) + Y2; (C)

, X - X1 X - X2 . , Y , .

,

(X1,Y1) = (-100, -100)
(X2,Y2) = (0, 0)
X = 0.76

. (A):

Y = (0.76 - -100) * (0 - -100) / (0 - -100) + -100
  = 101 * 100 / 100 - 100
  = 1

(C) :

Y = (0.76 - 0) * (0 - -100) / (0 - -100) + 0
  = 0.76 * 100 / 100 + 0
  = 0.76

, :

  • . (B) (A).

  • .

+1

Y

 X - X1    Y - Y1
------- = -------
X2 - X1   Y2 - Y1

(A) (B) :

(A)    Y = (X - offsetX) * deltaY / deltaX + offsetY;
(B)    Y = (X - offsetX) / deltaX * deltaY + offsetY;

, "B... ". , |deltaX| |deltaY| 1, .

, ( 2 , ). X1,Y1 X2,Y2 , 1 2. X , Y .

FP, * / , FP: , , .

+ - : , , , .


, 2x .

, iy = (int) round(Y);

+3

, , : , , , , , .

- , , , .

+2

Source: https://habr.com/ru/post/1570843/


All Articles