generic_fixedpoint

Crates.iogeneric_fixedpoint
lib.rsgeneric_fixedpoint
version0.1.1
sourcesrc
created_at2024-04-14 00:34:01.935161
updated_at2024-04-14 00:34:46.76659
descriptionGeneric fixed-point numbers.
homepage
repositoryhttps://gitgud.io/deltanedas/fixedpoint
max_upload_size
id1207943
size11,059
(deltanedas)

documentation

README

fixedpoint

Fixed-point numbers with an underlying type and precision divisor.

Generally this is a power of 10 but you can do other things, like 256 to use a lower byte etc. Presumably the compiler optimises operations to use shifting instead of division/multiplication here.

// use it directly
let x = Fixed<u8, 10>::from(5.3f64);
println!("{}", x * 2); // 10.6

// have a wrapper that controls what operations can be done
// useful for units of measurement that have special semantics
// for example, multiplying lengths gives an area.
struct Length(Fixed<u16, 100>);
struct Area(Fixed<u32, 100>);

impl Mul for Length {
	type Output = Area;

	fn mul(self, rhs: Self) -> Area {
		Area((self.0 * rhs.0).into())
	}
}

Commit count: 0

cargo fmt