float_incdec.mx raw

   1  // Regression: `x++` and `x--` on a FLOAT must actually change x.
   2  //
   3  // buildAssign's `Rhs == nil` branch built the literal 1 as a ConstInt and merely
   4  // LABELLED it with the operand's type:
   5  //
   6  //	one := &SSAConst{typ: lv.SSAType(), val: &ConstInt{V:1}}
   7  //
   8  // so the RHS domain never matched a float operand and the statement compiled to
   9  // nothing:
  10  //
  11  //	y := 2.0; y++      -> y is still 2.0
  12  //	f := 1.5; f++      -> f is still 1.5
  13  //	i := int64(2); i++ -> 3        (integers were never affected)
  14  //	y += 1             -> 3        (compound assignment was never affected)
  15  //
  16  // The 1 is now a ConstFloat for a float operand.
  17  //
  18  // This is the bug behind math.Sin and math.Cos: their range reduction does
  19  // `j++; y++` with y a float64, so y stayed un-incremented and the reduced
  20  // argument was wrong. Sin(1.0) returned 0.977061263899476 instead of
  21  // 0.8414709848078965, and everything downstream of trig went with it.
  22  package main
  23  
  24  import "math"
  25  
  26  func main() {
  27  	// Termination: a batch program must still be stoppable, so the entry
  28  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  29  	select {
  30  	case <-shutdown:
  31  		return
  32  	default:
  33  	}
  34  
  35  	// float64, repeatedly, both directions
  36  	y := 2.0
  37  	y++
  38  	if y != 3.0 {
  39  		panic("y++ on float64")
  40  	}
  41  	y++
  42  	if y != 4.0 {
  43  		panic("y++ twice")
  44  	}
  45  	y--
  46  	if y != 3.0 {
  47  		panic("y-- on float64")
  48  	}
  49  
  50  	var f float32 = 1.25
  51  	f++
  52  	if f != 2.25 {
  53  		panic("f++ on float32")
  54  	}
  55  	f--
  56  	if f != 1.25 {
  57  		panic("f-- on float32")
  58  	}
  59  
  60  	// integers must keep working (this was the only case that did)
  61  	i := int64(2)
  62  	i++
  63  	i++
  64  	i--
  65  	if i != 3 {
  66  		panic("int64 ++/--")
  67  	}
  68  	var u uint32 = 7
  69  	u++
  70  	if u != 8 {
  71  		panic("uint32 ++")
  72  	}
  73  
  74  	// an element, not just a variable
  75  	p := []int32{1, 2, 3}
  76  	p[1]++
  77  	if p[1] != 3 {
  78  		panic("p[1]++")
  79  	}
  80  
  81  	// and the trig that depended on it
  82  	if math.Sin(1.0) != 0.8414709848078965 {
  83  		panic("Sin(1.0)")
  84  	}
  85  	if math.Cos(1.0) != 0.5403023058681398 {
  86  		panic("Cos(1.0)")
  87  	}
  88  	println("ok")
  89  }
  90