Files
obitools4/autodoc/docmd/pkg/obiutils/abs.md
T
Eric Coissac 8c7017a99d ⬆️ version bump to v4.5
- Update obioptions.Version from "Release 4.4.29" to "/v/ Release v5"
- Update version.txt from 4.29 → .30
(automated by Makefile)
2026-04-13 13:34:53 +02:00

42 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# `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
```go
func Abs[T constraints.Signed](x T) T
```
- **Generic constraint**: `T` must satisfy `constraints.Signed`, i.e., any signed integer type (`int`, `int8``int64`) or floating-point type (via future Go versions supporting floats in `constraints.Signed`).
- **Input**: A value of type `T`.
- **Output**: The absolute (non-negative) counterpart, same type as input.
## Semantics
- Returns `x` if `x ≥ 0`.
- Otherwise, returns `-x`, effectively flipping the sign.
- Handles all signed numeric types uniformly — no need for type-specific overloads.
## Example Usage
```go
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 Gos standard library conventions (e.g., similar to `math.Abs` but *generic* and not limited to floats).
## Limitations
- Does **not** support unsigned types (by design: `constraints.Signed` excludes them).
- For floating-point special cases (`NaN`, `-0.0`) behavior matches native negation semantics.
## Dependencies
- Requires `golang.org/x/exp/constraints` for the generic type constraint.