From 1bb05560cb8e9ec667da8f587ef35cca24c161c5 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sat, 5 Jun 2021 23:50:49 +0100 Subject: [PATCH] Make it so that multiplication doesn't overflow so easily --- agb/src/number.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/agb/src/number.rs b/agb/src/number.rs index 5f30e597..13fbbdf8 100644 --- a/agb/src/number.rs +++ b/agb/src/number.rs @@ -145,7 +145,11 @@ where { type Output = Self; fn mul(self, rhs: T) -> Self::Output { - Num((self.0 * rhs.into().0) >> N) + let rhs: Self = rhs.into(); + + Num(((self.floor() * rhs.floor()) << N) + + (self.floor() * rhs.frac() + rhs.floor() * self.frac()) + + ((self.frac() * rhs.frac()) >> N)) } } @@ -245,6 +249,10 @@ impl Num { self.0 >> N } + pub fn frac(&self) -> I { + self.0 & ((I::one() << N) - I::one()) + } + pub fn new(integral: I) -> Self { Self(integral << N) }