001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.special;
018    
019    import org.apache.commons.math.MathException;
020    
021    /**
022     * This is a utility class that provides computation methods related to the
023     * error functions.
024     *
025     * @version $Revision: 780933 $ $Date: 2009-06-02 00:39:12 -0400 (Tue, 02 Jun 2009) $
026     */
027    public class Erf {
028    
029        /**
030         * Default constructor.  Prohibit instantiation.
031         */
032        private Erf() {
033            super();
034        }
035    
036        /**
037         * Returns the error function erf(x).
038         * 
039         * The implementation of this method is based on:
040         * <ul>
041         * <li>
042         * <a href="http://mathworld.wolfram.com/Erf.html">
043         * Erf</a>, equation (3).</li>
044         * </ul>
045         * 
046         * @param x the value.
047         * @return the error function erf(x)
048         * @throws MathException if the algorithm fails to converge.
049         */
050        public static double erf(double x) throws MathException {
051            double ret = Gamma.regularizedGammaP(0.5, x * x, 1.0e-15, 10000);
052            if (x < 0) {
053                ret = -ret;
054            }
055            return ret;
056        }
057    }