@@ 40,12 40,15 @@ impl Adaa2 {
fn fallback(x0: f64, x1: f64, x2: f64) -> f64 {
const A: f64 = 1.0 / 2.0;
let x_bar = A * (x0 + x2);
- let delta = x_bar - x1;
+ let delta = x_bar.powi(2) - x1.powi(2);
+ let sum = x_bar + x1;
if Adaa2::is_small(delta) {
Adaa2::f0(A * (x_bar + x1))
} else {
- (2.0 / delta) * (Adaa2::f1(x_bar) + (Adaa2::f2(x1) - Adaa2::f2(x_bar)) / delta)
+ (Adaa2::f1(x_bar) + (Adaa2::f2(x1) - Adaa2::f2(x_bar)) / delta * sum) / delta
+ * 2.0
+ * sum
}
}
@@ 53,11 56,12 @@ impl Adaa2 {
let x0 = x0 as f64;
let x1 = self.x1;
let x2 = self.x2;
- let dif = x0 - x2;
+ let dif = x0.powi(2) - x2.powi(2);
+ let sum = x0 + x2;
let y = if Adaa2::is_small(dif) {
Adaa2::fallback(x0, x1, x2)
} else {
- (2.0 / (dif)) * (Adaa2::calc_d(x0, x1) - Adaa2::calc_d(x1, x2))
+ (Adaa2::calc_d(x0, x1) - Adaa2::calc_d(x1, x2)) / dif * 2.0 * sum
};
self.x2 = x1;