Merge pull request #42 from gwilymk/add-ability-to-change-fixnum-base

Add the ability to change the base of a fixnum
This commit is contained in:
Corwin 2021-06-05 16:38:44 +01:00 committed by GitHub
commit d3b4e7eacf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,14 @@ use core::{
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct Num<const N: usize>(i32);
pub 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;