Crates.io | microns |
lib.rs | microns |
version | 0.1.1 |
source | src |
created_at | 2024-12-10 14:59:34.59395 |
updated_at | 2024-12-10 15:05:18.780383 |
description | simple crate for float to fixed precision conversion |
homepage | |
repository | https://github.com/mj10021/microns |
max_upload_size | |
id | 1478591 |
size | 7,893 |
microns is a simple, dependency-free Rust library that enables working with floating-point values as fixed-precision integers. Its primary purpose is to help avoid the pitfalls of floating-point arithmetic in contexts where a fixed, micrometer-level precision is sufficient.
microns gets its name from the idea of converting millimeter-formatted floating-point values (f32
) into integer-based micrometer values (i32
). Instead of dealing with floating-point numbers directly and risking errors due to floating-point precision, you convert your f32
values into Microns
(stored internally as i32
), and then use standard arithmetic operations on these integers.
By scaling values such that 1_000 microns = 1 millimeter
, microns
makes it convenient to represent geometric coordinates, distances, and other measurements at a micron-level resolution. Although the primary use case might be for CNC machines, 3D printers, or robotics—where consistent, small increments matter—this library can be useful whenever controlled precision is required and i32::MIN < float < i32::MAX
.
Key points:
serde
for serialization).i32
integers, allowing you to consistently reason about increments of 1 micron.f32
values, returning Microns
.f32
to Microns
using Microns::from(f32)
.f32
using f32::from(microns)
.Microns
instance.works()
function ensures that the f32
value can fit in an i32
after scaling to microns.Use microns when: