1 Commits

Author SHA1 Message Date
1e83366386 assert range on truncation 2024-09-27 12:27:31 -07:00
5 changed files with 3 additions and 20 deletions

View File

@@ -1,6 +0,0 @@
[package]
name = "absrel"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@@ -1,4 +0,0 @@
/// This type represents an absolute value, such as a coordinate for a world position.
/// This is called a euclidean point in Geometric Algebra.
/// The most appropriate type here is a fixed-point value.
pub struct Absolute<T>(T);

View File

@@ -1,2 +0,0 @@
pub mod abs;
pub mod rel;

View File

@@ -1,8 +0,0 @@
/// This type represents the difference between two absolute values.
/// This is called an ideal point in Geometric Algebra.
/// After you get your time delta or position delta,
/// implement your number-crunching logic using this type.
/// Once the calculation is complete, it can be added to an absolute value
/// with the effect of offsetting the value to a new absolute value.
/// The most appropriate type here is a floating point value.
pub struct Relative<T>(T);

View File

@@ -585,6 +585,9 @@ macro_rules! impl_fix_rhs_lt_lhs_not_const_generic{
paste::item!{
#[inline]
pub fn [<fix_ $rhs>](self)->Fixed<$rhs,{$rhs*32}>{
let max=bnum::cast::As::as_::<BInt::<$lhs>>(BInt::<$rhs>::MAX).shl(($lhs-$rhs)*32);
let min=bnum::cast::As::as_::<BInt::<$lhs>>(BInt::<$rhs>::MIN).shl(($lhs-$rhs)*32);
assert!(min<self.bits&&self.bits<max,"Truncation detected");
Fixed::from_bits(bnum::cast::As::as_::<BInt::<$rhs>>(self.bits.shr(($lhs-$rhs)*32)))
}
}