Numerical operations when numbers are very large?

111 Views Asked by At

Explain the best way to evaluate $f(x,y) = \sqrt{(x^2 + y^2)}$ numerically when $x$ or $y$ are very large.

Does anyone have any insight to this? I'm lost. I usually know how to deal with these types of questions when it involves small numbers but no idea how to go about this.

2

There are 2 best solutions below

2
On

For the purposes of my example, I'm going to assume that both of your numbers are in the range of $10^{12}$. If you have $6*10^{12}$ for $x$ and $8*10^{12}$ for $y$, then you can factor out $10^{12}$ and end up with $$\sqrt{(10^{12})^2*(6^2+8^2)}.$$

This becomes $$\sqrt{6^2+8^2}*10^{12}.$$

If they are both large numbers, but very far apart in magnitude, then for all practical purposes you are going to get a number that's very close to the larger number and the smaller one might not even make it into your significant figures.

4
On

In case it helps, here is something like the idea behind kpres's answer turned into python code:

import math
def sqrt_sq_plus_sq(x, y):
   if abs(x) < abs(y): # swap x and y if necessary so that abs(x) >= abs(y)
      return sqrt_sq_plus_sq(y, x)
   (mx, ex) = math.frexp(x) # x = mx * 2**ex with 0 <= abs(mx) <= 1
   (my, ey) = math.frexp(y) # y = my * 2**ey with 0 <= abs(my) <= 1
   ny = my * (2**(ey - ex)) # y = ny * 2**ex
   return (2**ex) * math.sqrt(mx*mx + ny*ny)
      # return value is 2**ex * sqrt(mx^2 + ny^2) = sqrt(x^2 + y^2)

The python function math.frexp gives the (base 2) mantissa and exponent of a floating point number.

The idea is to scale the numbers up or down so that the mantissas are in the interval $[0, 1]$, do the calculation and then scale up or down to the right exponent. (Note that very small $x$ and $y$ are just as problematic as very large ones.)

Here are a couple of test runs:

>>> sqrt_sq_plus_sq(3e100,-4e100)
5e+100
>>> sqrt_sq_plus_sq(-5e-100, 12e-100)
1.3e-99