1

I have to create a function where I need to square values. I am told that even if the initial value is not too big or is not too small, the value squared might still overflow (returns inf) or underflow (returns 0), and I need to figure out how to prevent that.

My problem is I do not even understand what numbers can cause an overflow when squared when the number itself is not too large.

I thought it might be for the not too small numbers, e.g. 1/3 with repeating decimal numbers, but MATLAB just turns that into 0.3333.

Can someone give me an example of such a number or explain what kind of numbers would cause this?

Schorsch
  • 7,761
  • 6
  • 39
  • 65
user1804234
  • 331
  • 1
  • 3
  • 13

1 Answers1

2

For underflow, let's consider Planck's constant: 6.626070040e-34

sqrt(6.626070040e-34)
ans =
   2.5741e-17

Well, that's apparently not small enough, let's go smaller:

sqrt(6.626070040e-340)
ans =
     0

There's your underflow.

Overflow can be seen the same way, just use big numbers:

sqrt(6.626070040e34)
ans =
   2.5741e+17

sqrt(6.626070040e340)
ans =
   Inf

Underflow means the numbers are too small for MATLAB to handle, overflow means they are too big for MATLAB to handle.

Thanks to @obchardon here are the numbers on my MATLAB R2012a 64bits system:

realmax('double') %//largest allowed double
ans =
  1.7977e+308
realmin('double') %//smallest allowed double
ans =
  2.2251e-308

Now that we know what the largest possible value is that MATLAB can handle, let's try going below that and square it:

(realmax('double')-10).^2
ans =
   Inf

so the number we tried to square here (realmax('double')-10) is allowable by MATLAB, but not squarable.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 2
    Technically `realmin` is only the smallest *normalised* double. `eps(realmin)`, for example, returns a number smaller than `realmin`... – Dan Oct 08 '15 at 15:32
  • Wait, the part of the question I'm doing says this "even if a number is not too large, its square may overflow" don't the number you gave as an example count as too big? – user1804234 Oct 08 '15 at 15:37
  • `sqrt(realmax('double')+1)` works here, as does `sqrt(realmax('double')-1)`. You could try looping over numbers to see where the exact limit is. – Adriaan Oct 08 '15 at 15:41
  • so there isn't any special small number(small number of digits) where the square of that number will overflow? – user1804234 Oct 08 '15 at 16:09
  • I read 'square root', not 'square'. This actually makes a large difference. Basically every number larger than `sqrt(realmax('double'))` will be larger than MATLAB's largest allowable number. – Adriaan Oct 08 '15 at 16:16
  • @user1804234: square of small numbers will **under**flow. Since numbers smaller than 1 will become *smaller* when you square them, really small numbers can become small enough to become zero within machine precision. For instance, `eps(0)==4.9407e-324`, and `eps(0)^2==0`. (Don't ask me why `eps(0)` is not the same as `realmin`, I would've guessed them to be the same. OK: ask Dan who commented on just this above.) – Andras Deak -- Слава Україні Oct 08 '15 at 21:35
  • 1
    @Adriaan I don't think you'd need to loop to find what you had to add to `realmax` to make it overflow, that is what `eps` does. Just try `eps(realmax)` – Dan Oct 09 '15 at 06:20