function pf = tdist( t, v )
% TDIST( t, v) returns A(t|v), the probability
%   of observing a value of t or greater in a Student's
%   t-distribution with v degrees of freedom.
%
%   e.g., t for which A(t|v)=0.05 when v=20 is 2.086;
%   this can be verified by confirming that
%   TDIST( 2.086, 20 ) returns a probability of 0.05 or 5 %.
%
%   To find the value 2.086, use TDISTINV.

% References: Press et al., Numerical Recipes,
%   Cambridge, 1986;

% ^ Calls functions BETAI, BETACF and GAMMLN  ^

a = v ./ 2;
b = 1 ./ 2;
x = v ./ ( v + t .* t) ;
if ( a < 0 ),
 pf = 1.0;
else
 pf = betai(a,b,x);
end
