bessel_large_x.mx raw

   1  // Regression: math.J0/J1/Y0/Y1 must not take the "very large x" shortcut below
   2  // 2**129.
   3  //
   4  // j0.mx and j1.mx both guard their asymptotic branch with
   5  //
   6  //	Two129 = 1 << 129     // |x| > ~6.8056e+38
   7  //	if x > Two129 { ... skip pzero/qzero ... }
   8  //
   9  // Moxie does not evaluate a constant wider than 64 bits exactly, so `1 << 129`
  10  // folded to 0 and `x > Two129` was TRUE for every positive x - the shortcut was
  11  // taken permanently and pzero/qzero were never called:
  12  //
  13  //	J0(2.5) = -0.0723182119715167   want -0.04838377646819797
  14  //	Y0(2.5) = +0.499417646028127    want +0.4980703596152319
  15  //
  16  // pzero and qzero themselves were verified correct to 16 digits against an
  17  // independent reference, which is how the guard was found rather than a table.
  18  // `Two129` is now the exact hex float 0x1p129, and lgamma.mx's
  19  // `Tiny = 1.0 / (1 << 70)` is 0x1p-70 for the same reason.
  20  package main
  21  
  22  import "math"
  23  
  24  func close(got, want float64) (ok bool) {
  25  	d := got - want
  26  	if d < 0 {
  27  		d = -d
  28  	}
  29  	return d < 1e-14
  30  }
  31  
  32  func main() {
  33  	// Termination: a batch program must still be stoppable, so the entry
  34  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  35  	select {
  36  	case <-shutdown:
  37  		return
  38  	default:
  39  	}
  40  
  41  	if !close(math.J0(2.0), 0.22389077914123567) {
  42  		panic("J0(2.0)")
  43  	}
  44  	if !close(math.J0(2.5), -0.04838377646819797) {
  45  		panic("J0(2.5)")
  46  	}
  47  	if !close(math.J0(3.0), -0.26005195490193343) {
  48  		panic("J0(3.0)")
  49  	}
  50  	if !close(math.J0(8.0), 0.17165080713755357) {
  51  		panic("J0(8.0)")
  52  	}
  53  	if !close(math.J0(10.0), -0.2459357644513483) {
  54  		panic("J0(10.0)")
  55  	}
  56  	if !close(math.J1(2.5), 0.4970941024642741) {
  57  		panic("J1(2.5)")
  58  	}
  59  	if !close(math.J1(10.0), 0.04347274616886144) {
  60  		panic("J1(10.0)")
  61  	}
  62  	if !close(math.Y0(2.5), 0.4980703596152319) {
  63  		panic("Y0(2.5)")
  64  	}
  65  	if !close(math.Y1(2.5), 0.14591813796678535) {
  66  		panic("Y1(2.5)")
  67  	}
  68  	lg, _ := math.Lgamma(3.0)
  69  	if !close(lg, 0.6931471805599455) {
  70  		panic("Lgamma(3)")
  71  	}
  72  	if !close(math.Gamma(3.0), 2.0) {
  73  		panic("Gamma(3)")
  74  	}
  75  	// Jn/Yn carry their own copy of the same guard (Two302 = 1 << 302), so
  76  	// they were broken the same way. Values cross-checked against the
  77  	// recurrence J_{n+1} = (2n/x)J_n - J_{n-1} over the verified J0/J1 and
  78  	// Y0/Y1 above, not taken from a table.
  79  	if !close(math.Jn(0, 2.5), -0.04838377646819797) {
  80  		panic("Jn(0,2.5)")
  81  	}
  82  	if !close(math.Jn(1, 2.5), 0.4970941024642741) {
  83  		panic("Jn(1,2.5)")
  84  	}
  85  	if !close(math.Jn(2, 2.5), 0.4460590584396173) {
  86  		panic("Jn(2,2.5)")
  87  	}
  88  	if !close(math.Jn(3, 2.5), 0.21660039103911366) {
  89  		panic("Jn(3,2.5)")
  90  	}
  91  	if !close(math.Yn(0, 2.5), 0.4980703596152319) {
  92  		panic("Yn(0,2.5)")
  93  	}
  94  	if !close(math.Yn(1, 2.5), 0.14591813796678535) {
  95  		panic("Yn(1,2.5)")
  96  	}
  97  	if !close(math.Yn(2, 2.5), -0.3813358492418036) {
  98  		panic("Yn(2,2.5)")
  99  	}
 100  
 101  	// the trig that the float-increment bug had broken
 102  	if !close(math.Sin(1.0), 0.8414709848078965) {
 103  		panic("Sin(1.0)")
 104  	}
 105  	println("ok")
 106  }
 107