mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-11 09:31:34 +11:00
Add the ability to change the base of a fixnum
This commit is contained in:
parent
18e017ae73
commit
fe0e9f8196
|
@ -6,6 +6,14 @@ use core::{
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
pub struct Num<const N: usize>(i32);
|
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> {
|
impl<const N: usize> From<i32> for Num<N> {
|
||||||
fn from(value: i32) -> Self {
|
fn from(value: i32) -> Self {
|
||||||
Num(value << N)
|
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> {
|
impl<const N: usize> Display for Num<N> {
|
||||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
let integral = self.0 >> N;
|
let integral = self.0 >> N;
|
||||||
|
|
Loading…
Reference in a new issue