?

Average Error: 16.29% → 0.19%
Time: 7.3s
Precision: binary64
Cost: 7112

?

\[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
\[\begin{array}{l} \mathbf{if}\;z \leq -2 \cdot 10^{+25}:\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{elif}\;z \leq 8000:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\frac{z}{\left(y - z\right) + 1}}\\ \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* x (+ (- y z) 1.0)) z))
(FPCore (x y z)
 :precision binary64
 (if (<= z -2e+25)
   (- (* (/ y z) x) x)
   (if (<= z 8000.0) (- (/ (fma x y x) z) x) (/ x (/ z (+ (- y z) 1.0))))))
double code(double x, double y, double z) {
	return (x * ((y - z) + 1.0)) / z;
}
double code(double x, double y, double z) {
	double tmp;
	if (z <= -2e+25) {
		tmp = ((y / z) * x) - x;
	} else if (z <= 8000.0) {
		tmp = (fma(x, y, x) / z) - x;
	} else {
		tmp = x / (z / ((y - z) + 1.0));
	}
	return tmp;
}
function code(x, y, z)
	return Float64(Float64(x * Float64(Float64(y - z) + 1.0)) / z)
end
function code(x, y, z)
	tmp = 0.0
	if (z <= -2e+25)
		tmp = Float64(Float64(Float64(y / z) * x) - x);
	elseif (z <= 8000.0)
		tmp = Float64(Float64(fma(x, y, x) / z) - x);
	else
		tmp = Float64(x / Float64(z / Float64(Float64(y - z) + 1.0)));
	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_] := If[LessEqual[z, -2e+25], N[(N[(N[(y / z), $MachinePrecision] * x), $MachinePrecision] - x), $MachinePrecision], If[LessEqual[z, 8000.0], N[(N[(N[(x * y + x), $MachinePrecision] / z), $MachinePrecision] - x), $MachinePrecision], N[(x / N[(z / N[(N[(y - z), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\begin{array}{l}
\mathbf{if}\;z \leq -2 \cdot 10^{+25}:\\
\;\;\;\;\frac{y}{z} \cdot x - x\\

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

\mathbf{else}:\\
\;\;\;\;\frac{x}{\frac{z}{\left(y - z\right) + 1}}\\


\end{array}

Error?

Target

Original16.29%
Target0.72%
Herbie0.19%
\[\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 3 regimes
  2. if z < -2.00000000000000018e25

    1. Initial program 28.91

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

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

      [Start]28.91

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

      associate-*r/ [<=]0.1

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

      +-commutative [=>]0.1

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

      associate-+r- [=>]0.1

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

      div-sub [=>]0.1

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

      *-inverses [=>]0.1

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

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

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

      *-lft-identity [=>]0.09

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

      *-commutative [=>]0.09

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

      associate-*r/ [=>]8.32

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

      *-commutative [=>]8.32

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

      +-commutative [=>]8.32

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

      distribute-lft1-in [<=]8.32

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

      *-commutative [=>]8.32

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

      fma-def [=>]8.32

      \[ \frac{\color{blue}{\mathsf{fma}\left(x, y, x\right)}}{z} - x \]
    3. Taylor expanded in y around inf 8.32

      \[\leadsto \color{blue}{\frac{y \cdot x}{z}} - x \]
    4. Simplified0.09

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

      [Start]8.32

      \[ \frac{y \cdot x}{z} - x \]

      associate-*l/ [<=]0.09

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

    if -2.00000000000000018e25 < z < 8e3

    1. Initial program 0.36

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

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

      [Start]0.36

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

      associate-*r/ [<=]13.11

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

      +-commutative [=>]13.11

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

      associate-+r- [=>]13.11

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

      div-sub [=>]13.1

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

      *-inverses [=>]13.1

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

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

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

      *-lft-identity [=>]13.09

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

      *-commutative [=>]13.09

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

      associate-*r/ [=>]0.31

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

      *-commutative [=>]0.31

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

      +-commutative [=>]0.31

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

      distribute-lft1-in [<=]0.3

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

      *-commutative [=>]0.3

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

      fma-def [=>]0.3

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

    if 8e3 < z

    1. Initial program 26.77

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

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

      [Start]26.77

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

      associate-/l* [=>]0.12

      \[ \color{blue}{\frac{x}{\frac{z}{\left(y - z\right) + 1}}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.19

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -2 \cdot 10^{+25}:\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{elif}\;z \leq 8000:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\frac{z}{\left(y - z\right) + 1}}\\ \end{array} \]

Alternatives

Alternative 1
Error20.01%
Cost1377
\[\begin{array}{l} t_0 := \frac{x}{z} - x\\ t_1 := y \cdot \frac{x}{z}\\ \mathbf{if}\;y \leq -1.9 \cdot 10^{+192}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -1.25 \cdot 10^{+172}:\\ \;\;\;\;-x\\ \mathbf{elif}\;y \leq -1.95 \cdot 10^{+148}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 1550000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 1.45 \cdot 10^{+23}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 2.4 \cdot 10^{+31}:\\ \;\;\;\;-x\\ \mathbf{elif}\;y \leq 1.3 \cdot 10^{+112} \lor \neg \left(y \leq 4.4 \cdot 10^{+144}\right):\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error20.15%
Cost1377
\[\begin{array}{l} t_0 := \frac{x}{z} - x\\ t_1 := y \cdot \frac{x}{z}\\ \mathbf{if}\;y \leq -6.6 \cdot 10^{+191}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -1.8 \cdot 10^{+168}:\\ \;\;\;\;-x\\ \mathbf{elif}\;y \leq -4.5 \cdot 10^{+148}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 1700000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 1.45 \cdot 10^{+23}:\\ \;\;\;\;\frac{y \cdot x}{z}\\ \mathbf{elif}\;y \leq 2.05 \cdot 10^{+31}:\\ \;\;\;\;-x\\ \mathbf{elif}\;y \leq 2.2 \cdot 10^{+112} \lor \neg \left(y \leq 3 \cdot 10^{+143}\right):\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error33.35%
Cost980
\[\begin{array}{l} t_0 := y \cdot \frac{x}{z}\\ \mathbf{if}\;z \leq -1:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq -9.6 \cdot 10^{-202}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq -1.16 \cdot 10^{-261}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 3.2 \cdot 10^{-187}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq 8 \cdot 10^{+15}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
Alternative 4
Error0.17%
Cost841
\[\begin{array}{l} \mathbf{if}\;z \leq -1 \cdot 10^{-16} \lor \neg \left(z \leq 1.1 \cdot 10^{-13}\right):\\ \;\;\;\;\frac{x}{\frac{z}{\left(y - z\right) + 1}}\\ \mathbf{else}:\\ \;\;\;\;\left(y + 1\right) \cdot \frac{x}{z}\\ \end{array} \]
Alternative 5
Error0.21%
Cost840
\[\begin{array}{l} t_0 := \left(y - z\right) + 1\\ \mathbf{if}\;z \leq -1 \cdot 10^{+23}:\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{elif}\;z \leq 2800:\\ \;\;\;\;\frac{x \cdot t_0}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\frac{z}{t_0}}\\ \end{array} \]
Alternative 6
Error14.11%
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -7.8 \cdot 10^{-8} \lor \neg \left(z \leq 2.25\right):\\ \;\;\;\;\frac{x}{z} - x\\ \mathbf{else}:\\ \;\;\;\;\left(y + 1\right) \cdot \frac{x}{z}\\ \end{array} \]
Alternative 7
Error1.48%
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -1.05 \lor \neg \left(z \leq 2.05\right):\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{else}:\\ \;\;\;\;\left(y + 1\right) \cdot \frac{x}{z}\\ \end{array} \]
Alternative 8
Error29.88%
Cost456
\[\begin{array}{l} \mathbf{if}\;z \leq -1:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 2.05:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
Alternative 9
Error52.01%
Cost128
\[-x \]

Error

Reproduce?

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