1
$\begingroup$

I'm learning Newton-Raphson to get the reciprocal of any arbitrary value. I need this in order to accurately emulate how the PlayStation 1 does the divide. They used a modified version of the algorithm.

Trying to understand the basic algorithm I came across this video.

Which is all fine and dandy, makes sense. But then as I started writing the code to get the reciprocal, I wasn't sure how to assign the initial guess value. In his example, he set it to 0.1f to find the reciprocal for 7. I found that if I set it to anything else, I would get totally different results. e.g. If I set it to 0.5f, I would get -217.839 in 3 iterations.

Code:

float GetRecip(float Number, float InitialGuess, int Iterations)
{
    float Result;

    Result = InitialGuess;
    for (int i = 0; i < Iterations; i++)
    {
        Result = Result * (2 - Number*Result);
    }

    return (Result);
}

// Example:
float Recip1 = GetRecip(7, 0.1f, 3); // 0.142847776
float Recip2 = GetRecip(7, 0.5f, 3); // -217.839844

Changing the number of iterations doesn't help, it would yield more drastic different results.

How do I go about finding that initial guess of 0.1f?

  • 0
    First step I would take is to write down the iterations: $y_{k+1}=2 y_k - y_k^2 x$. Now one can do the "energy method" I believe (calculate what the difference in energy between steps are, e.g. some kind of norm.). If the energy diff doesn't decrease its bad news. Maybe you can read up on convergence disks and stuff like that too.2017-01-16

1 Answers 1

0

The first step in the original link, counting leading zeros resp. the number of hex digits of the number, can be replicated with a loop

x0 = 1
while(N*x0>1) x0 = x0/16

where the division by 16 can be implemented as bit manipulation of the exponent in a floating point format or as a bit shift in a fixed point format.

Or directly extract the exponent from the float format of the input and set a corresponding negated exponent in the initial point.

  • 0
    Thanks. More a partial answer but still helps. Do you know what they're doing with that unr table? What does it represent?2017-01-16