?

Average Error: 16.37% → 0.42%
Time: 6.3s
Precision: binary64
Cost: 8137

?

\[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
\[\begin{array}{l} t_0 := \left(y - z\right) + 1\\ t_1 := \frac{x \cdot t_0}{z}\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 5 \cdot 10^{+290}\right):\\ \;\;\;\;\frac{x}{\frac{z}{t_0}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\ \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* x (+ (- y z) 1.0)) z))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (+ (- y z) 1.0)) (t_1 (/ (* x t_0) z)))
   (if (or (<= t_1 (- INFINITY)) (not (<= t_1 5e+290)))
     (/ x (/ z t_0))
     (- (/ (fma x y x) z) x))))
double code(double x, double y, double z) {
	return (x * ((y - z) + 1.0)) / z;
}
double code(double x, double y, double z) {
	double t_0 = (y - z) + 1.0;
	double t_1 = (x * t_0) / z;
	double tmp;
	if ((t_1 <= -((double) INFINITY)) || !(t_1 <= 5e+290)) {
		tmp = x / (z / t_0);
	} else {
		tmp = (fma(x, y, x) / z) - x;
	}
	return tmp;
}
function code(x, y, z)
	return Float64(Float64(x * Float64(Float64(y - z) + 1.0)) / z)
end
function code(x, y, z)
	t_0 = Float64(Float64(y - z) + 1.0)
	t_1 = Float64(Float64(x * t_0) / z)
	tmp = 0.0
	if ((t_1 <= Float64(-Inf)) || !(t_1 <= 5e+290))
		tmp = Float64(x / Float64(z / t_0));
	else
		tmp = Float64(Float64(fma(x, y, x) / z) - x);
	end
	return tmp
end
code[x_, y_, z_] := N[(N[(x * N[(N[(y - z), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(y - z), $MachinePrecision] + 1.0), $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * t$95$0), $MachinePrecision] / z), $MachinePrecision]}, If[Or[LessEqual[t$95$1, (-Infinity)], N[Not[LessEqual[t$95$1, 5e+290]], $MachinePrecision]], N[(x / N[(z / t$95$0), $MachinePrecision]), $MachinePrecision], N[(N[(N[(x * y + x), $MachinePrecision] / z), $MachinePrecision] - x), $MachinePrecision]]]]
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\begin{array}{l}
t_0 := \left(y - z\right) + 1\\
t_1 := \frac{x \cdot t_0}{z}\\
\mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 5 \cdot 10^{+290}\right):\\
\;\;\;\;\frac{x}{\frac{z}{t_0}}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\


\end{array}

Error?

Target

Original16.37%
Target0.58%
Herbie0.42%
\[\begin{array}{l} \mathbf{if}\;x < -2.71483106713436 \cdot 10^{-162}:\\ \;\;\;\;\left(1 + y\right) \cdot \frac{x}{z} - x\\ \mathbf{elif}\;x < 3.874108816439546 \cdot 10^{-197}:\\ \;\;\;\;\left(x \cdot \left(\left(y - z\right) + 1\right)\right) \cdot \frac{1}{z}\\ \mathbf{else}:\\ \;\;\;\;\left(1 + y\right) \cdot \frac{x}{z} - x\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 x (+.f64 (-.f64 y z) 1)) z) < -inf.0 or 4.9999999999999998e290 < (/.f64 (*.f64 x (+.f64 (-.f64 y z) 1)) z)

    1. Initial program 94.68

      \[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
    2. Simplified1.83

      \[\leadsto \color{blue}{\frac{x}{\frac{z}{\left(y - z\right) + 1}}} \]
      Proof

      [Start]94.68

      \[ \frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]

      associate-/l* [=>]1.83

      \[ \color{blue}{\frac{x}{\frac{z}{\left(y - z\right) + 1}}} \]

    if -inf.0 < (/.f64 (*.f64 x (+.f64 (-.f64 y z) 1)) z) < 4.9999999999999998e290

    1. Initial program 0.22

      \[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
    2. Simplified0.14

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x} \]
      Proof

      [Start]0.22

      \[ \frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]

      associate-*r/ [<=]6.55

      \[ \color{blue}{x \cdot \frac{\left(y - z\right) + 1}{z}} \]

      +-commutative [=>]6.55

      \[ x \cdot \frac{\color{blue}{1 + \left(y - z\right)}}{z} \]

      associate-+r- [=>]6.55

      \[ x \cdot \frac{\color{blue}{\left(1 + y\right) - z}}{z} \]

      div-sub [=>]6.54

      \[ x \cdot \color{blue}{\left(\frac{1 + y}{z} - \frac{z}{z}\right)} \]

      *-inverses [=>]6.54

      \[ x \cdot \left(\frac{1 + y}{z} - \color{blue}{1}\right) \]

      distribute-rgt-out-- [<=]6.54

      \[ \color{blue}{\frac{1 + y}{z} \cdot x - 1 \cdot x} \]

      *-lft-identity [=>]6.54

      \[ \frac{1 + y}{z} \cdot x - \color{blue}{x} \]

      *-commutative [=>]6.54

      \[ \color{blue}{x \cdot \frac{1 + y}{z}} - x \]

      associate-*r/ [=>]0.14

      \[ \color{blue}{\frac{x \cdot \left(1 + y\right)}{z}} - x \]

      *-commutative [=>]0.14

      \[ \frac{\color{blue}{\left(1 + y\right) \cdot x}}{z} - x \]

      +-commutative [=>]0.14

      \[ \frac{\color{blue}{\left(y + 1\right)} \cdot x}{z} - x \]

      distribute-lft1-in [<=]0.14

      \[ \frac{\color{blue}{y \cdot x + x}}{z} - x \]

      *-commutative [=>]0.14

      \[ \frac{\color{blue}{x \cdot y} + x}{z} - x \]

      fma-def [=>]0.14

      \[ \frac{\color{blue}{\mathsf{fma}\left(x, y, x\right)}}{z} - x \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.42

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \leq -\infty \lor \neg \left(\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \leq 5 \cdot 10^{+290}\right):\\ \;\;\;\;\frac{x}{\frac{z}{\left(y - z\right) + 1}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\ \end{array} \]

Alternatives

Alternative 1
Error33.39%
Cost1112
\[\begin{array}{l} t_0 := y \cdot \frac{x}{z}\\ \mathbf{if}\;z \leq -1.45 \cdot 10^{+60}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq -6 \cdot 10^{-12}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq -8.2 \cdot 10^{-154}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq -3.8 \cdot 10^{-250}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 4.5 \cdot 10^{-100}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq 5.4 \cdot 10^{+57}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
Alternative 2
Error7.4%
Cost844
\[\begin{array}{l} t_0 := \frac{x \cdot y}{z} - x\\ \mathbf{if}\;z \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 3.85 \cdot 10^{-25}:\\ \;\;\;\;\frac{x + x \cdot y}{z}\\ \mathbf{elif}\;z \leq 5 \cdot 10^{+220}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} - x\\ \end{array} \]
Alternative 3
Error0.17%
Cost841
\[\begin{array}{l} \mathbf{if}\;z \leq -2 \cdot 10^{-17} \lor \neg \left(z \leq 5.5 \cdot 10^{-28}\right):\\ \;\;\;\;\frac{x}{\frac{z}{\left(y - z\right) + 1}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x + x \cdot y}{z}\\ \end{array} \]
Alternative 4
Error0.16%
Cost841
\[\begin{array}{l} t_0 := \left(y - z\right) + 1\\ \mathbf{if}\;z \leq -1 \cdot 10^{-15} \lor \neg \left(z \leq 10^{+16}\right):\\ \;\;\;\;\frac{x}{\frac{z}{t_0}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot t_0}{z}\\ \end{array} \]
Alternative 5
Error6.82%
Cost713
\[\begin{array}{l} \mathbf{if}\;y \leq -920 \lor \neg \left(y \leq 0.00046\right):\\ \;\;\;\;\frac{x \cdot y}{z} - x\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} - x\\ \end{array} \]
Alternative 6
Error17.68%
Cost585
\[\begin{array}{l} \mathbf{if}\;y \leq -1.02 \cdot 10^{+70} \lor \neg \left(y \leq 2 \cdot 10^{+20}\right):\\ \;\;\;\;y \cdot \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} - x\\ \end{array} \]
Alternative 7
Error17.83%
Cost584
\[\begin{array}{l} \mathbf{if}\;y \leq -1.35 \cdot 10^{+70}:\\ \;\;\;\;y \cdot \frac{x}{z}\\ \mathbf{elif}\;y \leq 7.8 \cdot 10^{+20}:\\ \;\;\;\;\frac{x}{z} - x\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\frac{z}{x}}\\ \end{array} \]
Alternative 8
Error31.38%
Cost456
\[\begin{array}{l} \mathbf{if}\;z \leq -0.00068:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 3.85 \cdot 10^{-25}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
Alternative 9
Error52.08%
Cost128
\[-x \]

Error

Reproduce?

herbie shell --seed 2023121 
(FPCore (x y z)
  :name "Diagrams.TwoD.Segment.Bernstein:evaluateBernstein from diagrams-lib-1.3.0.3"
  :precision binary64

  :herbie-target
  (if (< x -2.71483106713436e-162) (- (* (+ 1.0 y) (/ x z)) x) (if (< x 3.874108816439546e-197) (* (* x (+ (- y z) 1.0)) (/ 1.0 z)) (- (* (+ 1.0 y) (/ x z)) x)))

  (/ (* x (+ (- y z) 1.0)) z))