github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/cmd/compile/internal/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 package big_test 6 7 import ( 8 "cmd/compile/internal/big" 9 "fmt" 10 "math" 11 ) 12 13 func ExampleFloat_Add() { 14 // Operating 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 Example_Shift() { 30 // Implementing 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 }