mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-24 00:31:34 +11:00
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:
commit
d3b4e7eacf
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue