e_j0l.cpp

来自「这是整套横扫千军3D版游戏的源码」· C++ 代码 · 共 648 行 · 第 1/2 页

CPP
648
字号
/* See the import.pl script for potential modifications */
/*
 * ====================================================
 * 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.
 * ====================================================
 */

/* Long Double expansions 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.1l 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 */

/* __ieee754_j0(x), __ieee754_y0(x)
 * Bessel function of the first and second kinds of order zero.
 * Method -- j0(x):
 *	1. For tiny x, we use j0(x) = 1 - x^2/4 + x^4/64 - ...
 *	2. Reduce x to |x| since j0(x)=j0(-x),  and
 *	   for x in (0,2)
 *		j0(x) = 1 - z/4 + z^2*R0/S0,  where z = x*x;
 *	   for x in (2,inf)
 *		j0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)-q0(x)*sin(x0))
 *	   where x0 = x-pi/4. It is better to compute sin(x0),cos(x0)
 *	   as follow:
 *		cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4)
 *			= 1/sqrt(2) * (cos(x) + sin(x))
 *		sin(x0) = sin(x)cos(pi/4)-cos(x)sin(pi/4)
 *			= 1/sqrt(2) * (sin(x) - cos(x))
 *	   (To avoid cancellation, use
 *		sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
 *	    to compute the worse one.)
 *
 *	3 Special cases
 *		j0(nan)= nan
 *		j0(0) = 1
 *		j0(inf) = 0
 *
 * Method -- y0(x):
 *	1. For x<2.
 *	   Since
 *		y0(x) = 2/pi*(j0(x)*(ln(x/2)+Euler) + x^2/4 - ...)
 *	   therefore y0(x)-2/pi*j0(x)*ln(x) is an even function.
 *	   We use the following function to approximate y0,
 *		y0(x) = U(z)/V(z) + (2/pi)*(j0(x)*ln(x)), z= x^2
 *
 *	   Note: For tiny x, U/V = u0 and j0(x)~1, hence
 *		y0(tiny) = u0 + (2/pi)*ln(tiny), (choose tiny<2**-27)
 *	2. For x>=2.
 *		y0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)+q0(x)*sin(x0))
 *	   where x0 = x-pi/4. It is better to compute sin(x0),cos(x0)
 *	   by the method mentioned above.
 *	3. Special cases: y0(0)=-inf, y0(x<0)=NaN, y0(inf)=0.
 */

#include "math.h"
#include "math_private.h"

namespace streflop_libm {
#ifdef __STDC__
static Extended pzero (Extended), qzero (Extended);
#else
static Extended pzero (), qzero ();
#endif

#ifdef __STDC__
static const Extended
#else
static Extended
#endif
  huge = 1e4930L,
  one = 1.0l,
  invsqrtpi = 5.6418958354775628694807945156077258584405e-1l,
  tpi = 6.3661977236758134307553505349005744813784e-1l,

  /* J0(x) = 1 - x^2 / 4 + x^4 R0(x^2) / S0(x^2)
     0 <= x <= 2
     peak relative error 1.41e-22l */
  R[5] = {
  4.287176872744686992880841716723478740566E7l,
  -6.652058897474241627570911531740907185772E5l,
  7.011848381719789863458364584613651091175E3l,
  -3.168040850193372408702135490809516253693E1l,
  6.030778552661102450545394348845599300939E-2l,
},

 S[4] = {
   2.743793198556599677955266341699130654342E9l,
   3.364330079384816249840086842058954076201E7l,
   1.924119649412510777584684927494642526573E5l,
   6.239282256012734914211715620088714856494E2l,
   /*   1.000000000000000000000000000000000000000E0l,*/
};

#ifdef __STDC__
static const Extended zero = 0.0l;
#else
static Extended zero = 0.0l;
#endif

#ifdef __STDC__
Extended
__ieee754_j0l (Extended x)
#else
Extended
__ieee754_j0l (x)
     Extended x;
#endif
{
  Extended z, s, c, ss, cc, r, u, v;
  int32_t ix;
  u_int32_t se, i0, i1;

  GET_LDOUBLE_WORDS (se, i0, i1, x);
  ix = se & 0x7fff;
  if (ix >= 0x7fff)
    return one / (x * x);
  x = fabsl (x);
  if (ix >= 0x4000)		/* |x| >= 2.0l */
    {
      __sincosl (x, &s, &c);
      ss = s - c;
      cc = s + c;
      if (ix < 0x7ffe)
	{			/* make sure x+x not overflow */
	  z = -__cosl (x + x);
	  if ((s * c) < zero)
	    cc = z / ss;
	  else
	    ss = z / cc;
	}
      /*
       * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x)
       * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x)
       */
      if (ix > 0x4080)	/* 2^129 */
	z = (invsqrtpi * cc) / __ieee754_sqrtl (x);
      else
	{
	  u = pzero (x);
	  v = qzero (x);
	  z = invsqrtpi * (u * cc - v * ss) / __ieee754_sqrtl (x);
	}
      return z;
    }
  if (ix < 0x3fef) /* |x| < 2**-16 */
    {
      if (huge + x > one)
	{			/* raise inexact if x != 0 */
	  if (ix < 0x3fde) /* |x| < 2^-33 */
	    return one;
	  else
	    return one - 0.25l * x * x;
	}
    }
  z = x * x;
  r = z * (R[0] + z * (R[1] + z * (R[2] + z * (R[3] + z * R[4]))));
  s = S[0] + z * (S[1] + z * (S[2] + z * (S[3] + z)));
  if (ix < 0x3fff)
    {				/* |x| < 1.00l */
      return (one - 0.25l * z + z * (r / s));
    }
  else
    {
      u = 0.5l * x;
      return ((one + u) * (one - u) + z * (r / s));
    }
}


/* y0(x) = 2/pi ln(x) J0(x) + U(x^2)/V(x^2)
   0 < x <= 2
   peak relative error 1.7e-21l */
#ifdef __STDC__
static const Extended
#else
static Extended
#endif
U[6] = {
  -1.054912306975785573710813351985351350861E10l,
  2.520192609749295139432773849576523636127E10l,
  -1.856426071075602001239955451329519093395E9l,
  4.079209129698891442683267466276785956784E7l,
  -3.440684087134286610316661166492641011539E5l,
  1.005524356159130626192144663414848383774E3l,
};
#ifdef __STDC__
static const Extended
#else
static Extended
#endif
V[5] = {
  1.429337283720789610137291929228082613676E11l,
  2.492593075325119157558811370165695013002E9l,
  2.186077620785925464237324417623665138376E7l,
  1.238407896366385175196515057064384929222E5l,
  4.693924035211032457494368947123233101664E2l,
  /*  1.000000000000000000000000000000000000000E0l */
};

#ifdef __STDC__
Extended
__ieee754_y0l (Extended x)
#else
Extended
__ieee754_y0l (x)
     Extended x;
#endif
{
  Extended z, s, c, ss, cc, u, v;
  int32_t ix;
  u_int32_t se, i0, i1;

  GET_LDOUBLE_WORDS (se, i0, i1, x);
  ix = se & 0x7fff;
  /* Y0(NaN) is NaN, y0(-inf) is Nan, y0(inf) is 0  */
  if (se & 0x8000)
    return zero / (zero * x);
  if (ix >= 0x7fff)
    return one / (x + x * x);
  if ((i0 | i1) == 0)
    return -HUGE_VALL + x;  /* -inf and overflow exception.  */
  if (ix >= 0x4000)
    {				/* |x| >= 2.0l */

      /* y0(x) = sqrt(2/(pi*x))*(p0(x)*sin(x0)+q0(x)*cos(x0))
       * where x0 = x-pi/4
       *      Better formula:
       *              cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4)
       *                      =  1/sqrt(2) * (sin(x) + cos(x))
       *              sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
       *                      =  1/sqrt(2) * (sin(x) - cos(x))
       * To avoid cancellation, use
       *              sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
       * to compute the worse one.
       */
      __sincosl (x, &s, &c);
      ss = s - c;
      cc = s + c;
      /*
       * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x)
       * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x)
       */
      if (ix < 0x7ffe)
	{			/* make sure x+x not overflow */
	  z = -__cosl (x + x);
	  if ((s * c) < zero)
	    cc = z / ss;
	  else
	    ss = z / cc;
	}
      if (ix > 0x4080)	/* 1e39 */
	z = (invsqrtpi * ss) / __ieee754_sqrtl (x);
      else
	{
	  u = pzero (x);
	  v = qzero (x);
	  z = invsqrtpi * (u * ss + v * cc) / __ieee754_sqrtl (x);
	}
      return z;
    }
  if (ix <= 0x3fde) /* x < 2^-33 */
    {
      z = -7.380429510868722527629822444004602747322E-2l
	+ tpi * __ieee754_logl (x);
      return z;
    }
  z = x * x;
  u = U[0] + z * (U[1] + z * (U[2] + z * (U[3] + z * (U[4] + z * U[5]))));
  v = V[0] + z * (V[1] + z * (V[2] + z * (V[3] + z * (V[4] + z))));
  return (u / v + tpi * (__ieee754_j0l (x) * __ieee754_logl (x)));
}

/* The asymptotic expansions of pzero is
 *	1 - 9/128 s^2 + 11025/98304 s^4 - ...,	where s = 1/x.
 * For x >= 2, We approximate pzero by
 *	pzero(x) = 1 + s^2 R(s^2) / S(s^2)
 */
#ifdef __STDC__
static const Extended pR8[7] = {
#else
static Extended pR8[7] = {
#endif
  /* 8 <= x <= inf
     Peak relative error 4.62l */
  -4.094398895124198016684337960227780260127E-9l,
  -8.929643669432412640061946338524096893089E-7l,
  -6.281267456906136703868258380673108109256E-5l,
  -1.736902783620362966354814353559382399665E-3l,
  -1.831506216290984960532230842266070146847E-2l,
  -5.827178869301452892963280214772398135283E-2l,
  -2.087563267939546435460286895807046616992E-2l,
};
#ifdef __STDC__
static const Extended pS8[6] = {
#else
static Extended pS8[6] = {
#endif
  5.823145095287749230197031108839653988393E-8l,
  1.279281986035060320477759999428992730280E-5l,
  9.132668954726626677174825517150228961304E-4l,
  2.606019379433060585351880541545146252534E-2l,
  2.956262215119520464228467583516287175244E-1l,
  1.149498145388256448535563278632697465675E0l,
  /* 1.000000000000000000000000000000000000000E0l, */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?