float y0f(float x) { float z,s,c,ss,cc,u,v; int32_t hx,ix; GET_FLOAT_WORD(hx, x); ix = 0x7fffffff & hx; /* Y0(NaN) is NaN, y0(-inf) is Nan, y0(inf) is 0 */ if (ix >= 0x7f800000) return 1.0f/(x+x*x); if (ix == 0) return -1.0f/0.0f; if (hx < 0) return 0.0f/0.0f; if (ix >= 0x40000000) { /* |x| >= 2.0 */ /* 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. */ s = sinf(x); c = cosf(x); 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 < 0x7f000000) { /* make sure x+x not overflow */ z = -cosf(x+x); if (s*c < 0.0f) cc = z/ss; else ss = z/cc; } if (ix > 0x80000000) z = (invsqrtpi*ss)/sqrtf(x); else { u = pzerof(x); v = qzerof(x); z = invsqrtpi*(u*ss+v*cc)/sqrtf(x); } return z; } if (ix <= 0x32000000) { /* x < 2**-27 */ return u00 + tpi*logf(x); } z = x*x; u = u00+z*(u01+z*(u02+z*(u03+z*(u04+z*(u05+z*u06))))); v = 1.0f+z*(v01+z*(v02+z*(v03+z*v04))); return u/v + tpi*(j0f(x)*logf(x)); }
int main() { float y; int i; for (i = 0; i< 20; i++) { y = j0f(z[i]); printf("%.9e\n", y); } exit(0); }
static void RDFtoRP(const Curve &rdf, int npoints, Curve *rp) { const float wstep = 1.f / sqrtf(npoints); Curve tmp(rdf); for (int i = 0; i < rp->size(); ++i) { const float u0 = rp->ToX(i); const float u = TWOPI * u0; const float wndsize = rdf.x1 * std::min(0.5f, std::max(0.2f, 4.f * u0 * wstep)); for (int j = 0; j < tmp.size(); ++j) { float x = rdf.ToX(j); float wnd = BlackmanWindow(x, wndsize); tmp[j] = (rdf[j] - 1) * j0f(u*x) * x * wnd; } (*rp)[i] = fabsf(1.f + TWOPI * Integrate(tmp) * npoints); } }
int main(void) { #pragma STDC FENV_ACCESS ON float y; float d; int e, i, err = 0; struct f_f *p; for (i = 0; i < sizeof t/sizeof *t; i++) { p = t + i; if (p->r < 0) continue; fesetround(p->r); feclearexcept(FE_ALL_EXCEPT); y = j0f(p->x); e = fetestexcept(INEXACT|INVALID|DIVBYZERO|UNDERFLOW|OVERFLOW); if (!checkexcept(e, p->e, p->r)) { printf("%s:%d: bad fp exception: %s j0f(%a)=%a, want %s", p->file, p->line, rstr(p->r), p->x, p->y, estr(p->e)); printf(" got %s\n", estr(e)); err++; } d = ulperrf(y, p->y, p->dy); if (!checkulp(d, p->r)) { // printf("%s:%d: %s j0f(%a) want %a got %a ulperr %.3f = %a + %a\n", // p->file, p->line, rstr(p->r), p->x, p->y, y, d, d-p->dy, p->dy); err++; // TODO: avoid spamming the output printf(__FILE__ ": known to be broken near zeros\n"); break; } } return !!err; }
void domathf (void) { #ifndef NO_FLOAT float f1; float f2; int i1; f1 = acosf(0.0); fprintf( stdout, "acosf : %f\n", f1); f1 = acoshf(0.0); fprintf( stdout, "acoshf : %f\n", f1); f1 = asinf(1.0); fprintf( stdout, "asinf : %f\n", f1); f1 = asinhf(1.0); fprintf( stdout, "asinhf : %f\n", f1); f1 = atanf(M_PI_4); fprintf( stdout, "atanf : %f\n", f1); f1 = atan2f(2.3, 2.3); fprintf( stdout, "atan2f : %f\n", f1); f1 = atanhf(1.0); fprintf( stdout, "atanhf : %f\n", f1); f1 = cbrtf(27.0); fprintf( stdout, "cbrtf : %f\n", f1); f1 = ceilf(3.5); fprintf( stdout, "ceilf : %f\n", f1); f1 = copysignf(3.5, -2.5); fprintf( stdout, "copysignf : %f\n", f1); f1 = cosf(M_PI_2); fprintf( stdout, "cosf : %f\n", f1); f1 = coshf(M_PI_2); fprintf( stdout, "coshf : %f\n", f1); f1 = erff(42.0); fprintf( stdout, "erff : %f\n", f1); f1 = erfcf(42.0); fprintf( stdout, "erfcf : %f\n", f1); f1 = expf(0.42); fprintf( stdout, "expf : %f\n", f1); f1 = exp2f(0.42); fprintf( stdout, "exp2f : %f\n", f1); f1 = expm1f(0.00042); fprintf( stdout, "expm1f : %f\n", f1); f1 = fabsf(-1.123); fprintf( stdout, "fabsf : %f\n", f1); f1 = fdimf(1.123, 2.123); fprintf( stdout, "fdimf : %f\n", f1); f1 = floorf(0.5); fprintf( stdout, "floorf : %f\n", f1); f1 = floorf(-0.5); fprintf( stdout, "floorf : %f\n", f1); f1 = fmaf(2.1, 2.2, 3.01); fprintf( stdout, "fmaf : %f\n", f1); f1 = fmaxf(-0.42, 0.42); fprintf( stdout, "fmaxf : %f\n", f1); f1 = fminf(-0.42, 0.42); fprintf( stdout, "fminf : %f\n", f1); f1 = fmodf(42.0, 3.0); fprintf( stdout, "fmodf : %f\n", f1); /* no type-specific variant */ i1 = fpclassify(1.0); fprintf( stdout, "fpclassify : %d\n", i1); f1 = frexpf(42.0, &i1); fprintf( stdout, "frexpf : %f\n", f1); f1 = hypotf(42.0, 42.0); fprintf( stdout, "hypotf : %f\n", f1); i1 = ilogbf(42.0); fprintf( stdout, "ilogbf : %d\n", i1); /* no type-specific variant */ i1 = isfinite(3.0); fprintf( stdout, "isfinite : %d\n", i1); /* no type-specific variant */ i1 = isgreater(3.0, 3.1); fprintf( stdout, "isgreater : %d\n", i1); /* no type-specific variant */ i1 = isgreaterequal(3.0, 3.1); fprintf( stdout, "isgreaterequal : %d\n", i1); /* no type-specific variant */ i1 = isinf(3.0); fprintf( stdout, "isinf : %d\n", i1); /* no type-specific variant */ i1 = isless(3.0, 3.1); fprintf( stdout, "isless : %d\n", i1); /* no type-specific variant */ i1 = islessequal(3.0, 3.1); fprintf( stdout, "islessequal : %d\n", i1); /* no type-specific variant */ i1 = islessgreater(3.0, 3.1); fprintf( stdout, "islessgreater : %d\n", i1); /* no type-specific variant */ i1 = isnan(0.0); fprintf( stdout, "isnan : %d\n", i1); /* no type-specific variant */ i1 = isnormal(3.0); fprintf( stdout, "isnormal : %d\n", i1); /* no type-specific variant */ f1 = isunordered(1.0, 2.0); fprintf( stdout, "isunordered : %d\n", i1); f1 = j0f(1.2); fprintf( stdout, "j0f : %f\n", f1); f1 = j1f(1.2); fprintf( stdout, "j1f : %f\n", f1); f1 = jnf(2,1.2); fprintf( stdout, "jnf : %f\n", f1); f1 = ldexpf(1.2,3); fprintf( stdout, "ldexpf : %f\n", f1); f1 = lgammaf(42.0); fprintf( stdout, "lgammaf : %f\n", f1); f1 = llrintf(-0.5); fprintf( stdout, "llrintf : %f\n", f1); f1 = llrintf(0.5); fprintf( stdout, "llrintf : %f\n", f1); f1 = llroundf(-0.5); fprintf( stdout, "lroundf : %f\n", f1); f1 = llroundf(0.5); fprintf( stdout, "lroundf : %f\n", f1); f1 = logf(42.0); fprintf( stdout, "logf : %f\n", f1); f1 = log10f(42.0); fprintf( stdout, "log10f : %f\n", f1); f1 = log1pf(42.0); fprintf( stdout, "log1pf : %f\n", f1); f1 = log2f(42.0); fprintf( stdout, "log2f : %f\n", f1); f1 = logbf(42.0); fprintf( stdout, "logbf : %f\n", f1); f1 = lrintf(-0.5); fprintf( stdout, "lrintf : %f\n", f1); f1 = lrintf(0.5); fprintf( stdout, "lrintf : %f\n", f1); f1 = lroundf(-0.5); fprintf( stdout, "lroundf : %f\n", f1); f1 = lroundf(0.5); fprintf( stdout, "lroundf : %f\n", f1); f1 = modff(42.0,&f2); fprintf( stdout, "lmodff : %f\n", f1); f1 = nanf(""); fprintf( stdout, "nanf : %f\n", f1); f1 = nearbyintf(1.5); fprintf( stdout, "nearbyintf : %f\n", f1); f1 = nextafterf(1.5,2.0); fprintf( stdout, "nextafterf : %f\n", f1); f1 = powf(3.01, 2.0); fprintf( stdout, "powf : %f\n", f1); f1 = remainderf(3.01,2.0); fprintf( stdout, "remainderf : %f\n", f1); f1 = remquof(29.0,3.0,&i1); fprintf( stdout, "remquof : %f\n", f1); f1 = rintf(0.5); fprintf( stdout, "rintf : %f\n", f1); f1 = rintf(-0.5); fprintf( stdout, "rintf : %f\n", f1); f1 = roundf(0.5); fprintf( stdout, "roundf : %f\n", f1); f1 = roundf(-0.5); fprintf( stdout, "roundf : %f\n", f1); f1 = scalblnf(1.2,3); fprintf( stdout, "scalblnf : %f\n", f1); f1 = scalbnf(1.2,3); fprintf( stdout, "scalbnf : %f\n", f1); /* no type-specific variant */ i1 = signbit(1.0); fprintf( stdout, "signbit : %i\n", i1); f1 = sinf(M_PI_4); fprintf( stdout, "sinf : %f\n", f1); f1 = sinhf(M_PI_4); fprintf( stdout, "sinhf : %f\n", f1); f1 = sqrtf(9.0); fprintf( stdout, "sqrtf : %f\n", f1); f1 = tanf(M_PI_4); fprintf( stdout, "tanf : %f\n", f1); f1 = tanhf(M_PI_4); fprintf( stdout, "tanhf : %f\n", f1); f1 = tgammaf(2.1); fprintf( stdout, "tgammaf : %f\n", f1); f1 = truncf(3.5); fprintf( stdout, "truncf : %f\n", f1); f1 = y0f(1.2); fprintf( stdout, "y0f : %f\n", f1); f1 = y1f(1.2); fprintf( stdout, "y1f : %f\n", f1); f1 = ynf(3,1.2); fprintf( stdout, "ynf : %f\n", f1); #endif }
int main(int argc, char *argv[]) { float x = 0.0; if (argv) x = j0f((float) argc); return 0; }
TEST(math, j0f) { ASSERT_FLOAT_EQ(1.0f, j0f(0.0f)); ASSERT_FLOAT_EQ(0.76519769f, j0f(1.0f)); }
float jnf(int n, float x) { uint32_t ix; int nm1, sign, i; float a, b, temp; GET_FLOAT_WORD(ix, x); sign = ix >> 31; ix &= 0x7fffffff; if (ix > 0x7f800000) /* nan */ return x; /* J(-n,x) = J(n,-x), use |n|-1 to avoid overflow in -n */ if (n == 0) return j0f(x); if (n < 0) { nm1 = -(n + 1); x = -x; sign ^= 1; } else nm1 = n - 1; if (nm1 == 0) return j1f(x); sign &= n; /* even n: 0, odd n: signbit(x) */ x = fabsf(x); if (ix == 0 || ix == 0x7f800000) /* if x is 0 or inf */ b = 0.0f; else if (nm1 < x) { /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ a = j0f(x); b = j1f(x); for (i = 0; i < nm1;) { i++; temp = b; b = b * (2.0f * i / x) - a; a = temp; } } else { if (ix < 0x35800000) { /* x < 2**-20 */ /* x is tiny, return the first Taylor expansion of J(n,x) * J(n,x) = 1/n!*(x/2)^n - ... */ if (nm1 > 8) /* underflow */ nm1 = 8; temp = 0.5f * x; b = temp; a = 1.0f; for (i = 2; i <= nm1 + 1; i++) { a *= (float)i; /* a = n! */ b *= temp; /* b = (x/2)^n */ } b = b / a; } else { /* use backward recurrence */ /* x x^2 x^2 * J(n,x)/J(n-1,x) = ---- ------ ------ ..... * 2n - 2(n+1) - 2(n+2) * * 1 1 1 * (for large x) = ---- ------ ------ ..... * 2n 2(n+1) 2(n+2) * -- - ------ - ------ - * x x x * * Let w = 2n/x and h=2/x, then the above quotient * is equal to the continued fraction: * 1 * = ----------------------- * 1 * w - ----------------- * 1 * w+h - --------- * w+2h - ... * * To determine how many terms needed, let * Q(0) = w, Q(1) = w(w+h) - 1, * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), * When Q(k) > 1e4 good for single * When Q(k) > 1e9 good for double * When Q(k) > 1e17 good for quadruple */ /* determine k */ float t, q0, q1, w, h, z, tmp, nf; int k; nf = nm1 + 1.0f; w = 2 * nf / x; h = 2 / x; z = w + h; q0 = w; q1 = w * z - 1.0f; k = 1; while (q1 < 1.0e4f) { k += 1; z += h; tmp = z * q1 - q0; q0 = q1; q1 = tmp; } for (t = 0.0f, i = k; i >= 0; i--) t = 1.0f / (2 * (i + nf) / x - t); a = t; b = 1.0f; /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) * Hence, if n*(log(2n/x)) > ... * single 8.8722839355e+01 * double 7.09782712893383973096e+02 * long double 1.1356523406294143949491931077970765006170e+04 * then recurrent value may overflow and the result is * likely underflow to zero */ tmp = nf * logf(fabsf(w)); if (tmp < 88.721679688f) { for (i = nm1; i > 0; i--) { temp = b; b = 2.0f * i * b / x - a; a = temp; } } else { for (i = nm1; i > 0; i--) { temp = b; b = 2.0f * i * b / x - a; a = temp; /* scale b to avoid spurious overflow */ if (b > 0x1p60f) { a /= b; t /= b; b = 1.0f; } } } z = j0f(x); w = j1f(x); if (fabsf(z) >= fabsf(w)) b = t * z / b; else b = t * w / a; } } return sign ? -b : b; }
__global__ void FloatMathPrecise() { int iX; float fX, fY; acosf(1.0f); acoshf(1.0f); asinf(0.0f); asinhf(0.0f); atan2f(0.0f, 1.0f); atanf(0.0f); atanhf(0.0f); cbrtf(0.0f); fX = ceilf(0.0f); fX = copysignf(1.0f, -2.0f); cosf(0.0f); coshf(0.0f); cospif(0.0f); cyl_bessel_i0f(0.0f); cyl_bessel_i1f(0.0f); erfcf(0.0f); erfcinvf(2.0f); erfcxf(0.0f); erff(0.0f); erfinvf(1.0f); exp10f(0.0f); exp2f(0.0f); expf(0.0f); expm1f(0.0f); fX = fabsf(1.0f); fdimf(1.0f, 0.0f); fdividef(0.0f, 1.0f); fX = floorf(0.0f); fmaf(1.0f, 2.0f, 3.0f); fX = fmaxf(0.0f, 0.0f); fX = fminf(0.0f, 0.0f); fmodf(0.0f, 1.0f); frexpf(0.0f, &iX); hypotf(1.0f, 0.0f); ilogbf(1.0f); isfinite(0.0f); fX = isinf(0.0f); fX = isnan(0.0f); j0f(0.0f); j1f(0.0f); jnf(-1.0f, 1.0f); ldexpf(0.0f, 0); lgammaf(1.0f); llrintf(0.0f); llroundf(0.0f); log10f(1.0f); log1pf(-1.0f); log2f(1.0f); logbf(1.0f); logf(1.0f); lrintf(0.0f); lroundf(0.0f); modff(0.0f, &fX); fX = nanf("1"); fX = nearbyintf(0.0f); nextafterf(0.0f, 0.0f); norm3df(1.0f, 0.0f, 0.0f); norm4df(1.0f, 0.0f, 0.0f, 0.0f); normcdff(0.0f); normcdfinvf(1.0f); fX = 1.0f; normf(1, &fX); powf(1.0f, 0.0f); rcbrtf(1.0f); remainderf(2.0f, 1.0f); remquof(1.0f, 2.0f, &iX); rhypotf(0.0f, 1.0f); fY = rintf(1.0f); rnorm3df(0.0f, 0.0f, 1.0f); rnorm4df(0.0f, 0.0f, 0.0f, 1.0f); fX = 1.0f; rnormf(1, &fX); fY = roundf(0.0f); rsqrtf(1.0f); scalblnf(0.0f, 1); scalbnf(0.0f, 1); signbit(1.0f); sincosf(0.0f, &fX, &fY); sincospif(0.0f, &fX, &fY); sinf(0.0f); sinhf(0.0f); sinpif(0.0f); sqrtf(0.0f); tanf(0.0f); tanhf(0.0f); tgammaf(2.0f); fY = truncf(0.0f); y0f(1.0f); y1f(1.0f); ynf(1, 1.0f); }