-----------------------------------------------------
#
# ::math::Beta --
#
#	Return the value of the Beta function of parameters z and w.
#
# Parameters:
#	z, w : Two real parameters to the Beta function
#
# Results:
#	Returns the value of the Beta function.
#
# Side effects:
#	None.
#
# Beta( w, z ) is defined as:
#
#				  1_
#				  |  (z-1)     (w-1)
# Beta( w, z ) = Beta( z, w ) =	  | t	  (1-t)	     dt
#				 _|
#				  0
#
#	       = Gamma( z ) Gamma( w ) / Gamma( z + w )
#
# Results are returned as a floating point number precise to better
# than nine significant figures for w, z > 1.
#
#----------------------------------------------------------------------

proc ::math::Beta { z w } {

    # Check form of both args so that domain check can be made

    if { ![string is double $z] } {
	return -code error [expectDouble $z]
    }
    if { ![string is double $w] } {
	return -code error [expectDouble $w]
    }

    # Check sign of both args

    if { $z <= 0.0 } {
	set proc [lindex [info level 0] 0]
	set msg "first argument to $proc must be positive"
	return -code error -errorcode [list ARITH DOMAIN $msg] $msg
    }
    if { $w <= 0.0 } {
	set proc [lindex [info level 0] 0]
	set msg "second argument to $proc must be positive"
	return -code error -errorcode [list ARITH DOMAIN $msg] $msg
    }

    # Compute beta using gamma function, keeping stack trace clean.

    if { [catch { expr { exp( [ln_Gamma $z] + [ln_Gamma $w]
			      - [ln_Gamma [ expr { $z + $w }]] ) } } beta] } {

	return -code error -errorcode $::errorCode $beta

    } 

    return $beta

}
