Index: sasmodels/models/lib/wrc_cyl.c
===================================================================
--- sasmodels/models/lib/wrc_cyl.c	(revision 3a220e6f566d477c1f6f540eab46e4ecb26463c5)
+++ sasmodels/models/lib/wrc_cyl.c	(revision 237c9cffdaac344dd58354114e28b2a547625442)
@@ -7,9 +7,8 @@
 {
     const double x = L/b;
-    // Use Horner's method to evaluate:
-    //     pow(1.0+square(x/3.12)+cube(x/8.67), 0.176/3.0)
-    // Too many digits on the coefficients, but necessary for consistency
+    // Use Horner's method to evaluate Pedersen eq 15:
+    //     alpha^2 = [1.0 + (x/3.12)^2 + (x/8.67)^3] ^ (0.176/3)
     const double alphasq =
-        pow(1.0 + square(x)*(1.534414548417740e-03*x + 1.027284681130835e-01),
+        pow(1.0 + x*x*(1.027284681130835e-01 + 1.534414548417740e-03*x),
             5.866666666666667e-02);
     return alphasq*L*b/6.0;
@@ -19,16 +18,47 @@
 Rgsquareshort(double L, double b)
 {
-    const double r = b/L;
+    const double r = b/L;  // = 1/n_b in Pedersen ref.
     return Rgsquare(L, b) * (1.0 + r*(-1.5 + r*(1.5 + r*0.75*expm1(-2.0/r))));
 }
 
 static double
+w_WR(double x)
+{
+    // Pedersen eq. 16:
+    //    w = 1 - [1 + tanh((x-C4)/C5)]/2
+    const double C4 = 1.523;
+    const double C5 = 0.1477;
+    return 0.5 + 0.5*tanh((x - C4)/C5);
+}
+
+static double
+Sdebye(double qsq)
+{
+#if FLOAT_SIZE>4
+#  define DEBYE_CUTOFF 0.25  // 1e-15 error
+#else
+#  define DEBYE_CUTOFF 1.0  // 4e-7 error
+#endif
+
+    if (qsq < DEBYE_CUTOFF) {
+        const double x = qsq;
+        // mathematica: PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]
+        const double A1=1./15., A2=1./60, A3=0., A4=1./75600.;
+        const double B1=2./5., B2=1./15., B3=1./180., B4=1./5040.;
+        return ((((A4*x + A3)*x + A2)*x + A1)*x + 1.)
+             / ((((B4*x + B3)*x + B2)*x + B1)*x + 1.);
+    } else {
+        return 2.*(expm1(-qsq) + qsq)/(qsq*qsq);
+    }
+}
+
+static double
 a_long(double qp, double L, double b/*, double p1, double p2, double q0*/)
 {
-    // Note: caller sends p1, p2, q0 in as constants.
     const double p1 = 4.12;
     const double p2 = 4.42;
     const double q0 = 3.1;
 
+    // Constants C1, ..., C5 derived from least squares fit (Pedersen, eq 13,16)
     const double C1 = 1.22;
     const double C2 = 0.4288;
@@ -37,5 +67,4 @@
     const double C5 = 0.1477;
     const double miu = 0.585;
-
 
     const double C = (L/b>10.0 ? 3.06*pow(L/b, -0.44) : 1.0);
@@ -48,5 +77,5 @@
     const double em1_qr_b_sq = expm1(-qr_b_sq);
     const double sech2 = 1.0/square(cosh((qr_b-C4)/C5));
-    const double tanh1m = 1.0 - tanh(-C4 + qr_b/C5);
+    const double w = w_WR(qr_b);
 
     const double t1 = pow(q0, 1.0 + p1 + p2)/(b*(p1-p2));
@@ -57,12 +86,11 @@
     const double t3 = r*sech2/(2.*C5)*t11;
     const double t4 = r*(em1_qr_b_sq + qr_b_sq)*sech2 / (C5*qr_b_4);
-    const double t5 = -2.0 * r*qr_b*em1_qr_b_sq * tanh1m / qr_b_4;
-    const double t10 = (em1_qr_b_sq + qr_b_sq) * tanh1m / qr_b_4;
+    const double t5 = -4.0*r*qr_b*em1_qr_b_sq/qr_b_4 * (1.0 - w);
+    const double t10 = 2.0*(em1_qr_b_sq + qr_b_sq)/qr_b_4 * (1.0 - w); //=Sdebye*(1-w)
     const double t6 = 4.0*b/q0 * t10;
     const double t7 = r*((-3.0*C3*qr_b_miu -2.0*C2)*qr_b_miu -1.0*C1)*qr_b_miu/(miu*qr_b);
-    const double t8 = 2.0 - tanh1m;
-    const double t9 = b*C/(15.0*L) * (4.0 - exp(-qr_b_sq) * (11.0 + 7.0/qr_b_sq) + 7.0/qr_b_sq);
-    const double t12 = b*b*M_PI/(L*q0*q0) + t2 + t3 - t4 + t5 - t6 + 0.5*t7*t8;
-    const double t13 = -b*M_PI/(L*q0) + t9 + t10 + 0.5*t11*t8;
+    const double t9 = C*b/L * (4.0 - exp(-qr_b_sq) * (11.0 + 7.0/qr_b_sq) + 7.0/qr_b_sq)/15.0;
+    const double t12 = b*b*M_PI/(L*q0*q0) + t2 + t3 - t4 + t5 - t6 + t7*w;
+    const double t13 = -b*M_PI/(L*q0) + t9 + t10 + t11*w;
 
     const double a1 = pow(q0,p1)*t13 - t1*pow(q0,-p2)*(t12 + b*p1/q0*t13);
@@ -110,50 +138,9 @@
 }
 
-//WR named this w (too generic)
-static double
-w_WR(double x)
-{
-    return 0.5*(1 + tanh((x - 1.523)/0.1477));
-}
-
-static double
-Sdebye(double qsq)
-{
-#if FLOAT_SIZE>4
-#define DEBYE_CUTOFF 0.1  // 1e-14 error
-#else
-#define DEBYE_CUTOFF 0.9  // 4e-7 error
-#endif
-
-/* For double precision, the following gets 1e-15 error rather than 1e-14
-    if (qsq < 9./16.) {
-        // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]
-        const double A1=1./12., A2=2./99., A3=1./2640., A4=1./23760., A5=-1./1995840.;
-        const double B1=5./12., B2=5./66., B3=1./132., B4=1./2376., B5=1./95040.;
-        const double x = qsq;
-        return (((((A5*x + A4)*x + A3)*x + A2)*x + A1)*x + 1.)
-                /(((((B5*x + B4)*x + B3)*x + B2)*x + B1)*x + 1.);
-    }
-*/
-
-    if (qsq < DEBYE_CUTOFF) {
-        const double x = qsq;
-        const double C0 = +1.;
-        const double C1 = -1./3.;
-        const double C2 = +1./12.;
-        const double C3 = -1./60.;
-        const double C4 = +1./360.;
-        const double C5 = -1./2520.;
-        const double C6 = +1./20160.;
-        const double C7 = -1./181440.;
-        return ((((((C7*x + C6)*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
-    } else {
-        return 2.*(exp(-qsq) + qsq - 1.)/(qsq*qsq);
-    }
-}
 
 static double
 Sexv(double q, double L, double b)
 {
+    // Pedersen eq 13, corrected by Chen eq A.5, swapping w and 1-w
     const double C1=1.22;
     const double C2=0.4288;
@@ -161,23 +148,20 @@
     const double miu = 0.585;
     const double qr = q*sqrt(Rgsquare(L,b));
-    const double x = pow(qr, -1.0/miu);
+    const double qr_miu = pow(qr, -1.0/miu);
     const double w = w_WR(qr);
-
-    const double base = (1.0 - w)*Sdebye(qr*qr);
-    const double correction = w*x*(C1 + x*(C2 + x*C3));
-    return base + correction;
-}
-
-
+    const double t10 = qr*Sdebye(qr*qr)*(1.0 - w);
+    const double t11 = ((C3*qr_miu + C2)*qr_miu + C1)*qr_miu;
+
+    return t10 + w*t11;
+}
+
+// Modified by Yun on Oct. 15,
 static double
 Sexv_new(double q, double L, double b)
 {
-    // Modified by Yun on Oct. 15,
-
-    // Correction factor to apply to the returned Sexv
     const double qr = q*sqrt(Rgsquare(L,b));
     const double qr2 = qr*qr;
-    const double t1 = (4.0/15.0 + 7.0/(15.0*qr2) - (11.0/15.0 + 7.0/(15.0*qr2))*exp(-qr2))*(b/L);
-    const double C = (L/b > 10.0) ? 3.06*pow(L/b, -0.44)*t1 : t1;
+    const double C = (L/b > 10.0) ? 3.06*pow(L/b, -0.44) : 1.0;
+    const double t9 = C*b/L * (4.0 - exp(-qr2) * (11.0 + 7.0/qr2) + 7.0/qr2)/15.0;
 
     const double Sexv_orig = Sexv(q, L, b);
@@ -189,10 +173,9 @@
 
     if (qdel < 0) {
-        // return Sexv with the additional correction
-        return Sexv_orig + C;
+        return t9 + Sexv_orig;
     } else {
-        // recalculate Sexv base and return it with the additional correction
         const double w = w_WR(qr);
-        return (1.0 - w)*Sdebye(qr2) + C;
+        const double t10 = Sdebye(qr*qr)*(1.0 - w);
+        return t9 + t10;
     }
 }
@@ -214,9 +197,6 @@
     } else { // L <= 4*b : Shorter Chains
         if (q*b <= q0short) { // q*b <= fmax(1.9/Rg_short, 3)
-            if (q*b <= 0.01) {
-                ans = 1.0 - square(q*Rg_short)/3.0;
-            } else {
-                ans = Sdebye(square(q*Rg_short));
-            }
+            // 2017-10-01 pkienzle: moved low q approximation into Sdebye()
+            ans = Sdebye(square(q*Rg_short));
         } else {  // q*b > max(1.9/Rg_short, 3)
             ans = a_short(q, L, b /*, p1short, p2short*/, q0short);
