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

enetx / g / 14821882949

04 May 2025 01:51PM UTC coverage: 87.23% (+0.9%) from 86.333%
14821882949

push

github

enetx
unsafe ref

4 of 4 new or added lines in 1 file covered. (100.0%)

74 existing lines in 10 files now uncovered.

3347 of 3837 relevant lines covered (87.23%)

132.15 hits per line

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

91.84
/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
// Transform applies a transformation function to the Float and returns the result.
19
func (f Float) Transform(fn func(Float) Float) Float { return fn(f) }
2✔
20

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

8✔
26
        return buffer[bits.LeadingZeros64(f.UInt64())>>3:]
8✔
27
}
8✔
28

29
// Min returns the minimum of two Floats.
30
func (f Float) Min(b ...Float) Float { return cmp.Min(append(b, f)...) }
1✔
31

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

35
// Abs returns the absolute value of the Float.
36
func (f Float) Abs() Float { return Float(math.Abs(f.Std())) }
7✔
37

38
// Add adds two Floats and returns the result.
39
func (f Float) Add(b Float) Float { return f + b }
2✔
40

41
// BigFloat returns the Float as a *big.Float.
42
func (f Float) BigFloat() *big.Float { return big.NewFloat(f.Std()) }
3✔
43

44
// Cmp compares two Floats and returns an cmp.Ordering.
45
func (f Float) Cmp(b Float) cmp.Ordering { return cmp.Cmp(f, b) }
51✔
46

47
// Div divides two Floats and returns the result.
UNCOV
48
func (f Float) Div(b Float) Float { return f / b }
×
49

50
// IsZero checks if the Float is 0.
51
func (f Float) IsZero() bool { return f.Eq(0) }
3✔
52

53
// Eq checks if two Floats are equal.
54
func (f Float) Eq(b Float) bool { return f.Cmp(b).IsEq() }
39✔
55

56
// Std returns the Float as a float64.
57
func (f Float) Std() float64 { return float64(f) }
29✔
58

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

62
// Int returns the Float as an Int.
63
func (f Float) Int() Int { return Int(f) }
7✔
64

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

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

71
// Mul multiplies two Floats and returns the result.
72
func (f Float) Mul(b Float) Float { return f * b }
22✔
73

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

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

83
        return Int(f - 0.5)
3✔
84
}
85

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

110
        mult := 1
30✔
111
        for range precision {
90✔
112
                mult *= 10
60✔
113
        }
60✔
114

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

122
        return Float(int(result)) / Float(mult)
30✔
123
}
124

125
// Sub subtracts two Floats and returns the result.
126
func (f Float) Sub(b Float) Float { return f - b }
22✔
127

128
// UInt64 returns the Float as a uint64.
129
func (f Float) UInt64() uint64 { return math.Float64bits(f.Std()) }
16✔
130

131
// Float32 returns the Float as a float32.
132
func (f Float) Float32() float32 { return float32(f) }
3✔
133

134
// Print writes the value of the Float to the standard output (console)
135
// and returns the Float unchanged.
UNCOV
136
func (f Float) Print() Float { fmt.Print(f); return f }
×
137

138
// Println writes the value of the Float to the standard output (console) with a newline
139
// and returns the Float unchanged.
UNCOV
140
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