main.mx raw

   1  // Untyped constant promotion: an integer operand against a float operand is a
   2  // float operation.
   3  //
   4  // stage4's SSA-builder constant fold took the LEFT operand's type, so
   5  // `1/2.0` folded to the int constant 0 (and `var x float64 = 1/2.0` to 0.0)
   6  // while the same expression in a `const` declaration folded correctly. That
   7  // also truncated `1/Ln2`, the basis of math.Log2, and math.Log2 sizes the
   8  // buffer in math/big's base-10 conversion - so `big.Int.String` lost its
   9  // leading digits. The non-fold path already applied the promotion rule.
  10  package main
  11  
  12  import "math"
  13  
  14  func main() {
  15  	if 1/2.0 != 0.5 {
  16  		panic("1/2.0")
  17  	}
  18  	if 7/2.0 != 3.5 {
  19  		panic("7/2.0")
  20  	}
  21  	if 1/4.0 != 0.25 {
  22  		panic("1/4.0")
  23  	}
  24  	if 1/2.5 != 0.4 {
  25  		panic("1/2.5")
  26  	}
  27  	if 1.0/2 != 0.5 {
  28  		panic("1.0/2")
  29  	}
  30  	if 2*0.5 != 1.0 {
  31  		panic("2*0.5")
  32  	}
  33  	if 1+0.5 != 1.5 {
  34  		panic("1+0.5")
  35  	}
  36  	if 1-0.5 != 0.5 {
  37  		panic("1-0.5")
  38  	}
  39  	if 1/math.Ln2 < 1.442695 || 1/math.Ln2 > 1.442696 {
  40  		panic("1/Ln2")
  41  	}
  42  	l2 := math.Log2(10)
  43  	if l2 < 3.321928 || l2 > 3.321929 {
  44  		panic("Log2(10)")
  45  	}
  46  	println("ok")
  47  }
  48