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

?

Percentage Accurate: 79.5% → 91.0%
Time: 23.1s
Precision: binary64
Cost: 7625

?

\[ \begin{array}{c}[x, y] = \mathsf{sort}([x, y])\\ [t, a] = \mathsf{sort}([t, a])\\ \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 -3.4 \cdot 10^{+41} \lor \neg \left(z \leq 5.2 \cdot 10^{-125}\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 -3.4e+41) (not (<= z 5.2e-125)))
   (/ (+ (/ (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 <= -3.4e+41) || !(z <= 5.2e-125)) {
		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 <= -3.4e+41) || !(z <= 5.2e-125))
		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, -3.4e+41], N[Not[LessEqual[z, 5.2e-125]], $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 -3.4 \cdot 10^{+41} \lor \neg \left(z \leq 5.2 \cdot 10^{-125}\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 13 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.5%
Target80.6%
Herbie91.0%
\[\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 < -3.39999999999999998e41 or 5.20000000000000011e-125 < z

    1. Initial program 69.0%

      \[\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. Simplified88.1%

      \[\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]69.0%

      \[ \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* [=>]73.8%

      \[ \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 -3.39999999999999998e41 < z < 5.20000000000000011e-125

    1. Initial program 99.0%

      \[\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 simplification93.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -3.4 \cdot 10^{+41} \lor \neg \left(z \leq 5.2 \cdot 10^{-125}\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
Accuracy91.0%
Cost7625
\[\begin{array}{l} \mathbf{if}\;z \leq -3.4 \cdot 10^{+41} \lor \neg \left(z \leq 5.2 \cdot 10^{-125}\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
Accuracy87.2%
Cost5068
\[\begin{array}{l} t_1 := \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}\\ \mathbf{if}\;t_1 \leq -1 \cdot 10^{-184}:\\ \;\;\;\;\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{elif}\;t_1 \leq 0:\\ \;\;\;\;\frac{\frac{b}{z} + 9 \cdot \frac{x \cdot y}{z}}{c}\\ \mathbf{elif}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot -4\right) \cdot \frac{t}{c}\\ \end{array} \]
Alternative 3
Accuracy86.8%
Cost1604
\[\begin{array}{l} \mathbf{if}\;z \leq -1.9 \cdot 10^{+47}:\\ \;\;\;\;\left(9 \cdot \frac{x \cdot y}{z \cdot c} + \frac{b}{z \cdot c}\right) - 4 \cdot \frac{t \cdot a}{c}\\ \mathbf{elif}\;z \leq 9.8 \cdot 10^{+185}:\\ \;\;\;\;\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}\\ \mathbf{else}:\\ \;\;\;\;\frac{t \cdot \left(a \cdot -4\right) + \frac{b}{z}}{c}\\ \end{array} \]
Alternative 4
Accuracy84.5%
Cost1480
\[\begin{array}{l} t_1 := t \cdot \left(a \cdot -4\right)\\ \mathbf{if}\;z \leq -5.6 \cdot 10^{+208}:\\ \;\;\;\;\frac{t_1 + \frac{9 \cdot \left(x \cdot y\right)}{z}}{c}\\ \mathbf{elif}\;z \leq 1.9 \cdot 10^{+187}:\\ \;\;\;\;\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 + \frac{b}{z}}{c}\\ \end{array} \]
Alternative 5
Accuracy50.2%
Cost1104
\[\begin{array}{l} t_1 := 9 \cdot \left(\frac{y}{z} \cdot \frac{x}{c}\right)\\ \mathbf{if}\;y \leq -6.2 \cdot 10^{-121}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -1.62 \cdot 10^{-283}:\\ \;\;\;\;-4 \cdot \frac{a}{\frac{c}{t}}\\ \mathbf{elif}\;y \leq 2.1 \cdot 10^{-19}:\\ \;\;\;\;\frac{b}{z \cdot c}\\ \mathbf{elif}\;y \leq 1.2 \cdot 10^{+129}:\\ \;\;\;\;-4 \cdot \left(t \cdot \frac{a}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Accuracy50.3%
Cost1104
\[\begin{array}{l} \mathbf{if}\;y \leq -6.2 \cdot 10^{-121}:\\ \;\;\;\;9 \cdot \left(\frac{y}{z} \cdot \frac{x}{c}\right)\\ \mathbf{elif}\;y \leq -9.2 \cdot 10^{-284}:\\ \;\;\;\;-4 \cdot \frac{a}{\frac{c}{t}}\\ \mathbf{elif}\;y \leq 6.6 \cdot 10^{-18}:\\ \;\;\;\;\frac{b}{z \cdot c}\\ \mathbf{elif}\;y \leq 8.5 \cdot 10^{+129}:\\ \;\;\;\;-4 \cdot \left(t \cdot \frac{a}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;9 \cdot \frac{y}{\frac{z \cdot c}{x}}\\ \end{array} \]
Alternative 7
Accuracy49.8%
Cost1104
\[\begin{array}{l} \mathbf{if}\;y \leq -6.2 \cdot 10^{-121}:\\ \;\;\;\;9 \cdot \frac{x \cdot \frac{y}{z}}{c}\\ \mathbf{elif}\;y \leq -2.25 \cdot 10^{-284}:\\ \;\;\;\;-4 \cdot \frac{a}{\frac{c}{t}}\\ \mathbf{elif}\;y \leq 2.45 \cdot 10^{-17}:\\ \;\;\;\;\frac{b}{z \cdot c}\\ \mathbf{elif}\;y \leq 1.26 \cdot 10^{+128}:\\ \;\;\;\;-4 \cdot \left(t \cdot \frac{a}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;9 \cdot \frac{y}{\frac{z \cdot c}{x}}\\ \end{array} \]
Alternative 8
Accuracy49.5%
Cost978
\[\begin{array}{l} \mathbf{if}\;t \leq -5.5 \cdot 10^{+135} \lor \neg \left(t \leq -5 \cdot 10^{+121}\right) \land \left(t \leq -2.3 \cdot 10^{+31} \lor \neg \left(t \leq 3.7 \cdot 10^{-67}\right)\right):\\ \;\;\;\;-4 \cdot \left(t \cdot \frac{a}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{z \cdot c}\\ \end{array} \]
Alternative 9
Accuracy50.3%
Cost977
\[\begin{array}{l} \mathbf{if}\;t \leq -3.2 \cdot 10^{+135}:\\ \;\;\;\;-4 \cdot \frac{a}{\frac{c}{t}}\\ \mathbf{elif}\;t \leq -5 \cdot 10^{+121} \lor \neg \left(t \leq -1.9 \cdot 10^{+31}\right) \land t \leq 2.2 \cdot 10^{-67}:\\ \;\;\;\;\frac{b}{z \cdot c}\\ \mathbf{else}:\\ \;\;\;\;-4 \cdot \left(t \cdot \frac{a}{c}\right)\\ \end{array} \]
Alternative 10
Accuracy50.5%
Cost976
\[\begin{array}{l} t_1 := \frac{b}{z \cdot c}\\ t_2 := \left(a \cdot -4\right) \cdot \frac{t}{c}\\ \mathbf{if}\;t \leq -1.65 \cdot 10^{+137}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t \leq -5 \cdot 10^{+121}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t \leq -2.55 \cdot 10^{+31}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t \leq 3.2 \cdot 10^{-67}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;-4 \cdot \left(t \cdot \frac{a}{c}\right)\\ \end{array} \]
Alternative 11
Accuracy68.0%
Cost968
\[\begin{array}{l} \mathbf{if}\;t \leq -2.8 \cdot 10^{+138}:\\ \;\;\;\;\left(a \cdot -4\right) \cdot \frac{t}{c}\\ \mathbf{elif}\;t \leq 3.7 \cdot 10^{-67}:\\ \;\;\;\;\frac{b + 9 \cdot \left(x \cdot y\right)}{z \cdot c}\\ \mathbf{else}:\\ \;\;\;\;-4 \cdot \left(t \cdot \frac{a}{c}\right)\\ \end{array} \]
Alternative 12
Accuracy70.5%
Cost968
\[\begin{array}{l} \mathbf{if}\;t \leq -1.05 \cdot 10^{+31}:\\ \;\;\;\;\frac{t \cdot \left(a \cdot -4\right) + \frac{b}{z}}{c}\\ \mathbf{elif}\;t \leq 3.7 \cdot 10^{-67}:\\ \;\;\;\;\frac{b + 9 \cdot \left(x \cdot y\right)}{z \cdot c}\\ \mathbf{else}:\\ \;\;\;\;-4 \cdot \left(t \cdot \frac{a}{c}\right)\\ \end{array} \]
Alternative 13
Accuracy34.9%
Cost320
\[\frac{b}{z \cdot c} \]

Reproduce?

herbie shell --seed 2023271 
(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)))