/
float.go
173 lines (132 loc) · 3.81 KB
/
float.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package mathx
import "math/big"
// Float is an immutable arbitrary-precision floating-point type, wrapping
// the built-in math/big.Float (which is mutable). This package
// also has a simpler, two-argument API. For example:
//
// a := mathx.NewFloat(0.0)
// b := a.AddFloat64(123.0)
type Float big.Float
func NewFloat(x float64) *Float {
return (*Float)(big.NewFloat(x))
}
func ParseFloat(s string, base int, prec uint, mode big.RoundingMode) (*Float, int, error) {
f, b, err := big.ParseFloat(s, base, prec, mode)
return (*Float)(f), b, err
}
func (z *Float) Abs() *Float {
return (*Float)(new(big.Float).Abs((*big.Float)(z)))
}
func (z *Float) Acc() big.Accuracy {
return (*big.Float)(z).Acc()
}
func (z *Float) Add(y *Float) *Float {
return (*Float)(new(big.Float).Add((*big.Float)(z), (*big.Float)(y)))
}
func (z *Float) Cmp(y *Float) int {
return (*big.Float)(z).Cmp((*big.Float)(y))
}
func (z *Float) copy() *Float {
return (*Float)(new(big.Float).Set((*big.Float)(z)))
}
func (z *Float) Float32() (float32, big.Accuracy) {
return (*big.Float)(z).Float32()
}
func (z *Float) Float64() (float64, big.Accuracy) {
return (*big.Float)(z).Float64()
}
// func (z *Float) Format(s fmt.State, format rune) {
// (*big.Float)(z).Format(s, format)
// }
func (z *Float) Int() (*Int, big.Accuracy) {
i := new(big.Int)
i, acc := (*big.Float)(z).Int(i)
return (*Int)(i), acc
}
func (z *Float) Int64() (int64, big.Accuracy) {
return (*big.Float)(z).Int64()
}
func (z *Float) IsInf() bool {
return (*big.Float)(z).IsInf()
}
func (z *Float) IsInt() bool {
return (*big.Float)(z).IsInt()
}
func (z *Float) MantExp() (*Float, int) {
y := new(big.Float)
exp := (*big.Float)(z).MantExp(y)
return (*Float)(y), exp
}
func (z *Float) MarshalText() (text []byte, err error) {
return (*big.Float)(z).MarshalText()
}
func (z *Float) MinPrec() uint {
return (*big.Float)(z).MinPrec()
}
func (z *Float) Mode() big.RoundingMode {
return (*big.Float)(z).Mode()
}
func (z *Float) Mul(y *Float) *Float {
return (*Float)(new(big.Float).Mul((*big.Float)(z), (*big.Float)(y)))
}
func (z *Float) Neg() *Float {
return (*Float)(new(big.Float).Neg((*big.Float)(z)))
}
// func (z *Float) Parse(s string, base int) (f *Float, b int, err error) {
// }
func (z *Float) Prec() uint {
return (*big.Float)(z).Prec()
}
func (z *Float) Quo(y *Float) *Float {
return (*Float)(new(big.Float).Quo((*big.Float)(z), (*big.Float)(y)))
}
func (z *Float) Rat(r *big.Rat) (*big.Rat, big.Accuracy) {
return (*big.Float)(z).Rat(r)
}
// func (z *Float) SetInf(signbit bool) *Float {
// }
//
//
func FloatFromInt64(x int64) *Float {
return (*Float)(new(big.Float).SetInt64(x))
}
func (z *Float) SetExp(exp int) *Float {
return (*Float)(new(big.Float).SetMantExp((*big.Float)(z), exp))
}
func (z *Float) SetMode(mode big.RoundingMode) *Float {
return (*Float)((*big.Float)(z.copy()).SetMode(mode))
}
func (z *Float) SetPrec(prec uint) *Float {
return (*Float)((*big.Float)(z.copy()).SetPrec(prec))
}
func FloatFromRat(x *big.Rat) *Float {
return (*Float)(new(big.Float).SetRat(x))
}
func (z *Float) SetString(s string) (*Float, bool) {
f, b := new(big.Float).SetString(s)
return (*Float)(f), b
}
func FloatFromUint64(x uint64) *Float {
return (*Float)(new(big.Float).SetUint64(x))
}
func (z *Float) Sign() int {
return (*big.Float)(z).Sign()
}
func (z *Float) Signbit() bool {
return (*big.Float)(z).Signbit()
}
func (z *Float) String() string {
return (*big.Float)(z).String()
}
func (z *Float) Sub(y *Float) *Float {
return (*Float)(new(big.Float).Sub((*big.Float)(z), (*big.Float)(y)))
}
func (z *Float) Text(format byte, prec int) string {
return (*big.Float)(z).Text(format, prec)
}
func (z *Float) Uint64() (uint64, big.Accuracy) {
return (*big.Float)(z).Uint64()
}
func (z *Float) UnmarshalText(text []byte) error {
return (*big.Float)(z).UnmarshalText(text)
}