iliekturtles / uom

Units of measurement -- type-safe zero-cost dimensional analysis
Apache License 2.0
989 stars 89 forks source link

Support fixed-point storage type(s) #474

Open kleinesfilmroellchen opened 2 months ago

kleinesfilmroellchen commented 2 months ago

I want to utilize fixed-point arithmetic in unit-safe code for a few reasons:

(All of these relate to my use of mostly length and velocity quantities in CNC firmware and G-Code parsing, to provide some context)

I would like to use the popular fixed crate for fixed-point arithmetic. On a feature level, the types in this crate obviously support enough arithmetic for uom to work, but as noted elsewhere, due to the Conversion<V> implementation requirement it is extremely cumbersome to implement.

Note that resolving https://github.com/iliekturtles/uom/issues/122 would also solve this issue, since then it would be more feasible for me to implement the custom storage type myself.

The best current workaround is to use ISQ! with SI quantities, integer units of sufficient size, and SI prefixed units to increase the precision of the system. For instance, while in my case I would want to use 22.10 signed fixed point (32-bit values and a precision of ~100μm when using SI base units), I can emulate a close-enough precision by using micrometers and i32 storage type.