Merge pull request #304 from ijc8/format-fix

Show negative sign when formatting fixnums between -1 and 0.
This commit is contained in:
Corwin 2022-09-15 21:30:00 +01:00 committed by GitHub
commit b5e3cd04b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View file

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Fixed
- Fixed the fast magnitude function in agb_fixnum. This is also used in fast_normalise. Previously only worked for positive (x, y).
- Fixed formatting of fixed point numbers in the range (-1, 0), which previously appeared positive.
## [0.11.1] - 2022/08/02
@ -64,4 +65,4 @@ Version 0.10.0 brings about many new features. As with most `agb` upgrades, you
### Fixed
- Sprite data is now correctly aligned so fast copies will always work
- A few methods which should really be internal have had `pub` removed
- The crate now compiles (but does not run) doctests in CI which pointed out a large number of non-compiling examples
- The crate now compiles (but does not run) doctests in CI which pointed out a large number of non-compiling examples

View file

@ -537,6 +537,11 @@ impl<I: FixedWidthUnsignedInteger, const N: usize> Display for Num<I, N> {
// So we have to add 1 to the integral bit, and take 1 - fractional bit
if fractional != I::zero() && integral < I::zero() {
integral = integral + I::one();
if integral == I::zero() {
// If the number is in the range (-1, 0), then we just bumped `integral` from -1 to 0,
// so we need to compensate for the missing negative sign.
write!(f, "-")?;
}
fractional = (I::one() << N) - fractional;
}
@ -1028,15 +1033,17 @@ mod tests {
#[test]
fn formats_fractions_correctly() {
let a = Num::<i32, 8>::new(5);
let two = Num::<i32, 8>::new(4);
let four = Num::<i32, 8>::new(4);
let minus_one = Num::<i32, 8>::new(-1);
let b: Num<i32, 8> = a / two;
let b: Num<i32, 8> = a / four;
let c: Num<i32, 8> = b * minus_one;
let d: Num<i32, 8> = minus_one / four;
assert_eq!(b + c, 0.into());
assert_eq!(format!("{}", b), "1.25");
assert_eq!(format!("{}", c), "-1.25");
assert_eq!(format!("{}", d), "-0.25");
}
#[test]