Diagrams.Solve.Polynomial:cubForm from diagrams-solve-0.1, J

?

Percentage Accurate: 79.4% → 90.5%
Time: 21.8s
Precision: binary64
Cost: 7625

?

\[ \begin{array}{c}[x, y] = \mathsf{sort}([x, y])\\ \end{array} \]
\[\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c} \]
\[\begin{array}{l} \mathbf{if}\;z \leq -3700 \lor \neg \left(z \leq 4.2 \cdot 10^{-198}\right):\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(x, 9 \cdot y, b\right)}{z} + t \cdot \left(a \cdot -4\right)}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + \left(y \cdot \left(x \cdot 9\right) - a \cdot \left(t \cdot \left(z \cdot 4\right)\right)\right)}{z \cdot c}\\ \end{array} \]
(FPCore (x y z t a b c)
 :precision binary64
 (/ (+ (- (* (* x 9.0) y) (* (* (* z 4.0) t) a)) b) (* z c)))
(FPCore (x y z t a b c)
 :precision binary64
 (if (or (<= z -3700.0) (not (<= z 4.2e-198)))
   (/ (+ (/ (fma x (* 9.0 y) b) z) (* t (* a -4.0))) c)
   (/ (+ b (- (* y (* x 9.0)) (* a (* t (* z 4.0))))) (* z c))))
double code(double x, double y, double z, double t, double a, double b, double c) {
	return ((((x * 9.0) * y) - (((z * 4.0) * t) * a)) + b) / (z * c);
}
double code(double x, double y, double z, double t, double a, double b, double c) {
	double tmp;
	if ((z <= -3700.0) || !(z <= 4.2e-198)) {
		tmp = ((fma(x, (9.0 * y), b) / z) + (t * (a * -4.0))) / c;
	} else {
		tmp = (b + ((y * (x * 9.0)) - (a * (t * (z * 4.0))))) / (z * c);
	}
	return tmp;
}
function code(x, y, z, t, a, b, c)
	return Float64(Float64(Float64(Float64(Float64(x * 9.0) * y) - Float64(Float64(Float64(z * 4.0) * t) * a)) + b) / Float64(z * c))
end
function code(x, y, z, t, a, b, c)
	tmp = 0.0
	if ((z <= -3700.0) || !(z <= 4.2e-198))
		tmp = Float64(Float64(Float64(fma(x, Float64(9.0 * y), b) / z) + Float64(t * Float64(a * -4.0))) / c);
	else
		tmp = Float64(Float64(b + Float64(Float64(y * Float64(x * 9.0)) - Float64(a * Float64(t * Float64(z * 4.0))))) / Float64(z * c));
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_, c_] := N[(N[(N[(N[(N[(x * 9.0), $MachinePrecision] * y), $MachinePrecision] - N[(N[(N[(z * 4.0), $MachinePrecision] * t), $MachinePrecision] * a), $MachinePrecision]), $MachinePrecision] + b), $MachinePrecision] / N[(z * c), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_, a_, b_, c_] := If[Or[LessEqual[z, -3700.0], N[Not[LessEqual[z, 4.2e-198]], $MachinePrecision]], N[(N[(N[(N[(x * N[(9.0 * y), $MachinePrecision] + b), $MachinePrecision] / z), $MachinePrecision] + N[(t * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], N[(N[(b + N[(N[(y * N[(x * 9.0), $MachinePrecision]), $MachinePrecision] - N[(a * N[(t * N[(z * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(z * c), $MachinePrecision]), $MachinePrecision]]
\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c}
\begin{array}{l}
\mathbf{if}\;z \leq -3700 \lor \neg \left(z \leq 4.2 \cdot 10^{-198}\right):\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(x, 9 \cdot y, b\right)}{z} + t \cdot \left(a \cdot -4\right)}{c}\\

\mathbf{else}:\\
\;\;\;\;\frac{b + \left(y \cdot \left(x \cdot 9\right) - a \cdot \left(t \cdot \left(z \cdot 4\right)\right)\right)}{z \cdot c}\\


\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 12 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Target

Original79.4%
Target80.5%
Herbie90.5%
\[\begin{array}{l} \mathbf{if}\;\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c} < -1.100156740804105 \cdot 10^{-171}:\\ \;\;\;\;\frac{\left(\left(x \cdot 9\right) \cdot y - \left(z \cdot 4\right) \cdot \left(t \cdot a\right)\right) + b}{z \cdot c}\\ \mathbf{elif}\;\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c} < 0:\\ \;\;\;\;\frac{\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z}}{c}\\ \mathbf{elif}\;\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c} < 1.1708877911747488 \cdot 10^{-53}:\\ \;\;\;\;\frac{\left(\left(x \cdot 9\right) \cdot y - \left(z \cdot 4\right) \cdot \left(t \cdot a\right)\right) + b}{z \cdot c}\\ \mathbf{elif}\;\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c} < 2.876823679546137 \cdot 10^{+130}:\\ \;\;\;\;\left(\left(9 \cdot \frac{y}{c}\right) \cdot \frac{x}{z} + \frac{b}{c \cdot z}\right) - 4 \cdot \frac{a \cdot t}{c}\\ \mathbf{elif}\;\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c} < 1.3838515042456319 \cdot 10^{+158}:\\ \;\;\;\;\frac{\left(\left(x \cdot 9\right) \cdot y - \left(z \cdot 4\right) \cdot \left(t \cdot a\right)\right) + b}{z \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\left(9 \cdot \left(\frac{y}{c \cdot z} \cdot x\right) + \frac{b}{c \cdot z}\right) - 4 \cdot \frac{a \cdot t}{c}\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if z < -3700 or 4.19999999999999986e-198 < z

    1. Initial program 70.8%

      \[\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c} \]
    2. Simplified91.7%

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(x, 9 \cdot y, b\right)}{z} + t \cdot \left(a \cdot -4\right)}{c}} \]
      Step-by-step derivation

      [Start]70.8%

      \[ \frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c} \]

      associate-/r* [=>]76.3%

      \[ \color{blue}{\frac{\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z}}{c}} \]

    if -3700 < z < 4.19999999999999986e-198

    1. Initial program 94.7%

      \[\frac{\left(\left(x \cdot 9\right) \cdot y - \left(\left(z \cdot 4\right) \cdot t\right) \cdot a\right) + b}{z \cdot c} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification92.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -3700 \lor \neg \left(z \leq 4.2 \cdot 10^{-198}\right):\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(x, 9 \cdot y, b\right)}{z} + t \cdot \left(a \cdot -4\right)}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + \left(y \cdot \left(x \cdot 9\right) - a \cdot \left(t \cdot \left(z \cdot 4\right)\right)\right)}{z \cdot c}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy90.5%
Cost7625
\[\begin{array}{l} \mathbf{if}\;z \leq -3700 \lor \neg \left(z \leq 4.2 \cdot 10^{-198}\right):\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(x, 9 \cdot y, b\right)}{z} + t \cdot \left(a \cdot -4\right)}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + \left(y \cdot \left(x \cdot 9\right) - a \cdot \left(t \cdot \left(z \cdot 4\right)\right)\right)}{z \cdot c}\\ \end{array} \]
Alternative 2
Accuracy85.6%
Cost1480
\[\begin{array}{l} t_1 := t \cdot \left(a \cdot -4\right)\\ \mathbf{if}\;z \leq -6.6 \cdot 10^{+190}:\\ \;\;\;\;\frac{t_1 + \frac{b}{z}}{c}\\ \mathbf{elif}\;z \leq 1.45 \cdot 10^{+108}:\\ \;\;\;\;\frac{b + \left(x \cdot \left(9 \cdot y\right) - \left(z \cdot 4\right) \cdot \left(t \cdot a\right)\right)}{z \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_1 + 9 \cdot \left(x \cdot \frac{y}{z}\right)}{c}\\ \end{array} \]
Alternative 3
Accuracy77.6%
Cost1357
\[\begin{array}{l} t_1 := t \cdot \left(a \cdot -4\right)\\ \mathbf{if}\;z \leq -5.2 \cdot 10^{+139}:\\ \;\;\;\;\frac{t_1 + \frac{b}{z}}{c}\\ \mathbf{elif}\;z \leq -0.0005 \lor \neg \left(z \leq 180000000\right):\\ \;\;\;\;\frac{t_1 + 9 \cdot \left(x \cdot \frac{y}{z}\right)}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + 9 \cdot \left(x \cdot y\right)}{z \cdot c}\\ \end{array} \]
Alternative 4
Accuracy77.2%
Cost1356
\[\begin{array}{l} t_1 := t \cdot \left(a \cdot -4\right)\\ \mathbf{if}\;z \leq -3 \cdot 10^{+141}:\\ \;\;\;\;\frac{t_1 + \frac{b}{z}}{c}\\ \mathbf{elif}\;z \leq -0.0102:\\ \;\;\;\;\frac{9 \cdot \frac{x \cdot y}{z} + -4 \cdot \left(t \cdot a\right)}{c}\\ \mathbf{elif}\;z \leq 3000000000:\\ \;\;\;\;\frac{b + 9 \cdot \left(x \cdot y\right)}{z \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_1 + 9 \cdot \left(x \cdot \frac{y}{z}\right)}{c}\\ \end{array} \]
Alternative 5
Accuracy75.9%
Cost969
\[\begin{array}{l} \mathbf{if}\;z \leq -0.00186 \lor \neg \left(z \leq 6.6 \cdot 10^{-66}\right):\\ \;\;\;\;\frac{t \cdot \left(a \cdot -4\right) + \frac{b}{z}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + 9 \cdot \left(x \cdot y\right)}{z \cdot c}\\ \end{array} \]
Alternative 6
Accuracy67.7%
Cost968
\[\begin{array}{l} \mathbf{if}\;z \leq -0.013:\\ \;\;\;\;\frac{-4}{\frac{\frac{c}{a}}{t}}\\ \mathbf{elif}\;z \leq 6500000000000:\\ \;\;\;\;\frac{b + 9 \cdot \left(x \cdot y\right)}{z \cdot c}\\ \mathbf{else}:\\ \;\;\;\;-4 \cdot \frac{t}{\frac{c}{a}}\\ \end{array} \]
Alternative 7
Accuracy48.4%
Cost844
\[\begin{array}{l} \mathbf{if}\;b \leq -9.2 \cdot 10^{+97}:\\ \;\;\;\;\frac{\frac{1}{z}}{\frac{c}{b}}\\ \mathbf{elif}\;b \leq -4.2 \cdot 10^{-197}:\\ \;\;\;\;9 \cdot \frac{y}{\frac{c}{\frac{x}{z}}}\\ \mathbf{elif}\;b \leq 7.5 \cdot 10^{+80}:\\ \;\;\;\;\frac{a \cdot \left(t \cdot -4\right)}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{z \cdot c}\\ \end{array} \]
Alternative 8
Accuracy50.9%
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -0.00245 \lor \neg \left(z \leq 66000000000000\right):\\ \;\;\;\;-4 \cdot \left(a \cdot \frac{t}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{z \cdot c}\\ \end{array} \]
Alternative 9
Accuracy50.8%
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -0.00086 \lor \neg \left(z \leq 39000000\right):\\ \;\;\;\;-4 \cdot \frac{t}{\frac{c}{a}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{z \cdot c}\\ \end{array} \]
Alternative 10
Accuracy50.8%
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -0.00125 \lor \neg \left(z \leq 24000000000000\right):\\ \;\;\;\;-4 \cdot \frac{t}{\frac{c}{a}}\\ \mathbf{else}:\\ \;\;\;\;b \cdot \frac{1}{z \cdot c}\\ \end{array} \]
Alternative 11
Accuracy50.8%
Cost712
\[\begin{array}{l} \mathbf{if}\;z \leq -0.0028:\\ \;\;\;\;\frac{-4}{\frac{\frac{c}{a}}{t}}\\ \mathbf{elif}\;z \leq 6200000:\\ \;\;\;\;b \cdot \frac{1}{z \cdot c}\\ \mathbf{else}:\\ \;\;\;\;-4 \cdot \frac{t}{\frac{c}{a}}\\ \end{array} \]
Alternative 12
Accuracy34.1%
Cost320
\[\frac{b}{z \cdot c} \]

Reproduce?

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

  :herbie-target
  (if (< (/ (+ (- (* (* x 9.0) y) (* (* (* z 4.0) t) a)) b) (* z c)) -1.100156740804105e-171) (/ (+ (- (* (* x 9.0) y) (* (* z 4.0) (* t a))) b) (* z c)) (if (< (/ (+ (- (* (* x 9.0) y) (* (* (* z 4.0) t) a)) b) (* z c)) 0.0) (/ (/ (+ (- (* (* x 9.0) y) (* (* (* z 4.0) t) a)) b) z) c) (if (< (/ (+ (- (* (* x 9.0) y) (* (* (* z 4.0) t) a)) b) (* z c)) 1.1708877911747488e-53) (/ (+ (- (* (* x 9.0) y) (* (* z 4.0) (* t a))) b) (* z c)) (if (< (/ (+ (- (* (* x 9.0) y) (* (* (* z 4.0) t) a)) b) (* z c)) 2.876823679546137e+130) (- (+ (* (* 9.0 (/ y c)) (/ x z)) (/ b (* c z))) (* 4.0 (/ (* a t) c))) (if (< (/ (+ (- (* (* x 9.0) y) (* (* (* z 4.0) t) a)) b) (* z c)) 1.3838515042456319e+158) (/ (+ (- (* (* x 9.0) y) (* (* z 4.0) (* t a))) b) (* z c)) (- (+ (* 9.0 (* (/ y (* c z)) x)) (/ b (* c z))) (* 4.0 (/ (* a t) c))))))))

  (/ (+ (- (* (* x 9.0) y) (* (* (* z 4.0) t) a)) b) (* z c)))