ExampleOverflow demonstrates how to detect or error on overflow.
()
| 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() { |