Average Error: 7.6 → 0.8
Time: 6.1s
Precision: binary64
\[ \begin{array}{c}[x, y] = \mathsf{sort}([x, y])\\ [z, t] = \mathsf{sort}([z, t])\\ \end{array} \]
\[\frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2} \]
\[\begin{array}{l} t_1 := \mathsf{fma}\left(\frac{z}{\frac{a}{t}}, -4.5, \left(y \cdot \frac{x}{a}\right) \cdot 0.5\right)\\ t_2 := x \cdot y + t \cdot \left(z \cdot -9\right)\\ \mathbf{if}\;t_2 \leq -2 \cdot 10^{+271}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_2 \leq 4 \cdot 10^{+248}:\\ \;\;\;\;-4.5 \cdot \frac{z \cdot t}{a} + 0.5 \cdot \frac{x \cdot y}{a}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
(FPCore (x y z t a)
 :precision binary64
 (/ (- (* x y) (* (* z 9.0) t)) (* a 2.0)))
(FPCore (x y z t a)
 :precision binary64
 (let* ((t_1 (fma (/ z (/ a t)) -4.5 (* (* y (/ x a)) 0.5)))
        (t_2 (+ (* x y) (* t (* z -9.0)))))
   (if (<= t_2 -2e+271)
     t_1
     (if (<= t_2 4e+248)
       (+ (* -4.5 (/ (* z t) a)) (* 0.5 (/ (* x y) a)))
       t_1))))
double code(double x, double y, double z, double t, double a) {
	return ((x * y) - ((z * 9.0) * t)) / (a * 2.0);
}
double code(double x, double y, double z, double t, double a) {
	double t_1 = fma((z / (a / t)), -4.5, ((y * (x / a)) * 0.5));
	double t_2 = (x * y) + (t * (z * -9.0));
	double tmp;
	if (t_2 <= -2e+271) {
		tmp = t_1;
	} else if (t_2 <= 4e+248) {
		tmp = (-4.5 * ((z * t) / a)) + (0.5 * ((x * y) / a));
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x, y, z, t, a)
	return Float64(Float64(Float64(x * y) - Float64(Float64(z * 9.0) * t)) / Float64(a * 2.0))
end
function code(x, y, z, t, a)
	t_1 = fma(Float64(z / Float64(a / t)), -4.5, Float64(Float64(y * Float64(x / a)) * 0.5))
	t_2 = Float64(Float64(x * y) + Float64(t * Float64(z * -9.0)))
	tmp = 0.0
	if (t_2 <= -2e+271)
		tmp = t_1;
	elseif (t_2 <= 4e+248)
		tmp = Float64(Float64(-4.5 * Float64(Float64(z * t) / a)) + Float64(0.5 * Float64(Float64(x * y) / a)));
	else
		tmp = t_1;
	end
	return tmp
end
code[x_, y_, z_, t_, a_] := N[(N[(N[(x * y), $MachinePrecision] - N[(N[(z * 9.0), $MachinePrecision] * t), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_, a_] := Block[{t$95$1 = N[(N[(z / N[(a / t), $MachinePrecision]), $MachinePrecision] * -4.5 + N[(N[(y * N[(x / a), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(x * y), $MachinePrecision] + N[(t * N[(z * -9.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$2, -2e+271], t$95$1, If[LessEqual[t$95$2, 4e+248], N[(N[(-4.5 * N[(N[(z * t), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[(N[(x * y), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2}
\begin{array}{l}
t_1 := \mathsf{fma}\left(\frac{z}{\frac{a}{t}}, -4.5, \left(y \cdot \frac{x}{a}\right) \cdot 0.5\right)\\
t_2 := x \cdot y + t \cdot \left(z \cdot -9\right)\\
\mathbf{if}\;t_2 \leq -2 \cdot 10^{+271}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_2 \leq 4 \cdot 10^{+248}:\\
\;\;\;\;-4.5 \cdot \frac{z \cdot t}{a} + 0.5 \cdot \frac{x \cdot y}{a}\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}

Error

Target

Original7.6
Target5.7
Herbie0.8
\[\begin{array}{l} \mathbf{if}\;a < -2.090464557976709 \cdot 10^{+86}:\\ \;\;\;\;0.5 \cdot \frac{y \cdot x}{a} - 4.5 \cdot \frac{t}{\frac{a}{z}}\\ \mathbf{elif}\;a < 2.144030707833976 \cdot 10^{+99}:\\ \;\;\;\;\frac{x \cdot y - z \cdot \left(9 \cdot t\right)}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{a} \cdot \left(x \cdot 0.5\right) - \frac{t}{a} \cdot \left(z \cdot 4.5\right)\\ \end{array} \]

Derivation

  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 x y) (*.f64 (*.f64 z 9) t)) < -1.99999999999999991e271 or 4.00000000000000018e248 < (-.f64 (*.f64 x y) (*.f64 (*.f64 z 9) t))

    1. Initial program 43.2

      \[\frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2} \]
    2. Simplified42.9

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(t, z \cdot -4.5, x \cdot \frac{y}{2}\right)}{a}} \]
    3. Taylor expanded in t around 0 42.7

      \[\leadsto \color{blue}{-4.5 \cdot \frac{t \cdot z}{a} + 0.5 \cdot \frac{y \cdot x}{a}} \]
    4. Applied egg-rr0.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{t}{a} \cdot z, -4.5, \frac{y}{\frac{a}{x}} \cdot 0.5\right)} \]
    5. Taylor expanded in y around 0 23.3

      \[\leadsto \mathsf{fma}\left(\frac{t}{a} \cdot z, -4.5, \color{blue}{\frac{y \cdot x}{a}} \cdot 0.5\right) \]
    6. Simplified0.5

      \[\leadsto \mathsf{fma}\left(\frac{t}{a} \cdot z, -4.5, \color{blue}{\left(y \cdot \frac{x}{a}\right)} \cdot 0.5\right) \]
    7. Applied egg-rr0.5

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{z}{\frac{a}{t}}}, -4.5, \left(y \cdot \frac{x}{a}\right) \cdot 0.5\right) \]

    if -1.99999999999999991e271 < (-.f64 (*.f64 x y) (*.f64 (*.f64 z 9) t)) < 4.00000000000000018e248

    1. Initial program 0.8

      \[\frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2} \]
    2. Simplified0.8

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(t, z \cdot -4.5, x \cdot \frac{y}{2}\right)}{a}} \]
    3. Taylor expanded in t around 0 0.8

      \[\leadsto \color{blue}{-4.5 \cdot \frac{t \cdot z}{a} + 0.5 \cdot \frac{y \cdot x}{a}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot y + t \cdot \left(z \cdot -9\right) \leq -2 \cdot 10^{+271}:\\ \;\;\;\;\mathsf{fma}\left(\frac{z}{\frac{a}{t}}, -4.5, \left(y \cdot \frac{x}{a}\right) \cdot 0.5\right)\\ \mathbf{elif}\;x \cdot y + t \cdot \left(z \cdot -9\right) \leq 4 \cdot 10^{+248}:\\ \;\;\;\;-4.5 \cdot \frac{z \cdot t}{a} + 0.5 \cdot \frac{x \cdot y}{a}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{z}{\frac{a}{t}}, -4.5, \left(y \cdot \frac{x}{a}\right) \cdot 0.5\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2022182 
(FPCore (x y z t a)
  :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, I"
  :precision binary64

  :herbie-target
  (if (< a -2.090464557976709e+86) (- (* 0.5 (/ (* y x) a)) (* 4.5 (/ t (/ a z)))) (if (< a 2.144030707833976e+99) (/ (- (* x y) (* z (* 9.0 t))) (* a 2.0)) (- (* (/ y a) (* x 0.5)) (* (/ t a) (* z 4.5)))))

  (/ (- (* x y) (* (* z 9.0) t)) (* a 2.0)))