fast approximated hypotenuse without squared root

1.7k Views Asked by At

imagine you are driving a car on a road going to a town, then you see a sign "yourDestTown: 84 km"

i am trying to find a way to calculate fast distances just as informational reference info, where precision is not important as speed (actually i am using this formula for a points of interest minecraft plugin)

  • how to get a fast and acceptable approximation to distance?
  • is this way ok?
  • have anyone a better or faster solution?
FAST APPROXIMATED HYPOTENUSE WITHOUT SQUARED ROOT FOR DISTANCE CALCULATIONS

i inspired this formula in quake3 "fast inverse square root"

let's consider a right triangle with "A" as long cathetus and "B" as short one,

     B
 +-------
 |      /
 |     /
A|    /
 |   /H
 |  /
 | /
 |/

we can take a proportional triangle with long catetus 1 and short one variable by dividing B/A

 b = B/A
 +-----
 |    /
1|   /
 |  /h
 | /
 |/

we can find a value to operate with b to find the hypotenuse "h", that later could multiply with "A" to find original hypotenuse lenght "H"

the function of hypotenuse against short leg is

      ______
     /     2
h = V 1 + b


  |        ,´
  |    _,-´
 1|---´  
  |
h |
  |
  +----------
  0         1  
            b

i noticed this curve looks similar to an hyperbola, which involves division i believe this should be the better approximation but my current math knowledge does not allow me to follow this calculatations

i think it also looks like the zone of a parabol near the foci

i want to find a value "V" in function of "b" so that multiplying "b*V" finds a good approximation of "h"

i found that the approximated equation "h ~= 1 + 0.43b^2 " satisfies fairly well the problem between range 0 <= b <= 1

                      ______
             2       /     2
    1 + 0.43b   ~=  V 1 + b

i think the curves looks very similar in specified range, so summarizing with A (longer) and B being right triangle legs and H being hypothenuse we got

b = B/A 2 H ~= A (1 + 0.43b )

until does not fit perfectly is ok for me since i just use it for information (a minecraft plugin that prints the approximated distance between 2 portals) maybe a correction could be added later by someone else (graph made it with geogebra online calculator)

graph

i found the max differences are about 0.015 (1.5%) in x~=0.6 and x=1, not so high for just informational info... maybe if you are driving a car and a road sign says "destTown: 203km" instead "200km" the difference is negligible, at 100km/h you will reach the town in 2:00 instead 2:02

would be useful if someone finds a more precise method with just basic operations (+-*) (no roots, trigonometry, logarithms or expensive ones) or huge lookup tables, at most a 2nd degree equation with a constant as it was for fast inverse square root

https://en.wikipedia.org/wiki/Fast_inverse_square_root

2

There are 2 best solutions below

1
On

Some observations and explanations, because I'm not 100% sure that there's actually a question here:

  • Note that $y = \sqrt{1 + x^2}$ is, in fact, a branch of a hyperbola, since it can be rearranged as $y^2 - x^2 = 1$.

  • You say that you can approximate $y = \sqrt{1 + x^2}$ by $1 + 0.43 x^2$. Notice that the first few terms of the Taylor series of $y = \sqrt{1 + x^2}$ centered at $x = 0$ are $$y = 1 + 0x + \frac 1 2 x^2 + ...$$ and since $0.43 \approx 0.5$, this explains the approximation you found. In fact, since the next term of the series is $-x^4 / 8 \le 0$, using a coefficient a little under $1/2$ for the $x^2$ term might be helping the approximation.

So you have some nice observations, but unfortunately it's probably not going to lead to a revolution....

11
On

How do you measure precision? Average over errors? Or some integral conditions? For small $b$ you have $$\sqrt{1+b^2} = 1 + \frac{1}{2}b^2-\frac{1}{8}b^4 + O(b^6).$$ Obviously your approximation is inaccurate for small $b$ if you look at the relative error.

The quadratic minimax approximation $$\sqrt{1+b^2} \approx 0.995961+0.066459b+0.355837 b^2 $$

has a maximum absolute error of about $0.004$ over the interval $[0,1].$