use num_integer::Roots; use num_traits::checked_pow; use num_traits::{AsPrimitive, PrimInt, Signed}; use std::f64::MANTISSA_DIGITS; use std::fmt::Debug; use std::mem;
impl<T> TestInteger for T where T: Roots + PrimInt + Debug + AsPrimitive<f64> + 'static {}
/// Check that each root is correct /// /// If `x` is positive, check `rⁿ ≤ x < (r+1)ⁿ`. /// If `x` is negative, check `(r-1)ⁿ < x ≤ rⁿ`. fn check<T>(v: &[T], n: u32) where
T: TestInteger,
{ for i in v { let rt = i.nth_root(n); // println!("nth_root({:?}, {}) = {:?}", i, n, rt); if n == 2 {
assert_eq!(rt, i.sqrt());
} elseif n == 3 {
assert_eq!(rt, i.cbrt());
} if *i >= T::zero() { let rt1 = rt + T::one();
assert!(rt.pow(n) <= *i); iflet Some(x) = checked_pow(rt1, n as usize) {
assert!(*i < x);
}
} else { let rt1 = rt - T::one();
assert!(rt < T::zero());
assert!(*i <= rt.pow(n)); iflet Some(x) = checked_pow(rt1, n as usize) {
assert!(x < *i);
}
};
}
}
/// Get the maximum value that will round down as `f64` (if any), /// and its successor that will round up. /// /// Important because the `std` implementations cast to `f64` to /// get a close approximation of the roots. fn mantissa_max<T>() -> Option<(T, T)> where
T: TestInteger,
{ let bits = if T::min_value().is_zero() { 8 * mem::size_of::<T>()
} else { 8 * mem::size_of::<T>() - 1
}; if bits > MANTISSA_DIGITS as usize { let rounding_bit = T::one() << (bits - MANTISSA_DIGITS as usize - 1); let x = T::max_value() - rounding_bit;
let x1 = x + T::one(); let x2 = x1 + T::one();
assert!(x.as_() < x1.as_());
assert_eq!(x1.as_(), x2.as_());
Some((x, x1))
} else {
None
}
}
fn extend<T>(v: &mut Vec<T>, start: T, end: T) where
T: TestInteger,
{ letmut i = start; while i < end {
v.push(i);
i = i + T::one();
}
v.push(i);
}
fn extend_shl<T>(v: &mut Vec<T>, start: T, end: T, mask: T) where
T: TestInteger,
{ letmut i = start; while i != end {
v.push(i);
i = (i << 1) & mask;
}
}
fn extend_shr<T>(v: &mut Vec<T>, start: T, end: T) where
T: TestInteger,
{ letmut i = start; while i != end {
v.push(i);
i = i >> 1;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.