Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: unexpected result of calculation

From   "Steichen" <[email protected]>
To   <[email protected]>
Subject   st: Re: unexpected result of calculation
Date   Tue, 2 Mar 2004 21:37:24 -0500

David Schriger writes:

> Would someone be kind enough to explain to me why the following
> produces a number when I use "15" but produces a missing for values > 18.
> This occurs whether I am doing a 'display' or 'generating' a variable.
> . di 9^140*(15^139)*exp(-9*15)/exp(lnfact(139))
> .28701151
> . di 9^140*(19^139)*exp(-9*19)/exp(lnfact(139))
> [no output produced]

Stata performs numeric operations from left to right following its rules for
the hierarchy of operators. Given those rules, it likely calculates 9^140,
then calculates 19^139, then multiplies them together. At that point, the
value exceeds the largest number that can be handled by the internal
precision used in Stata and a missing value is returned (you indicate "no
output produced" but I'm sure it returns the missing code, a period).  If
you rearrange your calculation so you divide earlier (thus keeping the
number within the internal storage range), you will get the correct answer:

. di 9^140 / exp(lnfact(139)) * (19^139) * exp(-9*19)

Note that I moved the "/ exp(lnfact(139))" term from the end to the "second"
position among the multiply and divide terms.


*   For searches and help try:

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