******************************************************************************
*                              Function QSTUD
*                             ~~~~~~~~~~~~~~~~
*  Purpose:
*
*     Compute a quantile of the Student distribution
*
*  Usage:
*
*     QSTUD ( Q, M, IND )
*
*  Input arguments:
*
*       Q - significance level, %, REAL*8
*       M - number of degree of freedom, INTEGER*4
*     IND - option parameter, INTEGER*4:
*            IND=1 - one-sided region
*            IND=2 - two-sided region
*
*  Returned value:
*
*     Quantile of the Student distribution, REAL*8
*
*  Called routines:
*
*     none
*
*  Method:
*
*        For M<6 the table value from Table 3.2 from L.N. Bol'shev and
*     N.V. Smirnov, Tables of mathematical statistics (Tablitsy matematicheskoi
*     statistiki), 3rd Edition, Moscow: Nauka, 1983, is returned
*
*        For M>=6 author's approximation is used. Approximatioin error does
*     not exceed:
*         0.0001 for Q=5,10 (10,20),
*         0.0003 for Q=2.5 (5),
*         0.0009 for Q=1 (2),
*         0.0016 for Q=0.5 (1),
*         0.0029 for Q=0.25 (0.5),
*         0.0055 for Q=0.1 (0.2),
*         0.0085 for Q=0.05 (0.1),
*     where teh first Q values are related to a one-sided region, and Q values
*     in parenthesis are related to a two-sided region.
*
*  Notes:
*
*        1. Valid Q values are 10, 5, 2.5, 1, 0.5, 0.25, 0.1, 0.05 for
*     a one-sided region and 20, 10, 5, 2, 1, 0.5, 0.2, 0.1 for a two-sided
*     region.
*        2. If given value of Q or IND is not valid, or M<1, -1 is returned.
*
*
* (C) Z.Malkin, 1992-2026
*
* Distributed under the GNU General Public License.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
******************************************************************************

      FUNCTION QSTUD ( Q, M, IND )

      IMPLICIT NONE

      INTEGER(4), PARAMETER :: ITL = 5          ! last M in table
      INTEGER(4)  M, IND, I, IQ

      REAL(8)  QSTUD, Q, QQ

      REAL(8)  QS(8) / 20D0, 10D0, 5D0, 2D0, 1D0, 0.5D0, 0.2D0, 0.1D0 /

* Table data.
      REAL(8)  QT(8,ITL)
     &  / 3.0777D0,   6.3138D0,  12.7062D0,  31.8205D0,
     &      63.6567D0, 127.3213D0, 318.3088D0, 636.6192D0,
     &    1.8856D0,   2.9200D0,   4.3027D0,   6.9646D0,
     &      9.9248D0,  14.0890D0,  22.3271D0,  31.5991D0,
     &    1.6377D0,   2.3534D0,   3.1824D0,   4.5407D0,
     &      5.8409D0,   7.4533D0,  10.2145D0,  12.9240D0,
     &    1.5332D0,   2.1318D0,   2.7764D0,   3.7469D0,
     &      4.6041D0,   5.5976D0,   7.1732D0,   8.6103D0,
     &    1.4759D0,   2.0150D0,   2.5706D0,   3.3649D0,
     &      4.0321D0,   4.7733D0,   5.8934D0,   6.8688D0 /

* Coefficients of the approximation expression.
      REAL(8)  QC(3,8)
     &  / 1.28152D0,   0.84831D0,  -.064067D0,
     &    1.64480D0,   1.52850D0,  -.087981D0,
     &    1.95980D0,   2.38476D0,  -1.10725D0,
     &    2.32591D0,   3.76264D0,  -1.39822D0,
     &    2.57499D0,   4.97934D0,  -1.60924D0,
     &    2.80555D0,   6.34022D0,  -1.81259D0,
     &    3.08731D0,   8.35661D0,  -2.06889D0,
     &    3.28595D0,  10.04544D0,  -2.25352D0  /

* ----------------------------------------------------------------------------

      QSTUD = -1D0

      IF ( IND .EQ. 1 )  THEN
         QQ = Q + Q
      ELSE IF ( IND .EQ. 2 ) THEN
         QQ = Q
      ELSE
         RETURN
      END IF

      DO I=1,8
         IF ( NINT(QQ*100D0) .EQ. NINT(QS(I)*100D0) )  THEN
            IQ = I
            GOTO 1
         END IF
      END DO

      RETURN

   1  CONTINUE

      IF ( M .LT. 1 )  THEN
         RETURN
      ELSE IF ( M .LE. ITL )  THEN
         QSTUD = QT(IQ,M)
      ELSE
         QSTUD = QC(1,IQ) + QC(2,IQ) / (M+QC(3,IQ))
      END IF

      END   ! QSTUD
