Skip to content

Commit ad47bbf

Browse files
committed
feat: type/number: add Clamp255Float64(), Utoi()
1 parent 8eb1ed3 commit ad47bbf

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

type/number/constraints.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,14 @@ type Signed interface {
1212
type Unsigned interface {
1313
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
1414
}
15+
16+
// Clamp255Float64 ensures values are within 0–255 range
17+
func Clamp255Float64(v float64) float64 {
18+
if v < 0 {
19+
return 0
20+
}
21+
if v > 255 {
22+
return 255
23+
}
24+
return v
25+
}

type/number/numconv.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,11 @@ func U64toi64(u uint64) (int64, error) {
114114
return int64(u), nil
115115
}
116116
}
117+
118+
func Utoi(u uint) (int, error) {
119+
if u > math.MaxInt {
120+
return 0, errorsutil.Wrapf(ErrOverflow, "uint value (%d) overflows int", u)
121+
} else {
122+
return int(u), nil
123+
}
124+
}

0 commit comments

Comments
 (0)