Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hydrogen602/unsigned-f64
A wrapper struct for f64 to guarantee certain f64 values are non-negative using types.
https://github.com/hydrogen602/unsigned-f64
Last synced: about 2 months ago
JSON representation
A wrapper struct for f64 to guarantee certain f64 values are non-negative using types.
- Host: GitHub
- URL: https://github.com/hydrogen602/unsigned-f64
- Owner: hydrogen602
- License: mit
- Created: 2023-02-26T07:13:26.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-17T04:02:37.000Z (about 1 year ago)
- Last Synced: 2024-10-31T11:34:01.101Z (2 months ago)
- Language: Rust
- Size: 20.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# UnsignedF64
A library to add the equivalent of unsigned ints to floats by making a `UnsignedF64` that can't be negative. This idea is for cases like simulation code, where bugs are hard to debug and types don't add much safety (Since everyting is a f64).
**For a more complete feature set, see [typed_floats](https://crates.io/crates/typed_floats).**
In order to address issues with `-0.0` being able to turn into negative numbers, `-0.0` will be converted to `0.0` when calling `UnsignedF64::new`.
## How To
Create new unsigned floats using `UnsignedF64::new`. This will check if the `f64` is negative or not and return an `Option`. Many `f64` methods are implemented on `UnsignedF64` so that numbers don't have to be rechecked when non-negativeness is guaranteed, e.g. the square root of an `UnsignedF64` is guaranteed to be non-negative, so `sqrt` returns `UnsignedF64`.
```Rust
// An example of using the UnsignedF64 type.let point1 = (3., 4.);
let point2 = (5., 12.);fn distance(p1: (f64, f64), p2: (f64, f64)) -> UnsignedF64 {
let x = p1.0 - p2.0;
let y = p1.1 - p2.1;
(UnsignedF64::square(x) + UnsignedF64::square(y)).sqrt()
}let d = distance(point1, point2);
println!(
"The distance between {:?} and {:?} is {}",
point1, point2, d
);
```## ToDo
- Implement the methods where I'm not sure if they can return negative numbers or not
- Implement `std::ops::Rem` for `UnsignedF64`
- Implement traits for `&UnsignedF64`
- Implement serde's Serialize & Deserialize (using features?)