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

enetx / g / 14661178371

25 Apr 2025 09:14AM UTC coverage: 86.248% (-0.2%) from 86.478%
14661178371

push

github

enetx
ref

124 of 174 new or added lines in 13 files covered. (71.26%)

1 existing line in 1 file now uncovered.

3462 of 4014 relevant lines covered (86.25%)

127.49 hits per line

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

92.0
/float.go
1
package g
2

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

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

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

18
// Ptr returns a pointer to the current Float value.
19
func (f Float) Ptr() *Float { return &f }
1✔
20

21
// Transform applies a transformation function to the Float and returns the result.
22
func (f Float) Transform(fn func(Float) Float) Float { return fn(f) }
2✔
23

24
// Bytes returns the Float as a byte slice.
25
func (f Float) Bytes() Bytes {
8✔
26
        buffer := make([]byte, 8)
8✔
27
        binary.BigEndian.PutUint64(buffer, f.UInt64())
8✔
28

8✔
29
        return buffer[bits.LeadingZeros64(f.UInt64())>>3:]
8✔
30
}
8✔
31

32
// Min returns the minimum of two Floats.
33
func (f Float) Min(b ...Float) Float { return cmp.Min(append(b, f)...) }
1✔
34

35
// Max returns the maximum of two Floats.
36
func (f Float) Max(b ...Float) Float { return cmp.Max(append(b, f)...) }
1✔
37

38
// Abs returns the absolute value of the Float.
39
func (f Float) Abs() Float { return Float(math.Abs(f.Std())) }
7✔
40

41
// Add adds two Floats and returns the result.
42
func (f Float) Add(b Float) Float { return f + b }
2✔
43

44
// BigFloat returns the Float as a *big.Float.
45
func (f Float) BigFloat() *big.Float { return big.NewFloat(f.Std()) }
3✔
46

47
// Cmp compares two Floats and returns an cmp.Ordering.
48
func (f Float) Cmp(b Float) cmp.Ordering { return cmp.Cmp(f, b) }
51✔
49

50
// Div divides two Floats and returns the result.
51
func (f Float) Div(b Float) Float { return f / b }
×
52

53
// IsZero checks if the Float is 0.
54
func (f Float) IsZero() bool { return f.Eq(0) }
3✔
55

56
// Eq checks if two Floats are equal.
57
func (f Float) Eq(b Float) bool { return f.Cmp(b).IsEq() }
39✔
58

59
// Std returns the Float as a float64.
60
func (f Float) Std() float64 { return float64(f) }
29✔
61

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

65
// Int returns the Float as an Int.
66
func (f Float) Int() Int { return Int(f) }
7✔
67

68
// String returns the Float as an String.
69
func (f Float) String() String { return String(strconv.FormatFloat(f.Std(), 'f', -1, 64)) }
3✔
70

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

74
// Mul multiplies two Floats and returns the result.
75
func (f Float) Mul(b Float) Float { return f * b }
22✔
76

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

80
// Round rounds the Float to the nearest integer and returns the result as an Int.
81
func (f Float) Round() Int {
7✔
82
        if f >= 0 {
11✔
83
                return Int(f + 0.5)
4✔
84
        }
4✔
85

86
        return Int(f - 0.5)
3✔
87
}
88

89
// RoundDecimal rounds the Float value to the specified number of decimal places.
90
//
91
// The function takes the number of decimal places (precision) as an argument and returns a new
92
// Float value rounded to that number of decimals. This is achieved by multiplying the Float
93
// value by a power of 10 equal to the desired precision, rounding the result, and then dividing
94
// the rounded result by the same power of 10.
95
//
96
// Parameters:
97
//
98
// - precision (Int): The number of decimal places to round the Float value to.
99
//
100
// Returns:
101
//
102
// - Float: A new Float value rounded to the specified number of decimal places.
103
//
104
// Example usage:
105
//
106
//        f := g.Float(3.14159)
107
//        rounded := f.RoundDecimal(2) // rounded will be 3.14
108
func (f Float) RoundDecimal(precision Int) Float {
31✔
109
        if precision < 0 {
32✔
110
                return f
1✔
111
        }
1✔
112

113
        mult := 1
30✔
114
        for range precision {
90✔
115
                mult *= 10
60✔
116
        }
60✔
117

118
        result := f * Float(mult)
30✔
119
        if result >= 0 {
59✔
120
                result += 0.5
29✔
121
        } else {
30✔
122
                result -= 0.5
1✔
123
        }
1✔
124

125
        return Float(int(result)) / Float(mult)
30✔
126
}
127

128
// Sub subtracts two Floats and returns the result.
129
func (f Float) Sub(b Float) Float { return f - b }
22✔
130

131
// UInt64 returns the Float as a uint64.
132
func (f Float) UInt64() uint64 { return math.Float64bits(f.Std()) }
16✔
133

134
// Float32 returns the Float as a float32.
135
func (f Float) Float32() float32 { return float32(f) }
3✔
136

137
// Print writes the value of the Float to the standard output (console)
138
// and returns the Float unchanged.
NEW
139
func (f Float) Print() Float { fmt.Print(f); return f }
×
140

141
// Println writes the value of the Float to the standard output (console) with a newline
142
// and returns the Float unchanged.
NEW
143
func (f Float) Println() Float { fmt.Println(f); return f }
×
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