mirror of
https://github.com/metabarcoding/obitools4.git
synced 2026-04-30 12:00:39 +00:00
8c7017a99d
- Update obioptions.Version from "Release 4.4.29" to "/v/ Release v5" - Update version.txt from 4.29 → .30 (automated by Makefile)
1.4 KiB
1.4 KiB
obiutils.Abs — Generic Absolute Value Function
This package provides a type-generic utility function for computing the absolute value of signed numeric types in Go.
Function Signature
func Abs[T constraints.Signed](x T) T
- Generic constraint:
Tmust satisfyconstraints.Signed, i.e., any signed integer type (int,int8–int64) or floating-point type (via future Go versions supporting floats inconstraints.Signed). - Input: A value of type
T. - Output: The absolute (non-negative) counterpart, same type as input.
Semantics
- Returns
xifx ≥ 0. - Otherwise, returns
-x, effectively flipping the sign. - Handles all signed numeric types uniformly — no need for type-specific overloads.
Example Usage
absInt := obiutils.Abs(-5) // → 5 (type: int)
absFloat64 := obiutils.Abs(-3.14) // → 3.14 (type: float64)
Design Rationale
- Leverages Go generics for reusability and type safety.
- Avoids duplication across
AbsInt,AbsFloat64, etc. - Follows Go’s standard library conventions (e.g., similar to
math.Absbut generic and not limited to floats).
Limitations
- Does not support unsigned types (by design:
constraints.Signedexcludes them). - For floating-point special cases (
NaN,-0.0) behavior matches native negation semantics.
Dependencies
- Requires
golang.org/x/exp/constraintsfor the generic type constraint.