https://github.com/martcpp/nonneg-float
A generic NonNegative float wrapper with compile-time checked macro
https://github.com/martcpp/nonneg-float
Last synced: 4 months ago
JSON representation
A generic NonNegative float wrapper with compile-time checked macro
- Host: GitHub
- URL: https://github.com/martcpp/nonneg-float
- Owner: martcpp
- License: apache-2.0
- Created: 2025-05-22T02:40:50.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-22T04:11:51.000Z (about 1 year ago)
- Last Synced: 2025-07-24T11:27:49.684Z (11 months ago)
- Language: Rust
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nonneg-float
A generic Rust crate providing a wrapper for non-negative floating-point numbers with a convenient macro for safe construction.
## Features
- Generic over any floating-point type (`f32`, `f64`, etc.) implementing `num_traits::Float`.
- Ensures values are non-negative and finite.
- Macro `nonneg!` for easy, safe instantiation with optional defaulting to zero.
- Panics at runtime if negative values are used with the macro.
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
nonneg-float = "0.1.0"
num-traits = "0.2"
```
## Examples
```use nonneg_float::{NonNegative, nonneg};
fn main() {
let a = nonneg!(f64); // defaults to 0.0
let b = nonneg!(5.5f64); // from literal
let c = nonneg!(f32, 3.14); // explicit type and value
println!("{}, {}, {}", a.get(), b.get(), c.get());
}
```