e_j1l.cpp

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

CPP
655
字号
/* 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_j1(x), __ieee754_y1(x)
 * Bessel function of the first and second kinds of order zero.
 * Method -- j1(x):
 *	1. For tiny x, we use j1(x) = x/2 - x^3/16 + x^5/384 - ...
 *	2. Reduce x to |x| since j1(x)=-j1(-x),  and
 *	   for x in (0,2)
 *		j1(x) = x/2 + x*z*R0/S0,  where z = x*x;
 *	   for x in (2,inf)
 * 		j1(x) = sqrt(2/(pi*x))*(p1(x)*cos(x1)-q1(x)*sin(x1))
 * 		y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
 * 	   where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
 *	   as follow:
 *		cos(x1) =  cos(x)cos(3pi/4)+sin(x)sin(3pi/4)
 *			=  1/sqrt(2) * (sin(x) - cos(x))
 *		sin(x1) =  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.)
 *
 *	3 Special cases
 *		j1(nan)= nan
 *		j1(0) = 0
 *		j1(inf) = 0
 *
 * Method -- y1(x):
 *	1. screen out x<=0 cases: y1(0)=-inf, y1(x<0)=NaN
 *	2. For x<2.
 *	   Since
 *		y1(x) = 2/pi*(j1(x)*(ln(x/2)+Euler)-1/x-x/2+5/64*x^3-...)
 *	   therefore y1(x)-2/pi*j1(x)*ln(x)-1/x is an odd function.
 *	   We use the following function to approximate y1,
 *		y1(x) = x*U(z)/V(z) + (2/pi)*(j1(x)*ln(x)-1/x), z= x^2
 *	   Note: For tiny x, 1/x dominate y1 and hence
 *		y1(tiny) = -2/pi/tiny
 *	3. For x>=2.
 * 		y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
 * 	   where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
 *	   by method mentioned above.
 */

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

namespace streflop_libm {
#ifdef __STDC__
static Extended pone (Extended), qone (Extended);
#else
static Extended pone (), qone ();
#endif

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

  /* J1(x) = .5 x + x x^2 R(x^2) / S(x^2)
     0 <= x <= 2
     Peak relative error 4.5e-21l */
R[5] = {
    -9.647406112428107954753770469290757756814E7l,
    2.686288565865230690166454005558203955564E6l,
    -3.689682683905671185891885948692283776081E4l,
    2.195031194229176602851429567792676658146E2l,
    -5.124499848728030297902028238597308971319E-1l,
},

  S[4] =
{
  1.543584977988497274437410333029029035089E9l,
  2.133542369567701244002565983150952549520E7l,
  1.394077011298227346483732156167414670520E5l,
  5.252401789085732428842871556112108446506E2l,
  /*  1.000000000000000000000000000000000000000E0l, */
};

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


#ifdef __STDC__
Extended
__ieee754_j1l (Extended x)
#else
Extended
__ieee754_j1l (x)
     Extended x;
#endif
{
  Extended z, c, r, s, ss, cc, u, v, y;
  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;
  y = fabsl (x);
  if (ix >= 0x4000)
    {				/* |x| >= 2.0l */
      __sincosl (y, &s, &c);
      ss = -s - c;
      cc = s - c;
      if (ix < 0x7ffe)
	{			/* make sure y+y not overflow */
	  z = __cosl (y + y);
	  if ((s * c) > zero)
	    cc = z / ss;
	  else
	    ss = z / cc;
	}
      /*
       * j1(x) = 1/sqrt(pi) * (P(1,x)*cc - Q(1,x)*ss) / sqrt(x)
       * y1(x) = 1/sqrt(pi) * (P(1,x)*ss + Q(1,x)*cc) / sqrt(x)
       */
      if (ix > 0x4080)
	z = (invsqrtpi * cc) / __ieee754_sqrtl (y);
      else
	{
	  u = pone (y);
	  v = qone (y);
	  z = invsqrtpi * (u * cc - v * ss) / __ieee754_sqrtl (y);
	}
      if (se & 0x8000)
	return -z;
      else
	return z;
    }
  if (ix < 0x3fde) /* |x| < 2^-33 */
    {
      if (huge + x > one)
	return 0.5l * x;		/* inexact if x!=0 necessary */
    }
  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)));
  r *= x;
  return (x * 0.5l + r / s);
}


/* Y1(x) = 2/pi * (log(x) * j1(x) - 1/x) + x R(x^2)
   0 <= x <= 2
   Peak relative error 2.3e-23l */
#ifdef __STDC__
static const Extended U0[6] = {
#else
static Extended U0[6] = {
#endif
  -5.908077186259914699178903164682444848615E10l,
  1.546219327181478013495975514375773435962E10l,
  -6.438303331169223128870035584107053228235E8l,
  9.708540045657182600665968063824819371216E6l,
  -6.138043997084355564619377183564196265471E4l,
  1.418503228220927321096904291501161800215E2l,
};
#ifdef __STDC__
static const Extended V0[5] = {
#else
static Extended V0[5] = {
#endif
  3.013447341682896694781964795373783679861E11l,
  4.669546565705981649470005402243136124523E9l,
  3.595056091631351184676890179233695857260E7l,
  1.761554028569108722903944659933744317994E5l,
  5.668480419646516568875555062047234534863E2l,
  /*  1.000000000000000000000000000000000000000E0l, */
};


#ifdef __STDC__
Extended
__ieee754_y1l (Extended x)
#else
Extended
__ieee754_y1l (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;
  /* if Y1(NaN) is NaN, Y1(-inf) is NaN, Y1(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 */
      __sincosl (x, &s, &c);
      ss = -s - c;
      cc = s - c;
      if (ix < 0x7fe00000)
	{			/* make sure x+x not overflow */
	  z = __cosl (x + x);
	  if ((s * c) > zero)
	    cc = z / ss;
	  else
	    ss = z / cc;
	}
      /* y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x0)+q1(x)*cos(x0))
       * where x0 = x-3pi/4
       *      Better formula:
       *              cos(x0) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4)
       *                      =  1/sqrt(2) * (sin(x) - cos(x))
       *              sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
       *                      = -1/sqrt(2) * (cos(x) + sin(x))
       * To avoid cancellation, use
       *              sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
       * to compute the worse one.
       */
      if (ix > 0x4080)
	z = (invsqrtpi * ss) / __ieee754_sqrtl (x);
      else
	{
	  u = pone (x);
	  v = qone (x);
	  z = invsqrtpi * (u * ss + v * cc) / __ieee754_sqrtl (x);
	}
      return z;
    }
  if (ix <= 0x3fbe)
    {				/* x < 2**-65 */
      return (-tpi / x);
    }
  z = x * x;
 u = U0[0] + z * (U0[1] + z * (U0[2] + z * (U0[3] + z * (U0[4] + z * U0[5]))));
 v = V0[0] + z * (V0[1] + z * (V0[2] + z * (V0[3] + z * (V0[4] + z))));
  return (x * (u / v) +
	  tpi * (__ieee754_j1l (x) * __ieee754_logl (x) - one / x));
}


/* For x >= 8, the asymptotic expansions of pone is
 *	1 + 15/128 s^2 - 4725/2^15 s^4 - ...,	where s = 1/x.
 * We approximate pone by
 * 	pone(x) = 1 + (R/S)
 */

/* J1(x) cosX + Y1(x) sinX  =  sqrt( 2/(pi x)) P1(x)
   P1(x) = 1 + z^2 R(z^2), z=1/x
   8 <= x <= inf  (0 <= z <= 0.125l)
   Peak relative error 5.2e-22l  */

#ifdef __STDC__
static const Extended pr8[7] = {
#else
static Extended pr8[7] = {
#endif
  8.402048819032978959298664869941375143163E-9l,
  1.813743245316438056192649247507255996036E-6l,
  1.260704554112906152344932388588243836276E-4l,
  3.439294839869103014614229832700986965110E-3l,
  3.576910849712074184504430254290179501209E-2l,
  1.131111483254318243139953003461511308672E-1l,
  4.480715825681029711521286449131671880953E-2l,
};
#ifdef __STDC__
static const Extended ps8[6] = {
#else
static Extended ps8[6] = {
#endif
  7.169748325574809484893888315707824924354E-8l,
  1.556549720596672576431813934184403614817E-5l,
  1.094540125521337139209062035774174565882E-3l,
  3.060978962596642798560894375281428805840E-2l,
  3.374146536087205506032643098619414507024E-1l,
  1.253830208588979001991901126393231302559E0l,
  /* 1.000000000000000000000000000000000000000E0l, */
};

/* J1(x) cosX + Y1(x) sinX  =  sqrt( 2/(pi x)) P1(x)
   P1(x) = 1 + z^2 R(z^2), z=1/x
   4.54541015625l <= x <= 8
   Peak relative error 7.7e-22l  */
#ifdef __STDC__
static const Extended pr5[7] = {
#else
static Extended pr5[7] = {
#endif
  4.318486887948814529950980396300969247900E-7l,
  4.715341880798817230333360497524173929315E-5l,

⌨️ 快捷键说明

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