#! /usr/bin/perl
#
#
# Calculate two-tailed significance level associated with Z(x) with x = ARGV[0]
#
# i.e., P(|Z|>=x|Normal Distribution), error(x) < 7.5*10(-8)
#
# Probabilities are calculated according to: 
# Abramowitz and Stegun,
# Handbook of mathematical functions
# Ninth printing, Dover publications, Inc., 1970                  
# p932, 26.2.17
# 
# 
# Copyright (C) 1996, 2001  Rob van Son
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
# The absolute function
sub abs{
($_[0] >= 0) ? $_[0] : -$_[0];
}
#
$x = &abs($ARGV[0]);
#
# P(x) = 1 - Z(x)(b1*t+b2*t**2+b3*t**3+b4*t**4+b5*t**5
# Z(x) = exp(-$x*$x/2.0)/(sqrt(2*3.14159265358979323846))
# t = 1/(1+p*x)
#
# Parameters
@b = (0.319381530, -0.356563782, 1.781477937, -1.821255978, 1.330274429);
$p = 0.2316419;
$t = 1/(1+$p*$x);
# Initialize variables
$fact = $t;
$Sum = 0;
# Sum polynomial
for($i=0; $i <= $#b; ++$i)
{ 
  $Sum += $b[$i]*$fact;
  $fact *= $t;
};
# Calculate probability
$p = 2*$Sum*exp(-$x*$x/2.0)/(sqrt(2*3.14159265358979323846));
# Write output
printf("%7.7f", $p);
