github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/go/math/big/floatexample_test.go (about) 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 // +build ignore 6 7 package big_test 8 9 import ( 10 "fmt" 11 "math" 12 "math/big" 13 ) 14 15 func ExampleFloat_Add() { 16 // Operating on numbers of different precision. 17 var x, y, z big.Float 18 x.SetInt64(1000) // x is automatically set to 64bit precision 19 y.SetFloat64(2.718281828) // y is automatically set to 53bit precision 20 z.SetPrec(32) 21 z.Add(&x, &y) 22 fmt.Printf("x = %.10g (%s, prec = %d, acc = %s)\n", &x, x.Text('p', 0), x.Prec(), x.Acc()) 23 fmt.Printf("y = %.10g (%s, prec = %d, acc = %s)\n", &y, y.Text('p', 0), y.Prec(), y.Acc()) 24 fmt.Printf("z = %.10g (%s, prec = %d, acc = %s)\n", &z, z.Text('p', 0), z.Prec(), z.Acc()) 25 // Output: 26 // x = 1000 (0x.fap+10, prec = 64, acc = Exact) 27 // y = 2.718281828 (0x.adf85458248cd8p+2, prec = 53, acc = Exact) 28 // z = 1002.718282 (0x.faadf854p+10, prec = 32, acc = Below) 29 } 30 31 func Example_Shift() { 32 // Implementing Float "shift" by modifying the (binary) exponents directly. 33 for s := -5; s <= 5; s++ { 34 x := big.NewFloat(0.5) 35 x.SetMantExp(x, x.MantExp(nil)+s) // shift x by s 36 fmt.Println(x) 37 } 38 // Output: 39 // 0.015625 40 // 0.03125 41 // 0.0625 42 // 0.125 43 // 0.25 44 // 0.5 45 // 1 46 // 2 47 // 4 48 // 8 49 // 16 50 } 51 52 func ExampleFloat_Cmp() { 53 inf := math.Inf(1) 54 zero := 0.0 55 56 operands := []float64{-inf, -1.2, -zero, 0, +1.2, +inf} 57 58 fmt.Println(" x y cmp") 59 fmt.Println("---------------") 60 for _, x64 := range operands { 61 x := big.NewFloat(x64) 62 for _, y64 := range operands { 63 y := big.NewFloat(y64) 64 fmt.Printf("%4g %4g %3d\n", x, y, x.Cmp(y)) 65 } 66 fmt.Println() 67 } 68 69 // Output: 70 // x y cmp 71 // --------------- 72 // -Inf -Inf 0 73 // -Inf -1.2 -1 74 // -Inf -0 -1 75 // -Inf 0 -1 76 // -Inf 1.2 -1 77 // -Inf +Inf -1 78 // 79 // -1.2 -Inf 1 80 // -1.2 -1.2 0 81 // -1.2 -0 -1 82 // -1.2 0 -1 83 // -1.2 1.2 -1 84 // -1.2 +Inf -1 85 // 86 // -0 -Inf 1 87 // -0 -1.2 1 88 // -0 -0 0 89 // -0 0 0 90 // -0 1.2 -1 91 // -0 +Inf -1 92 // 93 // 0 -Inf 1 94 // 0 -1.2 1 95 // 0 -0 0 96 // 0 0 0 97 // 0 1.2 -1 98 // 0 +Inf -1 99 // 100 // 1.2 -Inf 1 101 // 1.2 -1.2 1 102 // 1.2 -0 1 103 // 1.2 0 1 104 // 1.2 1.2 0 105 // 1.2 +Inf -1 106 // 107 // +Inf -Inf 1 108 // +Inf -1.2 1 109 // +Inf -0 1 110 // +Inf 0 1 111 // +Inf 1.2 1 112 // +Inf +Inf 0 113 }