mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-22 15:16:40 +11:00
Merge pull request #304 from ijc8/format-fix
Show negative sign when formatting fixnums between -1 and 0.
This commit is contained in:
commit
b5e3cd04b9
2 changed files with 11 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Add table
Reference in a new issue