| 1 |
// Copyright 2015 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 big_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"math" |
| 10 |
"math/big" |
| 11 |
) |
| 12 |
|
| 13 |
func ExampleFloat_Add() { |
| 14 |
// Operate on numbers of different precision. |
| 15 |
var x, y, z big.Float |
| 16 |
x.SetInt64(1000) // x is automatically set to 64bit precision |
| 17 |
y.SetFloat64(2.718281828) // y is automatically set to 53bit precision |
| 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 |
// Output: |
| 24 |
// x = 1000 (0x.fap+10, prec = 64, acc = Exact) |
| 25 |
// y = 2.718281828 (0x.adf85458248cd8p+2, prec = 53, acc = Exact) |
| 26 |
// z = 1002.718282 (0x.faadf854p+10, prec = 32, acc = Below) |
| 27 |
} |
| 28 |
|
| 29 |
func ExampleFloat_shift() { |
| 30 |
// Implement Float "shift" by modifying the (binary) exponents directly. |
| 31 |
for s := -5; s <= 5; s++ { |
| 32 |
x := big.NewFloat(0.5) |
| 33 |
x.SetMantExp(x, x.MantExp(nil)+s) // shift x by s |
| 34 |
fmt.Println(x) |
| 35 |
} |
| 36 |
// Output: |
| 37 |
// 0.015625 |
| 38 |
// 0.03125 |
| 39 |
// 0.0625 |
| 40 |
// 0.125 |
| 41 |
// 0.25 |
| 42 |
// 0.5 |
| 43 |
// 1 |
| 44 |
// 2 |
| 45 |
// 4 |
| 46 |
// 8 |
| 47 |
// 16 |
| 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 |
// Output: |
| 68 |
// x y cmp |
| 69 |
// --------------- |
| 70 |
// -Inf -Inf 0 |
| 71 |
// -Inf -1.2 -1 |
| 72 |
// -Inf -0 -1 |
| 73 |
// -Inf 0 -1 |
| 74 |
// -Inf 1.2 -1 |
| 75 |
// -Inf +Inf -1 |
| 76 |
// |
| 77 |
// -1.2 -Inf 1 |
| 78 |
// -1.2 -1.2 0 |
| 79 |
// -1.2 -0 -1 |
| 80 |
// -1.2 0 -1 |
| 81 |
// -1.2 1.2 -1 |
| 82 |
// -1.2 +Inf -1 |
| 83 |
// |
| 84 |
// -0 -Inf 1 |
| 85 |
// -0 -1.2 1 |
| 86 |
// -0 -0 0 |
| 87 |
// -0 0 0 |
| 88 |
// -0 1.2 -1 |
| 89 |
// -0 +Inf -1 |
| 90 |
// |
| 91 |
// 0 -Inf 1 |
| 92 |
// 0 -1.2 1 |
| 93 |
// 0 -0 0 |
| 94 |
// 0 0 0 |
| 95 |
// 0 1.2 -1 |
| 96 |
// 0 +Inf -1 |
| 97 |
// |
| 98 |
// 1.2 -Inf 1 |
| 99 |
// 1.2 -1.2 1 |
| 100 |
// 1.2 -0 1 |
| 101 |
// 1.2 0 1 |
| 102 |
// 1.2 1.2 0 |
| 103 |
// 1.2 +Inf -1 |
| 104 |
// |
| 105 |
// +Inf -Inf 1 |
| 106 |
// +Inf -1.2 1 |
| 107 |
// +Inf -0 1 |
| 108 |
// +Inf 0 1 |
| 109 |
// +Inf 1.2 1 |
| 110 |
// +Inf +Inf 0 |
| 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 |
// sample operands above require 2 bits to represent mantissa |
| 126 |
// set binary precision to 2 to round them to integer values |
| 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 |
// Output: |
| 134 |
// x ToNearestEven ToNearestAway ToZero AwayFromZero ToNegativeInf ToPositiveInf |
| 135 |
// 2.6 3 3 2 3 2 3 |
| 136 |
// 2.5 2 3 2 3 2 3 |
| 137 |
// 2.1 2 2 2 3 2 3 |
| 138 |
// -2.1 -2 -2 -2 -3 -3 -2 |
| 139 |
// -2.5 -2 -3 -2 -3 -3 -2 |
| 140 |
// -2.6 -3 -3 -2 -3 -3 -2 |
| 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 |
// changing z changes r since they are identical |
| 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 |
// Output: |
| 159 |
// a) r = 1.23, z = 1.23, x = 1.23, r == z = true |
| 160 |
// b) r = 42, z = 42, r == z = true |
| 161 |
// c) z = 1, x = 1, z == x = false |
| 162 |
} |