📄 s_erfl.c
字号:
/* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== *//* Modifications and expansions for 128-bit long double are Copyright (C) 2001 Stephen L. Moshier <moshier@na-net.ornl.gov> and are incorporated herein by permission of the author. The author reserves the right to distribute this material elsewhere under different copying permissions. These modifications are distributed here under the following terms: This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* double erf(double x) * double erfc(double x) * x * 2 |\ * erf(x) = --------- | exp(-t*t)dt * sqrt(pi) \| * 0 * * erfc(x) = 1-erf(x) * Note that * erf(-x) = -erf(x) * erfc(-x) = 2 - erfc(x) * * Method: * 1. erf(x) = x + x*R(x^2) for |x| in [0, 7/8] * Remark. The formula is derived by noting * erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....) * and that * 2/sqrt(pi) = 1.128379167095512573896158903121545171688 * is close to one. * * 1a. erf(x) = 1 - erfc(x), for |x| > 1.0 * erfc(x) = 1 - erf(x) if |x| < 1/4 * * 2. For |x| in [7/8, 1], let s = |x| - 1, and * c = 0.84506291151 rounded to single (24 bits) * erf(s + c) = sign(x) * (c + P1(s)/Q1(s)) * Remark: here we use the taylor series expansion at x=1. * erf(1+s) = erf(1) + s*Poly(s) * = 0.845.. + P1(s)/Q1(s) * Note that |P1/Q1|< 0.078 for x in [0.84375,1.25] * * 3. For x in [1/4, 5/4], * erfc(s + const) = erfc(const) + s P1(s)/Q1(s) * for const = 1/4, 3/8, ..., 9/8 * and 0 <= s <= 1/8 . * * 4. For x in [5/4, 107], * erfc(x) = (1/x)*exp(-x*x-0.5625 + R(z)) * z=1/x^2 * The interval is partitioned into several segments * of width 1/8 in 1/x. * * Note1: * To compute exp(-x*x-0.5625+R/S), let s be a single * precision number and s := x; then * -x*x = -s*s + (s-x)*(s+x) * exp(-x*x-0.5626+R/S) = * exp(-s*s-0.5625)*exp((s-x)*(s+x)+R/S); * Note2: * Here 4 and 5 make use of the asymptotic series * exp(-x*x) * erfc(x) ~ ---------- * ( 1 + Poly(1/x^2) ) * x*sqrt(pi) * * 5. For inf > x >= 107 * erf(x) = sign(x) *(1 - tiny) (raise inexact) * erfc(x) = tiny*tiny (raise underflow) if x > 0 * = 2 - tiny if x<0 * * 7. Special case: * erf(0) = 0, erf(inf) = 1, erf(-inf) = -1, * erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2, * erfc/erf(NaN) is NaN */#include "math.h"#include "math_private.h"#include <math_ldbl_opt.h>/* Evaluate P[n] x^n + P[n-1] x^(n-1) + ... + P[0] */static long doubleneval (long double x, const long double *p, int n){ long double y; p += n; y = *p--; do { y = y * x + *p--; } while (--n > 0); return y;}/* Evaluate x^n+1 + P[n] x^(n) + P[n-1] x^(n-1) + ... + P[0] */static long doubledeval (long double x, const long double *p, int n){ long double y; p += n; y = x + *p--; do { y = y * x + *p--; } while (--n > 0); return y;}#ifdef __STDC__static const long double#elsestatic long double#endiftiny = 1e-300L, half = 0.5L, one = 1.0L, two = 2.0L, /* 2/sqrt(pi) - 1 */ efx = 1.2837916709551257389615890312154517168810E-1L, /* 8 * (2/sqrt(pi) - 1) */ efx8 = 1.0270333367641005911692712249723613735048E0L;/* erf(x) = x + x R(x^2) 0 <= x <= 7/8 Peak relative error 1.8e-35 */#define NTN1 8static const long double TN1[NTN1 + 1] ={ -3.858252324254637124543172907442106422373E10L, 9.580319248590464682316366876952214879858E10L, 1.302170519734879977595901236693040544854E10L, 2.922956950426397417800321486727032845006E9L, 1.764317520783319397868923218385468729799E8L, 1.573436014601118630105796794840834145120E7L, 4.028077380105721388745632295157816229289E5L, 1.644056806467289066852135096352853491530E4L, 3.390868480059991640235675479463287886081E1L};#define NTD1 8static const long double TD1[NTD1 + 1] ={ -3.005357030696532927149885530689529032152E11L, -1.342602283126282827411658673839982164042E11L, -2.777153893355340961288511024443668743399E10L, -3.483826391033531996955620074072768276974E9L, -2.906321047071299585682722511260895227921E8L, -1.653347985722154162439387878512427542691E7L, -6.245520581562848778466500301865173123136E5L, -1.402124304177498828590239373389110545142E4L, -1.209368072473510674493129989468348633579E2L/* 1.0E0 */};/* erf(z+1) = erf_const + P(z)/Q(z) -.125 <= z <= 0 Peak relative error 7.3e-36 */static const long double erf_const = 0.845062911510467529296875L;#define NTN2 8static const long double TN2[NTN2 + 1] ={ -4.088889697077485301010486931817357000235E1L, 7.157046430681808553842307502826960051036E3L, -2.191561912574409865550015485451373731780E3L, 2.180174916555316874988981177654057337219E3L, 2.848578658049670668231333682379720943455E2L, 1.630362490952512836762810462174798925274E2L, 6.317712353961866974143739396865293596895E0L, 2.450441034183492434655586496522857578066E1L, 5.127662277706787664956025545897050896203E-1L};#define NTD2 8static const long double TD2[NTD2 + 1] ={ 1.731026445926834008273768924015161048885E4L, 1.209682239007990370796112604286048173750E4L, 1.160950290217993641320602282462976163857E4L, 5.394294645127126577825507169061355698157E3L, 2.791239340533632669442158497532521776093E3L, 8.989365571337319032943005387378993827684E2L, 2.974016493766349409725385710897298069677E2L, 6.148192754590376378740261072533527271947E1L, 1.178502892490738445655468927408440847480E1L /* 1.0E0 */};/* erfc(x + 0.25) = erfc(0.25) + x R(x) 0 <= x < 0.125 Peak relative error 1.4e-35 */#define NRNr13 8static const long double RNr13[NRNr13 + 1] ={ -2.353707097641280550282633036456457014829E3L, 3.871159656228743599994116143079870279866E2L, -3.888105134258266192210485617504098426679E2L, -2.129998539120061668038806696199343094971E1L, -8.125462263594034672468446317145384108734E1L, 8.151549093983505810118308635926270319660E0L, -5.033362032729207310462422357772568553670E0L, -4.253956621135136090295893547735851168471E-2L, -8.098602878463854789780108161581050357814E-2L};#define NRDr13 7static const long double RDr13[NRDr13 + 1] ={ 2.220448796306693503549505450626652881752E3L, 1.899133258779578688791041599040951431383E2L, 1.061906712284961110196427571557149268454E3L, 7.497086072306967965180978101974566760042E1L, 2.146796115662672795876463568170441327274E2L, 1.120156008362573736664338015952284925592E1L, 2.211014952075052616409845051695042741074E1L, 6.469655675326150785692908453094054988938E-1L /* 1.0E0 */};/* erfc(0.25) = C13a + C13b to extra precision. */static const long double C13a = 0.723663330078125L;static const long double C13b = 1.0279753638067014931732235184287934646022E-5L;/* erfc(x + 0.375) = erfc(0.375) + x R(x) 0 <= x < 0.125 Peak relative error 1.2e-35 */#define NRNr14 8static const long double RNr14[NRNr14 + 1] ={ -2.446164016404426277577283038988918202456E3L, 6.718753324496563913392217011618096698140E2L, -4.581631138049836157425391886957389240794E2L, -2.382844088987092233033215402335026078208E1L, -7.119237852400600507927038680970936336458E1L, 1.313609646108420136332418282286454287146E1L, -6.188608702082264389155862490056401365834E0L, -2.787116601106678287277373011101132659279E-2L, -2.230395570574153963203348263549700967918E-2L};#define NRDr14 7static const long double RDr14[NRDr14 + 1] ={ 2.495187439241869732696223349840963702875E3L, 2.503549449872925580011284635695738412162E2L, 1.159033560988895481698051531263861842461E3L, 9.493751466542304491261487998684383688622E1L, 2.276214929562354328261422263078480321204E2L, 1.367697521219069280358984081407807931847E1L, 2.276988395995528495055594829206582732682E1L, 7.647745753648996559837591812375456641163E-1L /* 1.0E0 */};/* erfc(0.375) = C14a + C14b to extra precision. */static const long double C14a = 0.5958709716796875L;static const long double C14b = 1.2118885490201676174914080878232469565953E-5L;/* erfc(x + 0.5) = erfc(0.5) + x R(x) 0 <= x < 0.125 Peak relative error 4.7e-36 */#define NRNr15 8static const long double RNr15[NRNr15 + 1] ={ -2.624212418011181487924855581955853461925E3L, 8.473828904647825181073831556439301342756E2L, -5.286207458628380765099405359607331669027E2L, -3.895781234155315729088407259045269652318E1L, -6.200857908065163618041240848728398496256E1L, 1.469324610346924001393137895116129204737E1L, -6.961356525370658572800674953305625578903E0L, 5.145724386641163809595512876629030548495E-3L, 1.990253655948179713415957791776180406812E-2L};#define NRDr15 7static const long double RDr15[NRDr15 + 1] ={ 2.986190760847974943034021764693341524962E3L, 5.288262758961073066335410218650047725985E2L, 1.363649178071006978355113026427856008978E3L, 1.921707975649915894241864988942255320833E2L, 2.588651100651029023069013885900085533226E2L, 2.628752920321455606558942309396855629459E1L, 2.455649035885114308978333741080991380610E1L, 1.378826653595128464383127836412100939126E0L /* 1.0E0 */};/* erfc(0.5) = C15a + C15b to extra precision. */static const long double C15a = 0.4794921875L;static const long double C15b = 7.9346869534623172533461080354712635484242E-6L;/* erfc(x + 0.625) = erfc(0.625) + x R(x)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -