-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
95 lines (82 loc) · 2.28 KB
/
Copy pathexample_test.go
File metadata and controls
95 lines (82 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fpfmt
import (
"bytes"
"fmt"
"os"
"text/tabwriter"
)
var rows bytes.Buffer
func row(cols ...any) {
for i, c := range cols {
if i > 0 {
rows.WriteString("\t")
}
fmt.Fprint(&rows, c)
}
rows.WriteString("\n")
}
func table() {
tw := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
tw.Write(rows.Bytes())
tw.Flush()
rows.Reset()
}
func Example_unround() {
// Output:
// x raw str
// 6 24 ⟨6.0⟩
// 6.001 25 ⟨6.0+⟩
// 6.499 25 ⟨6.0+⟩
// 6.5 26 ⟨6.5⟩
// 6.501 27 ⟨6.5+⟩
// 6.999 27 ⟨6.5+⟩
// 7 28 ⟨7.0⟩
row("x", "raw", "str")
for _, x := range []float64{6, 6.001, 6.499, 6.5, 6.501, 6.999, 7} {
u := unround(x)
row(x, uint64(u), u)
}
table()
}
func Example_unround_floor() {
// Output:
// x floor round½↓ round round½↑ ceil
// ⟨6.0⟩ 6 6 6 6 6
// ⟨6.0+⟩ 6 6 6 6 7
// ⟨6.0+⟩ 6 6 6 6 7
// ⟨6.5⟩ 6 6 6 7 7
// ⟨6.5+⟩ 6 7 7 7 7
// ⟨6.5+⟩ 6 7 7 7 7
// ⟨7.0⟩ 7 7 7 7 7
// ⟨7.5⟩ 7 7 8 8 8
// ⟨8.5⟩ 8 8 8 9 9
row("x", "floor", "round½↓", "round", "round½↑", "ceil")
for _, x := range []float64{6, 6.001, 6.499, 6.5, 6.501, 6.999, 7, 7.5, 8.5} {
u := unround(x)
row(u, u.floor(), u.roundHalfDown(), u.round(), u.roundHalfUp(), u.ceil())
}
table()
}
func Example_div() {
// Output:
// ⟨2.5+⟩ 3
u := unround(15.1).div(6)
fmt.Println(u, u.round())
}
func Example_nudge() {
// Output:
// x nudge(-1).floor floor ceil nudge(+1).ceil
// ⟨15.0⟩ 14 15 15 16
// ⟨15.0+⟩ 15 15 16 16
// ⟨15.5+⟩ 15 15 16 16
// ⟨16.0⟩ 15 16 16 17
row("x", "nudge(-1).floor", "floor", "ceil", "nudge(+1).ceil")
for _, x := range []float64{15, 15.1, 15.9, 16} {
u := unround(x)
row(u, u.nudge(-1).floor(), u.floor(), u.ceil(), u.nudge(+1).ceil())
}
table()
}