📄 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"/* 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-4931L, 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) 0 <= x < 0.125 Peak relative error 5.1e-36 */#define NRNr16 8static const long double RNr16[NRNr16 + 1] ={ -2.347887943200680563784690094002722906820E3L, 8.008590660692105004780722726421020136482E2L, -5.257363310384119728760181252132311447963E2L, -4.471737717857801230450290232600243795637E1L, -4.849540386452573306708795324759300320304E1L, 1.140885264677134679275986782978655952843E1L, -6.731591085460269447926746876983786152300E0L, 1.370831653033047440345050025876085121231E-1L, 2.022958279982138755020825717073966576670E-2L,};#define NRDr16 7static const long double RDr16[NRDr16 + 1] ={ 3.075166170024837215399323264868308087281E3L, 8.730468942160798031608053127270430036627E2L, 1.458472799166340479742581949088453244767E3L, 3.230423687568019709453130785873540386217E2L, 2.804009872719893612081109617983169474655E2L, 4.465334221323222943418085830026979293091E1L, 2.612723259683205928103787842214809134746E1L, 2.341526751185244109722204018543276124997E0L, /* 1.0E0 */};/* erfc(0.625) = C16a + C16b to extra precision. */static const long double C16a = 0.3767547607421875L;static const long double C16b = 4.3570693945275513594941232097252997287766E-6L;/* erfc(x + 0.75) = erfc(0.75) + x R(x) 0 <= x < 0.125 Peak relative error 1.7e-35 */#define NRNr17 8static const long double RNr17[NRNr17 + 1] ={ -1.767068734220277728233364375724380366826E3L, 6.693746645665242832426891888805363898707E2L, -4.746224241837275958126060307406616817753E2L, -2.274160637728782675145666064841883803196E1L, -3.541232266140939050094370552538987982637E1L, 6.988950514747052676394491563585179503865E0L, -5.807687216836540830881352383529281215100E0L, 3.631915988567346438830283503729569443642E-1L, -1.488945487149634820537348176770282391202E-2L};#define NRDr17 7static const long double RDr17[NRDr17 + 1] ={ 2.748457523498150741964464942246913394647E3L, 1.020213390713477686776037331757871252652E3L, 1.388857635935432621972601695296561952738E3L, 3.903363681143817750895999579637315491087E2L, 2.784568344378139499217928969529219886578E2L, 5.555800830216764702779238020065345401144E1L, 2.646215470959050279430447295801291168941E1L, 2.984905282103517497081766758550112011265E0L, /* 1.0E0 */};/* erfc(0.75) = C17a + C17b to extra precision. */static const long double C17a = 0.2888336181640625L;static const long double C17b = 1.0748182422368401062165408589222625794046E-5L;/* erfc(x + 0.875) = erfc(0.875) + x R(x) 0 <= x < 0.125 Peak relative error 2.2e-35 */#define NRNr18 8static const long double RNr18[NRNr18 + 1] ={ -1.342044899087593397419622771847219619588E3L, 6.127221294229172997509252330961641850598E2L, -4.519821356522291185621206350470820610727E2L, 1.223275177825128732497510264197915160235E1L, -2.730789571382971355625020710543532867692E1L, 4.045181204921538886880171727755445395862E0L, -4.925146477876592723401384464691452700539E0L, 5.933878036611279244654299924101068088582E-1L, -5.557645435858916025452563379795159124753E-2L};#define NRDr18 7static const long double RDr18[NRDr18 + 1] ={ 2.557518000661700588758505116291983092951E3L, 1.070171433382888994954602511991940418588E3L, 1.344842834423493081054489613250688918709E3L, 4.161144478449381901208660598266288188426E2L, 2.763670252219855198052378138756906980422E2L, 5.998153487868943708236273854747564557632E1L, 2.657695108438628847733050476209037025318E1L, 3.252140524394421868923289114410336976512E0L, /* 1.0E0 */};/* erfc(0.875) = C18a + C18b to extra precision. */static const long double C18a = 0.215911865234375L;static const long double C18b = 1.3073705765341685464282101150637224028267E-5L;/* erfc(x + 1.0) = erfc(1.0) + x R(x) 0 <= x < 0.125 Peak relative error 1.6e-35 */#define NRNr19 8static const long double RNr19[NRNr19 + 1] ={ -1.139180936454157193495882956565663294826E3L, 6.134903129086899737514712477207945973616E2L, -4.628909024715329562325555164720732868263E2L, 4.165702387210732352564932347500364010833E1L, -2.286979913515229747204101330405771801610E1L, 1.870695256449872743066783202326943667722E0L, -4.177486601273105752879868187237000032364E0L, 7.533980372789646140112424811291782526263E-1L, -8.629945436917752003058064731308767664446E-2L};#define NRDr19 7static const long double RDr19[NRDr19 + 1] ={ 2.744303447981132701432716278363418643778E3L, 1.266396359526187065222528050591302171471E3L, 1.466739461422073351497972255511919814273E3L, 4.868710570759693955597496520298058147162E2L, 2.993694301559756046478189634131722579643E2L, 6.868976819510254139741559102693828237440E1L, 2.801505816247677193480190483913753613630E1L, 3.604439909194350263552750347742663954481E0L, /* 1.0E0 */};/* erfc(1.0) = C19a + C19b to extra precision. */static const long double C19a = 0.15728759765625L;static const long double C19b = 1.1609394035130658779364917390740703933002E-5L;/* erfc(x + 1.125) = erfc(1.125) + x R(x) 0 <= x < 0.125 Peak relative error 3.6e-36 */#define NRNr20 8static const long double RNr20[NRNr20 + 1] ={ -9.652706916457973956366721379612508047640E2L, 5.577066396050932776683469951773643880634E2L, -4.406335508848496713572223098693575485978E2L, 5.202893466490242733570232680736966655434E1L, -1.931311847665757913322495948705563937159E1L, -9.364318268748287664267341457164918090611E-2L, -3.306390351286352764891355375882586201069E0L, 7.573806045289044647727613003096916516475E-1L, -9.611744011489092894027478899545635991213E-2L};#define NRDr20 7static const long double RDr20[NRDr20 + 1] ={ 3.032829629520142564106649167182428189014E3L, 1.659648470721967719961167083684972196891E3L, 1.703545128657284619402511356932569292535E3L, 6.393465677731598872500200253155257708763E2L, 3.489131397281030947405287112726059221934E2L, 8.848641738570783406484348434387611713070E1L,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -