Difficult arithmetic or sleight of hand?

Vincent responded with the Fast Arc Cos algorithm by proposing this feature.

float arccos(float x) 
{ 
    x = 1 - (x + 1); 
    return pi * x / 2; 
} 

The question is why x = 1 - (x + 1), and not x = -x?

+3
source share
3 answers

It returns a different result only when (x + 1) causes a loss of accuracy, i.e. x is many orders of magnitude greater or less than one.

But I don’t think it’s difficult or clever, I think it’s just wrong .

cos(0) = 1 but f(1) = -pi/2
cos(pi/2) = 0 but f(0) = 0
cos(pi) = -1 but f(-1) = pi/2

where f(x)is the implementation of Vincent arccos. All of them are disabled on pi/2, the linear approximation, which will get at least these three points correctly, will be

g(x) = (1 - x) * pi / 2
+3
source

, , , x 1 -1 .

0

The addition leads to the normalization of both numbers (in this case, refers to x). IIRC, in Knuth Volume 2, in the chapter on floating point arithmetic, you can even see an expression like x + 0.

0
source

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


All Articles