• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

enetx / g / 18078749822

28 Sep 2025 07:21PM UTC coverage: 91.157% (-0.6%) from 91.734%
18078749822

push

github

enetx
ref mapord

196 of 254 new or added lines in 9 files covered. (77.17%)

14 existing lines in 4 files now uncovered.

6319 of 6932 relevant lines covered (91.16%)

633.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.79
/float.go
1
package g
2

3
import (
4
        "encoding/binary"
5
        "fmt"
6
        "math"
7
        "math/big"
8
        "strconv"
9

10
        "github.com/enetx/g/cmp"
11
        "github.com/enetx/g/constraints"
12
)
13

14
// NewFloat creates a new Float with the provided value.
15
func NewFloat[T constraints.Float | constraints.Integer](float T) Float { return Float(float) }
1✔
16

17
// Transform applies a transformation function to the Float and returns the result.
18
func (f Float) Transform(fn func(Float) Float) Float { return fn(f) }
2✔
19

20
// BytesBE returns the IEEE-754 representation of the Float as Bytes in BigEndian order.
21
// The Float is converted to its 64-bit IEEE-754 binary representation.
22
func (f Float) BytesBE() Bytes {
73✔
23
        var buf [8]byte
73✔
24
        bits := math.Float64bits(float64(f))
73✔
25
        binary.BigEndian.PutUint64(buf[:], bits)
73✔
26

73✔
27
        return Bytes(buf[:])
73✔
28
}
73✔
29

30
// BytesLE returns the IEEE-754 representation of the Float as Bytes in LittleEndian order.
31
// The Float is converted to its 64-bit IEEE-754 binary representation.
32
func (f Float) BytesLE() Bytes {
73✔
33
        var buf [8]byte
73✔
34
        bits := math.Float64bits(float64(f))
73✔
35
        binary.LittleEndian.PutUint64(buf[:], bits)
73✔
36

73✔
37
        return Bytes(buf[:])
73✔
38
}
73✔
39

40
// Min returns the minimum of two Floats.
41
func (f Float) Min(b ...Float) Float { return cmp.Min(append(b, f)...) }
1✔
42

43
// Max returns the maximum of two Floats.
44
func (f Float) Max(b ...Float) Float { return cmp.Max(append(b, f)...) }
1✔
45

46
// Sqrt returns the square root of the Float.
47
//
48
// Returns:
49
//   - Float: The square root of the Float. If the Float is negative, the result is NaN.
50
//
51
// Example usage:
52
//
53
//        f := g.NewFloat(9)
54
//        result := f.Sqrt() // result = 3
NEW
55
func (f Float) Sqrt() Float { return Float(math.Sqrt(f.Std())) }
×
56

57
// Pow raises the Float to the power of the given exponent.
58
//
59
// Parameters:
60
//   - exp (Float): The exponent to raise the Float to.
61
//
62
// Returns:
63
//   - Float: The result of raising the Float to the specified power.
64
//
65
// Example usage:
66
//
67
//        f := g.NewFloat(2)
68
//        result := f.Pow(3) // result = 8
NEW
69
func (f Float) Pow(exp Float) Float { return Float(math.Pow(f.Std(), exp.Std())) }
×
70

71
// Mod returns the floating-point remainder of f / b as defined by IEEE 754.
72
//
73
// Parameters:
74
//   - b (Float): The divisor.
75
//
76
// Returns:
77
//   - Float: The remainder of f / b.
78
//
79
// Example usage:
80
//
81
//        f := g.NewFloat(5.5)
82
//        result := f.Mod(2) // result = 1.5
NEW
83
func (f Float) Mod(b Float) Float { return Float(math.Mod(f.Std(), b.Std())) }
×
84

85
// Abs returns the absolute value of the Float.
86
func (f Float) Abs() Float { return Float(math.Abs(f.Std())) }
3✔
87

88
// Add adds two Floats and returns the result.
89
func (f Float) Add(b Float) Float { return f + b }
2✔
90

91
// BigFloat returns the Float as a *big.Float.
92
func (f Float) BigFloat() *big.Float { return big.NewFloat(f.Std()) }
3✔
93

94
// Cmp compares two Floats and returns an cmp.Ordering.
95
func (f Float) Cmp(b Float) cmp.Ordering { return cmp.Cmp(f, b) }
51✔
96

97
// Div divides two Floats and returns the result.
98
func (f Float) Div(b Float) Float { return f / b }
1✔
99

100
// IsZero checks if the Float is 0.
101
func (f Float) IsZero() bool { return f.Eq(0) }
3✔
102

103
// Eq checks if two Floats are equal.
104
func (f Float) Eq(b Float) bool { return f.Cmp(b).IsEq() }
39✔
105

106
// Std returns the Float as a float64.
107
func (f Float) Std() float64 { return float64(f) }
66✔
108

109
// Gt checks if the Float is greater than the specified Float.
110
func (f Float) Gt(b Float) bool { return f.Cmp(b).IsGt() }
4✔
111

112
// Int returns the Float as an Int.
113
func (f Float) Int() Int { return Int(f) }
3✔
114

115
// String returns the Float as an String.
116
func (f Float) String() String { return String(strconv.FormatFloat(f.Std(), 'g', -1, 64)) }
3✔
117

118
// Lt checks if the Float is less than the specified Float.
119
func (f Float) Lt(b Float) bool { return f.Cmp(b).IsLt() }
4✔
120

121
// Mul multiplies two Floats and returns the result.
122
func (f Float) Mul(b Float) Float { return f * b }
22✔
123

124
// Ne checks if two Floats are not equal.
125
func (f Float) Ne(b Float) bool { return !f.Eq(b) }
32✔
126

127
// Round rounds the Float to the nearest integer and returns the result as an Int.
128
func (f Float) Round() Int { return Int(math.Round(f.Std())) }
7✔
129

130
// RoundDecimal rounds the Float value to the specified number of decimal places.
131
//
132
// Parameters:
133
//   - precision (Int): The number of decimal places to round to. If negative, the Float is returned unchanged.
134
//     Values greater than 308 are capped at 308 to prevent overflow.
135
//
136
// Returns:
137
//   - Float: A new Float value rounded to the specified number of decimal places.
138
func (f Float) RoundDecimal(precision Int) Float {
31✔
139
        if precision < 0 {
32✔
140
                return f
1✔
141
        }
1✔
142

143
        if precision > 308 { // 10^309 == +Inf
30✔
NEW
144
                precision = 308
×
NEW
145
        }
×
146

147
        pow := math.Pow(10, float64(precision))
30✔
148

30✔
149
        return Float(math.Round(f.Std()*pow) / pow)
30✔
150
}
151

152
// TruncDecimal truncates the Float value to the specified number of decimal places.
153
//
154
// Parameters:
155
//   - precision (Int): The number of decimal places to truncate to. If negative, the Float is returned unchanged.
156
//     Values greater than 308 are capped at 308.
157
//
158
// Returns:
159
//   - Float: A new Float value truncated to the specified number of decimal places.
NEW
160
func (f Float) TruncDecimal(precision Int) Float {
×
NEW
161
        if precision < 0 {
×
NEW
162
                return f
×
NEW
163
        }
×
164

NEW
165
        if precision > 308 {
×
NEW
166
                precision = 308
×
NEW
167
        }
×
168

NEW
169
        pow := math.Pow(10, float64(precision))
×
NEW
170

×
NEW
171
        return Float(math.Trunc(f.Std()*pow) / pow)
×
172
}
173

174
// CeilDecimal rounds the Float value up (towards +Inf) to the specified number of decimal places.
175
//
176
// Parameters:
177
//   - precision (Int): The number of decimal places to round up to. If negative, the Float is returned unchanged.
178
//     Values greater than 308 are capped at 308.
179
//
180
// Returns:
181
//   - Float: A new Float value rounded up to the specified number of decimal places.
NEW
182
func (f Float) CeilDecimal(precision Int) Float {
×
NEW
183
        if precision < 0 {
×
NEW
184
                return f
×
NEW
185
        }
×
186

NEW
187
        if precision > 308 {
×
NEW
188
                precision = 308
×
NEW
189
        }
×
190

NEW
191
        pow := math.Pow(10, float64(precision))
×
NEW
192

×
NEW
193
        return Float(math.Ceil(f.Std()*pow) / pow)
×
194
}
195

196
// FloorDecimal rounds the Float value down (towards -Inf) to the specified number of decimal places.
197
//
198
// Parameters:
199
//   - precision (Int): The number of decimal places to round down to. If negative, the Float is returned unchanged.
200
//     Values greater than 308 are capped at 308.
201
//
202
// Returns:
203
//   - Float: A new Float value rounded down to the specified number of decimal places.
NEW
204
func (f Float) FloorDecimal(precision Int) Float {
×
UNCOV
205
        if precision < 0 {
×
UNCOV
206
                return f
×
UNCOV
207
        }
×
208

NEW
209
        if precision > 308 {
×
NEW
210
                precision = 308
×
NEW
211
        }
×
212

NEW
213
        pow := math.Pow(10, float64(precision))
×
UNCOV
214

×
NEW
215
        return Float(math.Floor(f.Std()*pow) / pow)
×
216
}
217

218
// Sub subtracts two Floats and returns the result.
219
func (f Float) Sub(b Float) Float { return f - b }
22✔
220

221
// Bits returns IEEE-754 representation of f.
222
func (f Float) Bits() uint64 { return math.Float64bits(f.Std()) }
10✔
223

224
// Float32 returns the Float as a float32.
225
func (f Float) Float32() float32 { return float32(f) }
3✔
226

227
// Print writes the value of the Float to the standard output (console)
228
// and returns the Float unchanged.
229
func (f Float) Print() Float { fmt.Print(f); return f }
1✔
230

231
// Println writes the value of the Float to the standard output (console) with a newline
232
// and returns the Float unchanged.
233
func (f Float) Println() Float { fmt.Println(f); return f }
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc