| 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 |
"slices" |
| 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 |
slices.SortFunc(vs, func(a, b constant.Value) int { |
| 102 |
if constant.Compare(a, token.LSS, b) { |
| 103 |
return -1 |
| 104 |
} |
| 105 |
if constant.Compare(a, token.GTR, b) { |
| 106 |
return +1 |
| 107 |
} |
| 108 |
return 0 |
| 109 |
}) |
| 110 |
|
| 111 |
for _, v := range vs { |
| 112 |
fmt.Println(constant.StringVal(v)) |
| 113 |
} |
| 114 |
|
| 115 |
// Output: |
| 116 |
// |
| 117 |
// Frame |
| 118 |
// Z |
| 119 |
// a |
| 120 |
// bacon |
| 121 |
// defer |
| 122 |
// go |
| 123 |
} |
| 124 |
|
| 125 |
func ExampleSign() { |
| 126 |
zero := constant.MakeInt64(0) |
| 127 |
one := constant.MakeInt64(1) |
| 128 |
negOne := constant.MakeInt64(-1) |
| 129 |
|
| 130 |
mkComplex := func(a, b constant.Value) constant.Value { |
| 131 |
b = constant.MakeImag(b) |
| 132 |
return constant.BinaryOp(a, token.ADD, b) |
| 133 |
} |
| 134 |
|
| 135 |
vs := []constant.Value{ |
| 136 |
negOne, |
| 137 |
mkComplex(zero, negOne), |
| 138 |
mkComplex(one, negOne), |
| 139 |
mkComplex(negOne, one), |
| 140 |
mkComplex(negOne, negOne), |
| 141 |
zero, |
| 142 |
mkComplex(zero, zero), |
| 143 |
one, |
| 144 |
mkComplex(zero, one), |
| 145 |
mkComplex(one, one), |
| 146 |
} |
| 147 |
|
| 148 |
for _, v := range vs { |
| 149 |
fmt.Printf("% d %s\n", constant.Sign(v), v) |
| 150 |
} |
| 151 |
|
| 152 |
// Output: |
| 153 |
// |
| 154 |
// -1 -1 |
| 155 |
// -1 (0 + -1i) |
| 156 |
// -1 (1 + -1i) |
| 157 |
// -1 (-1 + 1i) |
| 158 |
// -1 (-1 + -1i) |
| 159 |
// 0 0 |
| 160 |
// 0 (0 + 0i) |
| 161 |
// 1 1 |
| 162 |
// 1 (0 + 1i) |
| 163 |
// 1 (1 + 1i) |
| 164 |
} |
| 165 |
|
| 166 |
func ExampleVal() { |
| 167 |
maxint := constant.MakeInt64(math.MaxInt64) |
| 168 |
fmt.Printf("%v\n", constant.Val(maxint)) |
| 169 |
|
| 170 |
e := constant.MakeFloat64(math.E) |
| 171 |
fmt.Printf("%v\n", constant.Val(e)) |
| 172 |
|
| 173 |
b := constant.MakeBool(true) |
| 174 |
fmt.Printf("%v\n", constant.Val(b)) |
| 175 |
|
| 176 |
b = constant.Make(false) |
| 177 |
fmt.Printf("%v\n", constant.Val(b)) |
| 178 |
|
| 179 |
// Output: |
| 180 |
// |
| 181 |
// 9223372036854775807 |
| 182 |
// 6121026514868073/2251799813685248 |
| 183 |
// true |
| 184 |
// false |
| 185 |
} |