Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Trapping Underflows and Overflows in Mata


From   "Joseph Coveney" <jcoveney@bigplanet.com>
To   "Statalist" <statalist@hsphsun2.harvard.edu>
Subject   st: Trapping Underflows and Overflows in Mata
Date   Wed, 26 Mar 2008 22:53:31 +0900

I gather that I can test for an overflow condition for a real scalar in Mata
by testing whether it's missing (when it's not supposed to be).

: x = maxdouble()

: x
 8.9885e+307

: x = 10 * x

: x
 .

: if (missing((x))) printf("Seems to have overflowed");;
Seems to have overflowed
:


But I need some help in trapping a potential underflow.

: x = smallestdouble()

: x = x / 10

: x
 4.4501e-309

: if (x) printf("Not underflowed");;
Not underflowed
:

It seems as if Mata is handling underflows gradually (denormalized numbers)
and not abruptly (set to zero).  So, instead of

   if (!result) . . .

it should be

   if ((result > 0 && result < smallestdouble()) ||
     (result < 0 && result  > -smallestdouble())) . . .

or

   if (result && result > -smallestdouble()) &&
     result < smallestdouble()) . . .

or perhaps

   if (result && abs(result) < smallestdouble()) . . .

Am I on track here?


I guess that I could also use some guidance on when I should expect
underflows to become problematic in Mata--for example, the following seem
hold up well:

: y = x = smallestdouble()

: (y / 10) / (x / 10)
 1

: (y / 100) / (x / 10)
 .1

:

But holding the exponent at -308 (or rather its equivalent in base 2) and
successively shifting the fraction rightward will eventually show
unacceptably degraded precision for a typical application.

Joseph Coveney


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index