Source file
src/math/big/floatexample_test.go
1
2
3
4
5 package big_test
6
7 import (
8 "fmt"
9 "math"
10 "math/big"
11 )
12
13 func ExampleFloat_Add() {
14
15 var x, y, z big.Float
16 x.SetInt64(1000)
17 y.SetFloat64(2.718281828)
18 z.SetPrec(32)
19 z.Add(&x, &y)
20 fmt.Printf("x = %.10g (%s, prec = %d, acc = %s)\n", &x, x.Text('p', 0), x.Prec(), x.Acc())
21 fmt.Printf("y = %.10g (%s, prec = %d, acc = %s)\n", &y, y.Text('p', 0), y.Prec(), y.Acc())
22 fmt.Printf("z = %.10g (%s, prec = %d, acc = %s)\n", &z, z.Text('p', 0), z.Prec(), z.Acc())
23
24
25
26
27 }
28
29 func ExampleFloat_shift() {
30
31 for s := -5; s <= 5; s++ {
32 x := big.NewFloat(0.5)
33 x.SetMantExp(x, x.MantExp(nil)+s)
34 fmt.Println(x)
35 }
36
37
38
39
40
41
42
43
44
45
46
47
48 }
49
50 func ExampleFloat_Cmp() {
51 inf := math.Inf(1)
52 zero := 0.0
53
54 operands := []float64{-inf, -1.2, -zero, 0, +1.2, +inf}
55
56 fmt.Println(" x y cmp")
57 fmt.Println("---------------")
58 for _, x64 := range operands {
59 x := big.NewFloat(x64)
60 for _, y64 := range operands {
61 y := big.NewFloat(y64)
62 fmt.Printf("%4g %4g %3d\n", x, y, x.Cmp(y))
63 }
64 fmt.Println()
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 }
112
113 func ExampleRoundingMode() {
114 operands := []float64{2.6, 2.5, 2.1, -2.1, -2.5, -2.6}
115
116 fmt.Print(" x")
117 for mode := big.ToNearestEven; mode <= big.ToPositiveInf; mode++ {
118 fmt.Printf(" %s", mode)
119 }
120 fmt.Println()
121
122 for _, f64 := range operands {
123 fmt.Printf("%4g", f64)
124 for mode := big.ToNearestEven; mode <= big.ToPositiveInf; mode++ {
125
126
127 f := new(big.Float).SetPrec(2).SetMode(mode).SetFloat64(f64)
128 fmt.Printf(" %*g", len(mode.String()), f)
129 }
130 fmt.Println()
131 }
132
133
134
135
136
137
138
139
140
141 }
142
143 func ExampleFloat_Copy() {
144 var x, z big.Float
145
146 x.SetFloat64(1.23)
147 r := z.Copy(&x)
148 fmt.Printf("a) r = %g, z = %g, x = %g, r == z = %v\n", r, &z, &x, r == &z)
149
150
151 z.SetInt64(42)
152 fmt.Printf("b) r = %g, z = %g, r == z = %v\n", r, &z, r == &z)
153
154 x.SetPrec(1)
155 z.Copy(&x)
156 fmt.Printf("c) z = %g, x = %g, z == x = %v\n", &z, &x, &z == &x)
157
158
159
160
161
162 }
163
View as plain text