mirror of
https://github.com/italicsjenga/agb.git
synced 2025-02-02 12:36:35 +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
|
||||||
- Fixed the fast magnitude function in agb_fixnum. This is also used in fast_normalise. Previously only worked for positive (x, y).
|
- 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
|
## [0.11.1] - 2022/08/02
|
||||||
|
|
||||||
|
|
|
@ -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
|
// So we have to add 1 to the integral bit, and take 1 - fractional bit
|
||||||
if fractional != I::zero() && integral < I::zero() {
|
if fractional != I::zero() && integral < I::zero() {
|
||||||
integral = integral + I::one();
|
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;
|
fractional = (I::one() << N) - fractional;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1028,15 +1033,17 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn formats_fractions_correctly() {
|
fn formats_fractions_correctly() {
|
||||||
let a = Num::<i32, 8>::new(5);
|
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 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 c: Num<i32, 8> = b * minus_one;
|
||||||
|
let d: Num<i32, 8> = minus_one / four;
|
||||||
|
|
||||||
assert_eq!(b + c, 0.into());
|
assert_eq!(b + c, 0.into());
|
||||||
assert_eq!(format!("{}", b), "1.25");
|
assert_eq!(format!("{}", b), "1.25");
|
||||||
assert_eq!(format!("{}", c), "-1.25");
|
assert_eq!(format!("{}", c), "-1.25");
|
||||||
|
assert_eq!(format!("{}", d), "-0.25");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Reference in a new issue