diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index 26b9d2a..78885b7 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -33,6 +33,17 @@ impl Fixed{ self.bits } #[inline] + pub const fn raw_digit(value:i64)->Self{ + let mut digits=[0u64;N]; + digits[0]=value.abs() as u64; + //sign bit + digits[N-1]|=(value&i64::MIN) as u64; + Self::from_bits(BInt::from_bits(bnum::BUint::from_digits(digits))) + } +} +impl Fixed<1,F>{ + /// My old code called this function everywhere so let's provide it + #[inline] pub const fn raw(value:i64)->Self{ Self::from_bits(BInt::from_bits(bnum::BUint::from_digit(value as u64))) } @@ -107,6 +118,40 @@ impl std::iter::Sum for Fixed{ } } +macro_rules! impl_into_float { + ( $output: ty ) => { + impl Into<$output> for Fixed{ + #[inline] + fn into(self)->$output{ + let mut total=0.0; + let bits=self.bits.to_bits(); + let digits=bits.digits(); + for (i,digit) in digits[0..N-1].iter().enumerate(){ + // (i*64-F) as i32 will interpret the highest order bit as a sign bit but whatever + total+=(*digit as $output)*(2.0 as $output).powi((i*64-F) as i32); + } + //most significant digit holds the sign bit + //assume we are using a number with at least 1 digit... + total+=((*digits.last().unwrap() as i64).abs() as $output)*(2.0 as $output).powi(((N-1)*64-F) as i32); + if self.bits.is_negative(){ + total=-total; + } + total + } + } + } +} +impl_into_float!(f32); +impl_into_float!(f64); + +impl core::fmt::Display for Fixed{ + #[inline] + fn fmt(&self,f:&mut core::fmt::Formatter)->Result<(),core::fmt::Error>{ + let float:f32=(*self).into(); + core::write!(f,"{:.3}",float) + } +} + macro_rules! impl_additive_operator { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { impl $struct{ diff --git a/linear_ops/src/macros/matrix.rs b/linear_ops/src/macros/matrix.rs index 56634b6..bc3866f 100644 --- a/linear_ops/src/macros/matrix.rs +++ b/linear_ops/src/macros/matrix.rs @@ -87,6 +87,22 @@ macro_rules! impl_matrix { ) } } + + impl core::fmt::Display for Matrix{ + #[inline] + fn fmt(&self,f:&mut core::fmt::Formatter)->Result<(),core::fmt::Error>{ + for row in &self.array[0..Y]{ + core::write!(f,"\n")?; + for elem in &row[0..X-1]{ + core::write!(f,"{}, ",elem)?; + } + // assume we will be using matrices of size 1x1 or greater + core::write!(f,"{}",row.last().unwrap())?; + } + Ok(()) + } + } + impl core::ops::Mul> for Matrix where T:core::ops::Mul+Copy, diff --git a/linear_ops/src/macros/vector.rs b/linear_ops/src/macros/vector.rs index 3802f4a..cf1714f 100644 --- a/linear_ops/src/macros/vector.rs +++ b/linear_ops/src/macros/vector.rs @@ -47,6 +47,17 @@ macro_rules! impl_vector { } } + impl core::fmt::Display for Vector{ + #[inline] + fn fmt(&self,f:&mut core::fmt::Formatter)->Result<(),core::fmt::Error>{ + for elem in &self.array[0..N-1]{ + core::write!(f,"{}, ",elem)?; + } + // assume we will be using vectors of length 1 or greater + core::write!(f,"{}",self.array.last().unwrap()) + } + } + impl Vector{ #[inline] pub fn min(self,rhs:Self)->Self{ diff --git a/linear_ops/src/matrix.rs b/linear_ops/src/matrix.rs index be59900..2c7dfb5 100644 --- a/linear_ops/src/matrix.rs +++ b/linear_ops/src/matrix.rs @@ -1,6 +1,6 @@ use crate::vector::Vector; -#[derive(Clone,Copy,Hash,Eq,PartialEq)] +#[derive(Clone,Copy,Debug,Hash,Eq,PartialEq)] pub struct Matrix{ pub(crate) array:[[T;X];Y], } diff --git a/linear_ops/src/vector.rs b/linear_ops/src/vector.rs index b5a41e5..8d223de 100644 --- a/linear_ops/src/vector.rs +++ b/linear_ops/src/vector.rs @@ -3,7 +3,7 @@ /// v.x += v.z; /// println!("v.x={}",v.x); -#[derive(Clone,Copy,Hash,Eq,PartialEq)] +#[derive(Clone,Copy,Debug,Hash,Eq,PartialEq)] pub struct Vector{ pub(crate) array:[T;N], }