...

Source file src/go/constant/example_test.go

Documentation: go/constant

		 1  // Copyright 2018 The Go Authors. All rights reserved.
		 2  // Use of this source code is governed by a BSD-style
		 3  // license that can be found in the LICENSE file.
		 4  
		 5  package constant_test
		 6  
		 7  import (
		 8  	"fmt"
		 9  	"go/constant"
		10  	"go/token"
		11  	"math"
		12  	"sort"
		13  )
		14  
		15  func Example_complexNumbers() {
		16  	// Create the complex number 2.3 + 5i.
		17  	ar := constant.MakeFloat64(2.3)
		18  	ai := constant.MakeImag(constant.MakeInt64(5))
		19  	a := constant.BinaryOp(ar, token.ADD, ai)
		20  
		21  	// Compute (2.3 + 5i) * 11.
		22  	b := constant.MakeUint64(11)
		23  	c := constant.BinaryOp(a, token.MUL, b)
		24  
		25  	// Convert c into a complex128.
		26  	Ar, exact := constant.Float64Val(constant.Real(c))
		27  	if !exact {
		28  		fmt.Printf("Could not represent real part %s exactly as float64\n", constant.Real(c))
		29  	}
		30  	Ai, exact := constant.Float64Val(constant.Imag(c))
		31  	if !exact {
		32  		fmt.Printf("Could not represent imaginary part %s as exactly as float64\n", constant.Imag(c))
		33  	}
		34  	C := complex(Ar, Ai)
		35  
		36  	fmt.Println("literal", 25.3+55i)
		37  	fmt.Println("go/constant", c)
		38  	fmt.Println("complex128", C)
		39  
		40  	// Output:
		41  	//
		42  	// Could not represent real part 25.3 exactly as float64
		43  	// literal (25.3+55i)
		44  	// go/constant (25.3 + 55i)
		45  	// complex128 (25.299999999999997+55i)
		46  }
		47  
		48  func ExampleBinaryOp() {
		49  	// 11 / 0.5
		50  	a := constant.MakeUint64(11)
		51  	b := constant.MakeFloat64(0.5)
		52  	c := constant.BinaryOp(a, token.QUO, b)
		53  	fmt.Println(c)
		54  
		55  	// Output: 22
		56  }
		57  
		58  func ExampleUnaryOp() {
		59  	vs := []constant.Value{
		60  		constant.MakeBool(true),
		61  		constant.MakeFloat64(2.7),
		62  		constant.MakeUint64(42),
		63  	}
		64  
		65  	for i, v := range vs {
		66  		switch v.Kind() {
		67  		case constant.Bool:
		68  			vs[i] = constant.UnaryOp(token.NOT, v, 0)
		69  
		70  		case constant.Float:
		71  			vs[i] = constant.UnaryOp(token.SUB, v, 0)
		72  
		73  		case constant.Int:
		74  			// Use 16-bit precision.
		75  			// This would be equivalent to ^uint16(v).
		76  			vs[i] = constant.UnaryOp(token.XOR, v, 16)
		77  		}
		78  	}
		79  
		80  	for _, v := range vs {
		81  		fmt.Println(v)
		82  	}
		83  
		84  	// Output:
		85  	//
		86  	// false
		87  	// -2.7
		88  	// 65493
		89  }
		90  
		91  func ExampleCompare() {
		92  	vs := []constant.Value{
		93  		constant.MakeString("Z"),
		94  		constant.MakeString("bacon"),
		95  		constant.MakeString("go"),
		96  		constant.MakeString("Frame"),
		97  		constant.MakeString("defer"),
		98  		constant.MakeFromLiteral(`"a"`, token.STRING, 0),
		99  	}
	 100  
	 101  	sort.Slice(vs, func(i, j int) bool {
	 102  		// Equivalent to vs[i] <= vs[j].
	 103  		return constant.Compare(vs[i], token.LEQ, vs[j])
	 104  	})
	 105  
	 106  	for _, v := range vs {
	 107  		fmt.Println(constant.StringVal(v))
	 108  	}
	 109  
	 110  	// Output:
	 111  	//
	 112  	// Frame
	 113  	// Z
	 114  	// a
	 115  	// bacon
	 116  	// defer
	 117  	// go
	 118  }
	 119  
	 120  func ExampleSign() {
	 121  	zero := constant.MakeInt64(0)
	 122  	one := constant.MakeInt64(1)
	 123  	negOne := constant.MakeInt64(-1)
	 124  
	 125  	mkComplex := func(a, b constant.Value) constant.Value {
	 126  		b = constant.MakeImag(b)
	 127  		return constant.BinaryOp(a, token.ADD, b)
	 128  	}
	 129  
	 130  	vs := []constant.Value{
	 131  		negOne,
	 132  		mkComplex(zero, negOne),
	 133  		mkComplex(one, negOne),
	 134  		mkComplex(negOne, one),
	 135  		mkComplex(negOne, negOne),
	 136  		zero,
	 137  		mkComplex(zero, zero),
	 138  		one,
	 139  		mkComplex(zero, one),
	 140  		mkComplex(one, one),
	 141  	}
	 142  
	 143  	for _, v := range vs {
	 144  		fmt.Printf("% d %s\n", constant.Sign(v), v)
	 145  	}
	 146  
	 147  	// Output:
	 148  	//
	 149  	// -1 -1
	 150  	// -1 (0 + -1i)
	 151  	// -1 (1 + -1i)
	 152  	// -1 (-1 + 1i)
	 153  	// -1 (-1 + -1i)
	 154  	//	0 0
	 155  	//	0 (0 + 0i)
	 156  	//	1 1
	 157  	//	1 (0 + 1i)
	 158  	//	1 (1 + 1i)
	 159  }
	 160  
	 161  func ExampleVal() {
	 162  	maxint := constant.MakeInt64(math.MaxInt64)
	 163  	fmt.Printf("%v\n", constant.Val(maxint))
	 164  
	 165  	e := constant.MakeFloat64(math.E)
	 166  	fmt.Printf("%v\n", constant.Val(e))
	 167  
	 168  	b := constant.MakeBool(true)
	 169  	fmt.Printf("%v\n", constant.Val(b))
	 170  
	 171  	b = constant.Make(false)
	 172  	fmt.Printf("%v\n", constant.Val(b))
	 173  
	 174  	// Output:
	 175  	//
	 176  	// 9223372036854775807
	 177  	// 6121026514868073/2251799813685248
	 178  	// true
	 179  	// false
	 180  }
	 181  

View as plain text