1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. See the License for the specific language governing 13 // permissions and limitations under the License. 14 15 package apd_test 16 17 import ( 18 "fmt" 19 20 "github.com/cockroachdb/apd/v3" 21 ) 22 23 // ExampleOverflow demonstrates how to detect or error on overflow. 24 func ExampleContext_overflow() { 25 // Create a context that will overflow at 1e3. 26 c := apd.Context{ 27 MaxExponent: 2, 28 Traps: apd.Overflow, 29 } 30 one := apd.New(1, 0) 31 d := apd.New(997, 0) 32 for { 33 res, err := c.Add(d, d, one) 34 fmt.Printf("d: %8s, overflow: %5v, err: %v\n", d, res.Overflow(), err) 35 if err != nil { 36 return 37 } 38 } 39 // Output: 40 // d: 998, overflow: false, err: <nil> 41 // d: 999, overflow: false, err: <nil> 42 // d: Infinity, overflow: true, err: overflow 43 } 44 45 // ExampleInexact demonstrates how to detect inexact operations. 46 func ExampleContext_inexact() { 47 d := apd.New(27, 0) 48 three := apd.New(3, 0) 49 c := apd.BaseContext.WithPrecision(5) 50 for { 51 res, err := c.Quo(d, d, three) 52 fmt.Printf("d: %7s, inexact: %5v, err: %v\n", d, res.Inexact(), err) 53 if err != nil { 54 return 55 } 56 if res.Inexact() { 57 return 58 } 59 } 60 // Output: 61 // d: 9.0000, inexact: false, err: <nil> 62 // d: 3.0000, inexact: false, err: <nil> 63 // d: 1.0000, inexact: false, err: <nil> 64 // d: 0.33333, inexact: true, err: <nil> 65 } 66 67 func ExampleContext_Quantize() { 68 input, _, _ := apd.NewFromString("123.45") 69 output := new(apd.Decimal) 70 c := apd.BaseContext.WithPrecision(10) 71 for i := int32(-3); i <= 3; i++ { 72 res, _ := c.Quantize(output, input, i) 73 fmt.Printf("%2v: %s", i, output) 74 if res != 0 { 75 fmt.Printf(" (%s)", res) 76 } 77 fmt.Println() 78 } 79 // Output: 80 // -3: 123.450 81 // -2: 123.45 82 // -1: 123.5 (inexact, rounded) 83 // 0: 123 (inexact, rounded) 84 // 1: 1.2E+2 (inexact, rounded) 85 // 2: 1E+2 (inexact, rounded) 86 // 3: 0E+3 (inexact, rounded) 87 } 88 89 func ExampleErrDecimal() { 90 c := apd.BaseContext.WithPrecision(5) 91 ed := apd.MakeErrDecimal(c) 92 d := apd.New(10, 0) 93 fmt.Printf("%s, err: %v\n", d, ed.Err()) 94 ed.Add(d, d, apd.New(2, 1)) // add 20 95 fmt.Printf("%s, err: %v\n", d, ed.Err()) 96 ed.Quo(d, d, apd.New(0, 0)) // divide by zero 97 fmt.Printf("%s, err: %v\n", d, ed.Err()) 98 ed.Sub(d, d, apd.New(1, 0)) // attempt to subtract 1 99 // The subtraction doesn't occur and doesn't change the error. 100 fmt.Printf("%s, err: %v\n", d, ed.Err()) 101 // Output: 102 // 10, err: <nil> 103 // 30, err: <nil> 104 // Infinity, err: division by zero 105 // Infinity, err: division by zero 106 } 107 108 // ExampleRoundToIntegralExact demonstrates how to use RoundToIntegralExact to 109 // check if a number is an integer or not. Note the variations between integer 110 // (which allows zeros after the decimal point) and strict (which does not). See 111 // the documentation on Inexact and Rounded. 112 func ExampleContext_RoundToIntegralExact() { 113 inputs := []string{ 114 "123.4", 115 "123.0", 116 "123", 117 "12E1", 118 "120E-1", 119 "120E-2", 120 } 121 for _, input := range inputs { 122 d, _, _ := apd.NewFromString(input) 123 res, _ := apd.BaseContext.RoundToIntegralExact(d, d) 124 integer := !res.Inexact() 125 strict := !res.Rounded() 126 fmt.Printf("input: % 6s, output: %3s, integer: %5t, strict: %5t, res:", input, d, integer, strict) 127 if res != 0 { 128 fmt.Printf(" %s", res) 129 } 130 fmt.Println() 131 } 132 // Output: 133 // input: 123.4, output: 123, integer: false, strict: false, res: inexact, rounded 134 // input: 123.0, output: 123, integer: true, strict: false, res: rounded 135 // input: 123, output: 123, integer: true, strict: true, res: 136 // input: 12E1, output: 120, integer: true, strict: true, res: 137 // input: 120E-1, output: 12, integer: true, strict: false, res: rounded 138 // input: 120E-2, output: 1, integer: false, strict: false, res: inexact, rounded 139 } 140