diff --git a/agb-fixnum/Cargo.toml b/agb-fixnum/Cargo.toml index 8300c331..f48756dc 100644 --- a/agb-fixnum/Cargo.toml +++ b/agb-fixnum/Cargo.toml @@ -8,3 +8,4 @@ repository = "https://github.com/agbrs/agb" [dependencies] agb_macros = { version = "0.18.1", path = "../agb-macros" } +num = { version = "0.4", default-features = false } diff --git a/agb-fixnum/src/lib.rs b/agb-fixnum/src/lib.rs index 8b0d1b0c..9d912bff 100644 --- a/agb-fixnum/src/lib.rs +++ b/agb-fixnum/src/lib.rs @@ -170,6 +170,39 @@ fixed_width_signed_integer_impl!(i32); #[repr(transparent)] pub struct Num(I); +impl num::Zero for Num { + fn zero() -> Self { + Self::new(I::zero()) + } + + fn is_zero(&self) -> bool { + self.to_raw() == I::zero() + } +} + +impl num::One for Num { + fn one() -> Self { + Self::new(I::one()) + } +} + +impl num::Num for Num { + type FromStrRadixErr = ::FromStrRadixErr; + + fn from_str_radix(str: &str, radix: u32) -> Result { + // for some reason, if I don't have this it's an error, and if I do it is unused + #[allow(unused_imports)] + use num::traits::float::FloatCore; + + let v: f64 = f64::from_str_radix(str, radix)?; + + let integer = v.trunc(); + let fractional = v.fract() * (1u64 << 30) as f64; + + Ok(Self::new_from_parts((integer as i32, fractional as i32))) + } +} + /// An often convenient representation for the Game Boy Advance using word sized /// internal representation for maximum efficiency pub type FixedNum = Num; @@ -1109,6 +1142,7 @@ mod tests { use super::*; use alloc::format; + use num::Num as _; #[test] fn formats_whole_numbers_correctly() { @@ -1426,6 +1460,37 @@ mod tests { ); } + #[test] + fn test_str_radix() { + use alloc::string::ToString; + + macro_rules! str_radix_test { + ($val:tt) => { + assert_eq!( + Num::::from_str_radix(stringify!($val), 10).unwrap(), + num!($val) + ); + }; + (-$val:tt) => { + assert_eq!( + Num::::from_str_radix(&("-".to_string() + stringify!($val)), 10) + .unwrap(), + num!(-$val) + ); + }; + } + + str_radix_test!(0.1); + str_radix_test!(0.100000); + str_radix_test!(0000.1000); + str_radix_test!(000000.100000); + str_radix_test!(000000.1); + + str_radix_test!(138.229); + str_radix_test!(-138.229); + str_radix_test!(-1321.229231); + } + #[cfg(not(debug_assertions))] #[test] fn test_all_multiplies() { diff --git a/book/games/pong/Cargo.lock b/book/games/pong/Cargo.lock index da8c3c2c..ca6009a5 100644 --- a/book/games/pong/Cargo.lock +++ b/book/games/pong/Cargo.lock @@ -33,6 +33,7 @@ name = "agb_fixnum" version = "0.18.1" dependencies = [ "agb_macros", + "num", ] [[package]] @@ -253,7 +254,7 @@ dependencies = [ "byteorder", "color_quant", "num-iter", - "num-rational", + "num-rational 0.3.2", "num-traits", "png", ] @@ -297,6 +298,28 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca" +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-complex", + "num-integer", + "num-iter", + "num-rational 0.4.1", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.46" @@ -328,6 +351,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18" diff --git a/examples/amplitude/Cargo.lock b/examples/amplitude/Cargo.lock index f6e4ce7b..7180857e 100644 --- a/examples/amplitude/Cargo.lock +++ b/examples/amplitude/Cargo.lock @@ -33,6 +33,7 @@ name = "agb_fixnum" version = "0.18.1" dependencies = [ "agb_macros", + "num", ] [[package]] @@ -260,7 +261,7 @@ dependencies = [ "byteorder", "color_quant", "num-iter", - "num-rational", + "num-rational 0.3.2", "num-traits", "png", ] @@ -304,6 +305,28 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca" +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-complex", + "num-integer", + "num-iter", + "num-rational 0.4.1", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.46" @@ -335,6 +358,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18" diff --git a/examples/combo/Cargo.lock b/examples/combo/Cargo.lock index b114b358..974aeb78 100644 --- a/examples/combo/Cargo.lock +++ b/examples/combo/Cargo.lock @@ -33,6 +33,7 @@ name = "agb_fixnum" version = "0.18.1" dependencies = [ "agb_macros", + "num", ] [[package]] @@ -393,7 +394,7 @@ dependencies = [ "byteorder", "color_quant", "num-iter", - "num-rational", + "num-rational 0.3.2", "num-traits", "png", ] @@ -501,6 +502,28 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca" +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-complex", + "num-integer", + "num-iter", + "num-rational 0.4.1", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.46" @@ -532,6 +555,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18" diff --git a/examples/hyperspace-roll/Cargo.lock b/examples/hyperspace-roll/Cargo.lock index b6acee56..945750aa 100644 --- a/examples/hyperspace-roll/Cargo.lock +++ b/examples/hyperspace-roll/Cargo.lock @@ -33,6 +33,7 @@ name = "agb_fixnum" version = "0.18.1" dependencies = [ "agb_macros", + "num", ] [[package]] @@ -260,7 +261,7 @@ dependencies = [ "byteorder", "color_quant", "num-iter", - "num-rational", + "num-rational 0.3.2", "num-traits", "png", ] @@ -304,6 +305,28 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca" +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-complex", + "num-integer", + "num-iter", + "num-rational 0.4.1", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.46" @@ -335,6 +358,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18" diff --git a/examples/the-dungeon-puzzlers-lament/Cargo.lock b/examples/the-dungeon-puzzlers-lament/Cargo.lock index d961d1a7..fbce525e 100644 --- a/examples/the-dungeon-puzzlers-lament/Cargo.lock +++ b/examples/the-dungeon-puzzlers-lament/Cargo.lock @@ -33,6 +33,7 @@ name = "agb_fixnum" version = "0.18.1" dependencies = [ "agb_macros", + "num", ] [[package]] @@ -349,7 +350,7 @@ dependencies = [ "byteorder", "color_quant", "num-iter", - "num-rational", + "num-rational 0.3.2", "num-traits", "png", ] @@ -439,6 +440,28 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca" +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-complex", + "num-integer", + "num-iter", + "num-rational 0.4.1", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.46" @@ -470,6 +493,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18" diff --git a/examples/the-hat-chooses-the-wizard/Cargo.lock b/examples/the-hat-chooses-the-wizard/Cargo.lock index a8ab599d..68052bdb 100644 --- a/examples/the-hat-chooses-the-wizard/Cargo.lock +++ b/examples/the-hat-chooses-the-wizard/Cargo.lock @@ -33,6 +33,7 @@ name = "agb_fixnum" version = "0.18.1" dependencies = [ "agb_macros", + "num", ] [[package]] @@ -253,7 +254,7 @@ dependencies = [ "byteorder", "color_quant", "num-iter", - "num-rational", + "num-rational 0.3.2", "num-traits", "png", ] @@ -303,6 +304,28 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca" +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-complex", + "num-integer", + "num-iter", + "num-rational 0.4.1", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.46" @@ -334,6 +357,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18" diff --git a/examples/the-purple-night/Cargo.lock b/examples/the-purple-night/Cargo.lock index 35607273..e03a195a 100644 --- a/examples/the-purple-night/Cargo.lock +++ b/examples/the-purple-night/Cargo.lock @@ -33,6 +33,7 @@ name = "agb_fixnum" version = "0.18.1" dependencies = [ "agb_macros", + "num", ] [[package]] @@ -271,7 +272,7 @@ dependencies = [ "byteorder", "color_quant", "num-iter", - "num-rational", + "num-rational 0.3.2", "num-traits", "png", ] @@ -327,6 +328,28 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca" +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-complex", + "num-integer", + "num-iter", + "num-rational 0.4.1", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.46" @@ -358,6 +381,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18"