Add the ability to change the base of a fixnum

This commit is contained in:
Gwilym Kuiper 2021-06-05 16:34:31 +01:00
parent 18e017ae73
commit fe0e9f8196

View file

@ -6,6 +6,14 @@ use core::{
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct Num<const N: usize>(i32);
fn change_base<const N: usize, const M: usize>(num: Num<N>) -> Num<M> {
if N < M {
Num(num.0 << (M - N))
} else {
Num(num.0 >> (N - M))
}
}
impl<const N: usize> From<i32> for Num<N> {
fn from(value: i32) -> Self {
Num(value << N)
@ -166,6 +174,15 @@ fn test_division_by_2_and_15(_gba: &mut super::Gba) {
}
}
#[test_case]
fn test_change_base(_gba: &mut super::Gba) {
let two: Num<9> = 2.into();
let three: Num<4> = 3.into();
assert_eq!(two + change_base(three), 5.into());
assert_eq!(three + change_base(two), 5.into());
}
impl<const N: usize> Display for Num<N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let integral = self.0 >> N;