implement Debug + Display
This commit is contained in:
parent
7b78338c76
commit
9f77531995
@ -33,6 +33,17 @@ impl<const N:usize,const F:usize> Fixed<N,F>{
|
|||||||
self.bits
|
self.bits
|
||||||
}
|
}
|
||||||
#[inline]
|
#[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<const F:usize> Fixed<1,F>{
|
||||||
|
/// My old code called this function everywhere so let's provide it
|
||||||
|
#[inline]
|
||||||
pub const fn raw(value:i64)->Self{
|
pub const fn raw(value:i64)->Self{
|
||||||
Self::from_bits(BInt::from_bits(bnum::BUint::from_digit(value as u64)))
|
Self::from_bits(BInt::from_bits(bnum::BUint::from_digit(value as u64)))
|
||||||
}
|
}
|
||||||
@ -107,6 +118,40 @@ impl<const N:usize,const F:usize> std::iter::Sum for Fixed<N,F>{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! impl_into_float {
|
||||||
|
( $output: ty ) => {
|
||||||
|
impl<const N:usize,const F:usize> Into<$output> for Fixed<N,F>{
|
||||||
|
#[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<const N:usize,const F:usize> core::fmt::Display for Fixed<N,F>{
|
||||||
|
#[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 {
|
macro_rules! impl_additive_operator {
|
||||||
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
||||||
impl<const N:usize,const F:usize> $struct<N,F>{
|
impl<const N:usize,const F:usize> $struct<N,F>{
|
||||||
|
@ -87,6 +87,22 @@ macro_rules! impl_matrix {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<const X:usize,const Y:usize,T:core::fmt::Display> core::fmt::Display for Matrix<X,Y,T>{
|
||||||
|
#[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<const X:usize,const Y:usize,const Z:usize,T,U,V> core::ops::Mul<Matrix<Z,X,U>> for Matrix<X,Y,T>
|
impl<const X:usize,const Y:usize,const Z:usize,T,U,V> core::ops::Mul<Matrix<Z,X,U>> for Matrix<X,Y,T>
|
||||||
where
|
where
|
||||||
T:core::ops::Mul<U,Output=V>+Copy,
|
T:core::ops::Mul<U,Output=V>+Copy,
|
||||||
|
@ -47,6 +47,17 @@ macro_rules! impl_vector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<const N:usize,T:core::fmt::Display> core::fmt::Display for Vector<N,T>{
|
||||||
|
#[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<const N:usize,T:Ord> Vector<N,T>{
|
impl<const N:usize,T:Ord> Vector<N,T>{
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn min(self,rhs:Self)->Self{
|
pub fn min(self,rhs:Self)->Self{
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::vector::Vector;
|
use crate::vector::Vector;
|
||||||
|
|
||||||
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
|
#[derive(Clone,Copy,Debug,Hash,Eq,PartialEq)]
|
||||||
pub struct Matrix<const X:usize,const Y:usize,T>{
|
pub struct Matrix<const X:usize,const Y:usize,T>{
|
||||||
pub(crate) array:[[T;X];Y],
|
pub(crate) array:[[T;X];Y],
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
/// v.x += v.z;
|
/// v.x += v.z;
|
||||||
/// println!("v.x={}",v.x);
|
/// println!("v.x={}",v.x);
|
||||||
|
|
||||||
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
|
#[derive(Clone,Copy,Debug,Hash,Eq,PartialEq)]
|
||||||
pub struct Vector<const N:usize,T>{
|
pub struct Vector<const N:usize,T>{
|
||||||
pub(crate) array:[T;N],
|
pub(crate) array:[T;N],
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user