Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtribick committed Apr 20, 2020
1 parent b21df83 commit d1e3b29
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 43 deletions.
12 changes: 10 additions & 2 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,11 @@ impl TwoFloat {
pub fn div_euclid(&self, rhs: &TwoFloat) -> TwoFloat {
let quotient = (self / rhs).trunc();
if (self - &quotient * rhs) < 0.0 {
if *rhs > 0.0 { quotient - 1.0 } else { quotient + 1.0 }
if *rhs > 0.0 {
quotient - 1.0
} else {
quotient + 1.0
}
} else {
quotient
}
Expand All @@ -450,7 +454,11 @@ impl TwoFloat {
/// assert_eq!((-a).rem_euclid(&(-b)), TwoFloat::from(1.0));
pub fn rem_euclid(&self, rhs: &TwoFloat) -> TwoFloat {
let remainder = self % rhs;
if remainder < 0.0 { remainder + rhs.abs() } else { remainder }
if remainder < 0.0 {
remainder + rhs.abs()
} else {
remainder
}
}
}

Expand Down
128 changes: 102 additions & 26 deletions src/functions/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ impl TwoFloat {
/// assert_eq!(a, TwoFloat::from(1.0));
/// assert_eq!(b, TwoFloat::from(0.0));
pub fn trunc(&self) -> TwoFloat {
if self.is_sign_positive() { self.floor() } else { self.ceil() }
if self.is_sign_positive() {
self.floor()
} else {
self.ceil()
}
}

/// Returns the smallest integer greater than or equal to the number.
Expand Down Expand Up @@ -153,10 +157,22 @@ mod tests {
assert_eq!(TwoFloat::new_add(-5.0, 1e-200).trunc(), -4.0);
assert_eq!(TwoFloat::new_add(-5.0, -1e-200).trunc(), -5.0);

assert_eq!(TwoFloat::new_add(EXP2_60, 1.5).trunc(), TwoFloat::new_add(EXP2_60, 1.0));
assert_eq!(TwoFloat::new_add(EXP2_60, -1.5).trunc(), TwoFloat::new_add(EXP2_60, -2.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, 1.5).trunc(), TwoFloat::new_add(-EXP2_60, 2.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, -1.5).trunc(), TwoFloat::new_add(-EXP2_60, -1.0));
assert_eq!(
TwoFloat::new_add(EXP2_60, 1.5).trunc(),
TwoFloat::new_add(EXP2_60, 1.0)
);
assert_eq!(
TwoFloat::new_add(EXP2_60, -1.5).trunc(),
TwoFloat::new_add(EXP2_60, -2.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, 1.5).trunc(),
TwoFloat::new_add(-EXP2_60, 2.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, -1.5).trunc(),
TwoFloat::new_add(-EXP2_60, -1.0)
);
}

#[test]
Expand All @@ -169,10 +185,22 @@ mod tests {
assert_eq!(TwoFloat::new_add(-5.0, 1e-200).ceil(), -4.0);
assert_eq!(TwoFloat::new_add(-5.0, -1e-200).ceil(), -5.0);

assert_eq!(TwoFloat::new_add(EXP2_60, 1.5).ceil(), TwoFloat::new_add(EXP2_60, 2.0));
assert_eq!(TwoFloat::new_add(EXP2_60, -1.5).ceil(), TwoFloat::new_add(EXP2_60, -1.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, 1.5).ceil(), TwoFloat::new_add(-EXP2_60, 2.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, -1.5).ceil(), TwoFloat::new_add(-EXP2_60, -1.0));
assert_eq!(
TwoFloat::new_add(EXP2_60, 1.5).ceil(),
TwoFloat::new_add(EXP2_60, 2.0)
);
assert_eq!(
TwoFloat::new_add(EXP2_60, -1.5).ceil(),
TwoFloat::new_add(EXP2_60, -1.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, 1.5).ceil(),
TwoFloat::new_add(-EXP2_60, 2.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, -1.5).ceil(),
TwoFloat::new_add(-EXP2_60, -1.0)
);
}

#[test]
Expand All @@ -185,10 +213,22 @@ mod tests {
assert_eq!(TwoFloat::new_add(-5.0, 1e-200).floor(), -5.0);
assert_eq!(TwoFloat::new_add(-5.0, -1e-200).floor(), -6.0);

assert_eq!(TwoFloat::new_add(EXP2_60, 1.5).floor(), TwoFloat::new_add(EXP2_60, 1.0));
assert_eq!(TwoFloat::new_add(EXP2_60, -1.5).floor(), TwoFloat::new_add(EXP2_60, -2.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, 1.5).floor(), TwoFloat::new_add(-EXP2_60, 1.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, -1.5).floor(), TwoFloat::new_add(-EXP2_60, -2.0));
assert_eq!(
TwoFloat::new_add(EXP2_60, 1.5).floor(),
TwoFloat::new_add(EXP2_60, 1.0)
);
assert_eq!(
TwoFloat::new_add(EXP2_60, -1.5).floor(),
TwoFloat::new_add(EXP2_60, -2.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, 1.5).floor(),
TwoFloat::new_add(-EXP2_60, 1.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, -1.5).floor(),
TwoFloat::new_add(-EXP2_60, -2.0)
);
}

#[test]
Expand All @@ -213,18 +253,54 @@ mod tests {
assert_eq!(TwoFloat::new_add(-1.5, 1e-200).round(), -1.0);
assert_eq!(TwoFloat::new_add(-1.5, -1e-200).round(), -2.0);

assert_eq!(TwoFloat::new_add(EXP2_60, 0.9).round(), TwoFloat::new_add(EXP2_60, 1.0));
assert_eq!(TwoFloat::new_add(EXP2_60, 1.1).round(), TwoFloat::new_add(EXP2_60, 1.0));
assert_eq!(TwoFloat::new_add(EXP2_60, -0.9).round(), TwoFloat::new_add(EXP2_60, -1.0));
assert_eq!(TwoFloat::new_add(EXP2_60, -1.1).round(), TwoFloat::new_add(EXP2_60, -1.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, 0.9).round(), TwoFloat::new_add(-EXP2_60, 1.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, 1.1).round(), TwoFloat::new_add(-EXP2_60, 1.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, -0.9).round(), TwoFloat::new_add(-EXP2_60, -1.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, -1.1).round(), TwoFloat::new_add(-EXP2_60, -1.0));

assert_eq!(TwoFloat::new_add(EXP2_60, 1.5).round(), TwoFloat::new_add(EXP2_60, 2.0));
assert_eq!(TwoFloat::new_add(EXP2_60, -1.5).round(), TwoFloat::new_add(EXP2_60, -1.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, 1.5).round(), TwoFloat::new_add(-EXP2_60, 1.0));
assert_eq!(TwoFloat::new_add(-EXP2_60, -1.5).round(), TwoFloat::new_add(-EXP2_60, -2.0));
assert_eq!(
TwoFloat::new_add(EXP2_60, 0.9).round(),
TwoFloat::new_add(EXP2_60, 1.0)
);
assert_eq!(
TwoFloat::new_add(EXP2_60, 1.1).round(),
TwoFloat::new_add(EXP2_60, 1.0)
);
assert_eq!(
TwoFloat::new_add(EXP2_60, -0.9).round(),
TwoFloat::new_add(EXP2_60, -1.0)
);
assert_eq!(
TwoFloat::new_add(EXP2_60, -1.1).round(),
TwoFloat::new_add(EXP2_60, -1.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, 0.9).round(),
TwoFloat::new_add(-EXP2_60, 1.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, 1.1).round(),
TwoFloat::new_add(-EXP2_60, 1.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, -0.9).round(),
TwoFloat::new_add(-EXP2_60, -1.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, -1.1).round(),
TwoFloat::new_add(-EXP2_60, -1.0)
);

assert_eq!(
TwoFloat::new_add(EXP2_60, 1.5).round(),
TwoFloat::new_add(EXP2_60, 2.0)
);
assert_eq!(
TwoFloat::new_add(EXP2_60, -1.5).round(),
TwoFloat::new_add(EXP2_60, -1.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, 1.5).round(),
TwoFloat::new_add(-EXP2_60, 1.0)
);
assert_eq!(
TwoFloat::new_add(-EXP2_60, -1.5).round(),
TwoFloat::new_add(-EXP2_60, -2.0)
);
}
}
41 changes: 34 additions & 7 deletions src/functions/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ impl TwoFloat {
///
/// assert_eq!(c, -a);
pub fn copysign(&self, sign: &TwoFloat) -> TwoFloat {
if self.is_sign_positive() == sign.is_sign_positive() { *self } else { -self }
if self.is_sign_positive() == sign.is_sign_positive() {
*self
} else {
-self
}
}

/// Returns a number that represents the sign of the value.
Expand All @@ -90,9 +94,16 @@ impl TwoFloat {
/// assert_eq!(b.signum(), -1.0);
pub fn signum(&self) -> TwoFloat {
if self.is_valid() {
if self.is_sign_positive() { TwoFloat::from(1.0) } else { TwoFloat::from(-1.0) }
if self.is_sign_positive() {
TwoFloat::from(1.0)
} else {
TwoFloat::from(-1.0)
}
} else {
TwoFloat { hi: std::f64::NAN, lo: std::f64::NAN }
TwoFloat {
hi: std::f64::NAN,
lo: std::f64::NAN,
}
}
}
}
Expand All @@ -116,16 +127,32 @@ mod tests {
assert!(TwoFloat { hi: 0.0, lo: -0.0 }.is_sign_positive());
assert!(!TwoFloat { hi: -0.0, lo: 0.0 }.is_sign_positive());
assert!(!TwoFloat { hi: -0.0, lo: -0.0 }.is_sign_positive());
assert!(TwoFloat { hi: 1.0, lo: -1e-300}.is_sign_positive());
assert!(!TwoFloat { hi: -1.0, lo: -1e-300 }.is_sign_positive());
assert!(TwoFloat {
hi: 1.0,
lo: -1e-300
}
.is_sign_positive());
assert!(!TwoFloat {
hi: -1.0,
lo: -1e-300
}
.is_sign_positive());
}

#[test]
fn is_sign_negative_test() {
assert!(!TwoFloat { hi: 0.0, lo: -0.0 }.is_sign_negative());
assert!(TwoFloat { hi: -0.0, lo: 0.0 }.is_sign_negative());
assert!(TwoFloat { hi: -0.0, lo: -0.0 }.is_sign_negative());
assert!(!TwoFloat { hi: 1.0, lo: -1e-300 }.is_sign_negative());
assert!(TwoFloat { hi: -1.0, lo: -1e-300 }.is_sign_negative());
assert!(!TwoFloat {
hi: 1.0,
lo: -1e-300
}
.is_sign_negative());
assert!(TwoFloat {
hi: -1.0,
lo: -1e-300
}
.is_sign_negative());
}
}
5 changes: 4 additions & 1 deletion src/functions/trigonometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,9 @@ mod tests {
assert_eq!(-PI, TwoFloat::from(-0.0).atan2(&TwoFloat::from(-1.0)));
assert_eq!(FRAC_PI_2, TwoFloat::from(1.0).atan2(&TwoFloat::from(0.0)));
assert_eq!(-FRAC_PI_2, TwoFloat::from(-1.0).atan2(&TwoFloat::from(0.0)));
assert!((0.73f64.atan2(0.21f64) - TwoFloat::from(0.73).atan2(&TwoFloat::from(0.21))).abs() < THRESHOLD);
assert!(
(0.73f64.atan2(0.21f64) - TwoFloat::from(0.73).atan2(&TwoFloat::from(0.21))).abs()
< THRESHOLD
);
}
}
16 changes: 13 additions & 3 deletions tests/fraction_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,18 @@ randomized_test!(round_hi_half_test, |rng: F64Rand| {
};
let result = source.round();

assert!(result.is_valid(), "round({:?}) produced invalid value", source);
assert!(
result.is_valid(),
"round({:?}) produced invalid value",
source
);
assert_eq!(result, expected, "Incorrect value of round({:?})", source);
});

randomized_test!(round_lo_fract_test, |rng: F64Rand| {
let (a_fract, b) = get_valid_pair(rng, |x, y| y.fract() != 0.0 && y.fract().abs() != 0.5 && no_overlap(x.trunc(), y));
let (a_fract, b) = get_valid_pair(rng, |x, y| {
y.fract() != 0.0 && y.fract().abs() != 0.5 && no_overlap(x.trunc(), y)
});
let a = a_fract.trunc();
let source = TwoFloat::try_new(a, b).unwrap();
let expected = TwoFloat::new_add(a, b.round());
Expand All @@ -455,7 +461,11 @@ randomized_test!(round_lo_half_test, |rng: F64Rand| {

let result = source.round();

assert!(result.is_valid(), "round({:?}) produced invalid value", source);
assert!(
result.is_valid(),
"round({:?}) produced invalid value",
source
);
assert_eq!(result, expected, "Incorrect value of round({:?})", source);
});

Expand Down
6 changes: 2 additions & 4 deletions tests/trigonometry_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,8 @@ fn tan_atan_test() {
#[test]
fn sin_cos_atan2_test() {
let mut rng = rand::thread_rng();
let dist = rand::distributions::Uniform::new_inclusive(
-std::f64::consts::PI,
std::f64::consts::PI,
);
let dist =
rand::distributions::Uniform::new_inclusive(-std::f64::consts::PI, std::f64::consts::PI);
for _ in 0..TEST_ITERS {
let source = TwoFloat::from(rng.sample(dist));
let (s, c) = source.sin_cos();
Expand Down

0 comments on commit d1e3b29

Please sign in to comment.