Average Error: 1.6 → 0.8
Time: 6.4s
Precision: binary64
Cost: 13640
\[\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right| \]
\[\begin{array}{l} t_0 := \left|\mathsf{fma}\left(x, \frac{z}{y}, \frac{-4 - x}{y}\right)\right|\\ \mathbf{if}\;x \leq -5 \cdot 10^{-70}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.3 \cdot 10^{-160}:\\ \;\;\;\;\left|\frac{\left(x + 4\right) - x \cdot z}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (x y z) :precision binary64 (fabs (- (/ (+ x 4.0) y) (* (/ x y) z))))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (fabs (fma x (/ z y) (/ (- -4.0 x) y)))))
   (if (<= x -5e-70)
     t_0
     (if (<= x 1.3e-160) (fabs (/ (- (+ x 4.0) (* x z)) y)) t_0))))
double code(double x, double y, double z) {
	return fabs((((x + 4.0) / y) - ((x / y) * z)));
}
double code(double x, double y, double z) {
	double t_0 = fabs(fma(x, (z / y), ((-4.0 - x) / y)));
	double tmp;
	if (x <= -5e-70) {
		tmp = t_0;
	} else if (x <= 1.3e-160) {
		tmp = fabs((((x + 4.0) - (x * z)) / y));
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x, y, z)
	return abs(Float64(Float64(Float64(x + 4.0) / y) - Float64(Float64(x / y) * z)))
end
function code(x, y, z)
	t_0 = abs(fma(x, Float64(z / y), Float64(Float64(-4.0 - x) / y)))
	tmp = 0.0
	if (x <= -5e-70)
		tmp = t_0;
	elseif (x <= 1.3e-160)
		tmp = abs(Float64(Float64(Float64(x + 4.0) - Float64(x * z)) / y));
	else
		tmp = t_0;
	end
	return tmp
end
code[x_, y_, z_] := N[Abs[N[(N[(N[(x + 4.0), $MachinePrecision] / y), $MachinePrecision] - N[(N[(x / y), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[x_, y_, z_] := Block[{t$95$0 = N[Abs[N[(x * N[(z / y), $MachinePrecision] + N[(N[(-4.0 - x), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[x, -5e-70], t$95$0, If[LessEqual[x, 1.3e-160], N[Abs[N[(N[(N[(x + 4.0), $MachinePrecision] - N[(x * z), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right|
\begin{array}{l}
t_0 := \left|\mathsf{fma}\left(x, \frac{z}{y}, \frac{-4 - x}{y}\right)\right|\\
\mathbf{if}\;x \leq -5 \cdot 10^{-70}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq 1.3 \cdot 10^{-160}:\\
\;\;\;\;\left|\frac{\left(x + 4\right) - x \cdot z}{y}\right|\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error

Derivation

  1. Split input into 2 regimes
  2. if x < -4.9999999999999998e-70 or 1.30000000000000002e-160 < x

    1. Initial program 1.1

      \[\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right| \]
    2. Simplified1.2

      \[\leadsto \color{blue}{\left|\mathsf{fma}\left(x, \frac{z}{y}, \frac{-4 - x}{y}\right)\right|} \]
      Proof
      (fabs.f64 (fma.f64 x (/.f64 z y) (/.f64 (-.f64 -4 x) y))): 0 points increase in error, 0 points decrease in error
      (fabs.f64 (fma.f64 x (/.f64 z y) (/.f64 (-.f64 (Rewrite<= metadata-eval (neg.f64 4)) x) y))): 0 points increase in error, 0 points decrease in error
      (fabs.f64 (fma.f64 x (/.f64 z y) (/.f64 (Rewrite<= unsub-neg_binary64 (+.f64 (neg.f64 4) (neg.f64 x))) y))): 0 points increase in error, 0 points decrease in error
      (fabs.f64 (fma.f64 x (/.f64 z y) (/.f64 (Rewrite<= distribute-neg-in_binary64 (neg.f64 (+.f64 4 x))) y))): 0 points increase in error, 0 points decrease in error
      (fabs.f64 (fma.f64 x (/.f64 z y) (/.f64 (neg.f64 (Rewrite<= +-commutative_binary64 (+.f64 x 4))) y))): 0 points increase in error, 0 points decrease in error
      (fabs.f64 (fma.f64 x (/.f64 z y) (Rewrite<= distribute-neg-frac_binary64 (neg.f64 (/.f64 (+.f64 x 4) y))))): 0 points increase in error, 0 points decrease in error
      (fabs.f64 (Rewrite<= fma-neg_binary64 (-.f64 (*.f64 x (/.f64 z y)) (/.f64 (+.f64 x 4) y)))): 3 points increase in error, 0 points decrease in error
      (fabs.f64 (-.f64 (Rewrite=> associate-*r/_binary64 (/.f64 (*.f64 x z) y)) (/.f64 (+.f64 x 4) y))): 26 points increase in error, 20 points decrease in error
      (fabs.f64 (-.f64 (Rewrite<= associate-*l/_binary64 (*.f64 (/.f64 x y) z)) (/.f64 (+.f64 x 4) y))): 17 points increase in error, 25 points decrease in error
      (Rewrite<= fabs-sub_binary64 (fabs.f64 (-.f64 (/.f64 (+.f64 x 4) y) (*.f64 (/.f64 x y) z)))): 0 points increase in error, 0 points decrease in error

    if -4.9999999999999998e-70 < x < 1.30000000000000002e-160

    1. Initial program 2.5

      \[\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right| \]
    2. Taylor expanded in x around 0 6.7

      \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} + \left(\frac{1}{y} - \frac{z}{y}\right) \cdot x}\right| \]
    3. Simplified0.0

      \[\leadsto \left|\color{blue}{\frac{\left(x - -4\right) - z \cdot x}{y}}\right| \]
      Proof
      (/.f64 (-.f64 (-.f64 x -4) (*.f64 z x)) y): 0 points increase in error, 0 points decrease in error
      (/.f64 (-.f64 (Rewrite=> sub-neg_binary64 (+.f64 x (neg.f64 -4))) (*.f64 z x)) y): 0 points increase in error, 0 points decrease in error
      (/.f64 (-.f64 (+.f64 x (Rewrite=> metadata-eval 4)) (*.f64 z x)) y): 0 points increase in error, 0 points decrease in error
      (/.f64 (-.f64 (Rewrite<= +-rgt-identity_binary64 (+.f64 (+.f64 x 4) 0)) (*.f64 z x)) y): 0 points increase in error, 0 points decrease in error
      (/.f64 (-.f64 (+.f64 (+.f64 x 4) 0) (Rewrite<= *-commutative_binary64 (*.f64 x z))) y): 0 points increase in error, 0 points decrease in error
      (/.f64 (-.f64 (Rewrite=> +-rgt-identity_binary64 (+.f64 x 4)) (*.f64 x z)) y): 0 points increase in error, 0 points decrease in error
      (/.f64 (-.f64 (Rewrite=> +-commutative_binary64 (+.f64 4 x)) (*.f64 x z)) y): 0 points increase in error, 0 points decrease in error
      (/.f64 (-.f64 (+.f64 4 x) (Rewrite=> *-commutative_binary64 (*.f64 z x))) y): 0 points increase in error, 0 points decrease in error
      (Rewrite=> div-sub_binary64 (-.f64 (/.f64 (+.f64 4 x) y) (/.f64 (*.f64 z x) y))): 5 points increase in error, 2 points decrease in error
      (-.f64 (/.f64 (Rewrite<= +-commutative_binary64 (+.f64 x 4)) y) (/.f64 (*.f64 z x) y)): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (+.f64 x 4) y) (/.f64 (Rewrite<= *-commutative_binary64 (*.f64 x z)) y)): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (+.f64 x 4) y) (Rewrite<= associate-*l/_binary64 (*.f64 (/.f64 x y) z))): 14 points increase in error, 29 points decrease in error
      (Rewrite<= unsub-neg_binary64 (+.f64 (/.f64 (+.f64 x 4) y) (neg.f64 (*.f64 (/.f64 x y) z)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 (+.f64 x 4) y) (Rewrite<= distribute-rgt-neg-out_binary64 (*.f64 (/.f64 x y) (neg.f64 z)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 (/.f64 x y) (neg.f64 z)) (/.f64 (+.f64 x 4) y))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 (/.f64 x y) (neg.f64 z)) (/.f64 (Rewrite<= *-lft-identity_binary64 (*.f64 1 (+.f64 x 4))) y)): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 (/.f64 x y) (neg.f64 z)) (Rewrite<= associate-*l/_binary64 (*.f64 (/.f64 1 y) (+.f64 x 4)))): 22 points increase in error, 5 points decrease in error
      (+.f64 (*.f64 (/.f64 x y) (neg.f64 z)) (Rewrite=> distribute-rgt-in_binary64 (+.f64 (*.f64 x (/.f64 1 y)) (*.f64 4 (/.f64 1 y))))): 0 points increase in error, 2 points decrease in error
      (+.f64 (*.f64 (/.f64 x y) (neg.f64 z)) (+.f64 (Rewrite=> *-commutative_binary64 (*.f64 (/.f64 1 y) x)) (*.f64 4 (/.f64 1 y)))): 0 points increase in error, 0 points decrease in error
      (Rewrite=> associate-+r+_binary64 (+.f64 (+.f64 (*.f64 (/.f64 x y) (neg.f64 z)) (*.f64 (/.f64 1 y) x)) (*.f64 4 (/.f64 1 y)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (+.f64 (Rewrite=> *-commutative_binary64 (*.f64 (neg.f64 z) (/.f64 x y))) (*.f64 (/.f64 1 y) x)) (*.f64 4 (/.f64 1 y))): 0 points increase in error, 0 points decrease in error
      (+.f64 (+.f64 (Rewrite=> associate-*r/_binary64 (/.f64 (*.f64 (neg.f64 z) x) y)) (*.f64 (/.f64 1 y) x)) (*.f64 4 (/.f64 1 y))): 29 points increase in error, 14 points decrease in error
      (+.f64 (+.f64 (Rewrite=> associate-/l*_binary64 (/.f64 (neg.f64 z) (/.f64 y x))) (*.f64 (/.f64 1 y) x)) (*.f64 4 (/.f64 1 y))): 8 points increase in error, 29 points decrease in error
      (+.f64 (+.f64 (Rewrite<= distribute-neg-frac_binary64 (neg.f64 (/.f64 z (/.f64 y x)))) (*.f64 (/.f64 1 y) x)) (*.f64 4 (/.f64 1 y))): 0 points increase in error, 0 points decrease in error
      (+.f64 (+.f64 (neg.f64 (Rewrite=> associate-/r/_binary64 (*.f64 (/.f64 z y) x))) (*.f64 (/.f64 1 y) x)) (*.f64 4 (/.f64 1 y))): 26 points increase in error, 12 points decrease in error
      (+.f64 (+.f64 (Rewrite=> distribute-lft-neg-in_binary64 (*.f64 (neg.f64 (/.f64 z y)) x)) (*.f64 (/.f64 1 y) x)) (*.f64 4 (/.f64 1 y))): 0 points increase in error, 0 points decrease in error
      (+.f64 (+.f64 (*.f64 (Rewrite<= mul-1-neg_binary64 (*.f64 -1 (/.f64 z y))) x) (*.f64 (/.f64 1 y) x)) (*.f64 4 (/.f64 1 y))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= distribute-rgt-in_binary64 (*.f64 x (+.f64 (*.f64 -1 (/.f64 z y)) (/.f64 1 y)))) (*.f64 4 (/.f64 1 y))): 1 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 x (Rewrite<= +-commutative_binary64 (+.f64 (/.f64 1 y) (*.f64 -1 (/.f64 z y))))) (*.f64 4 (/.f64 1 y))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 x (+.f64 (/.f64 1 y) (Rewrite=> mul-1-neg_binary64 (neg.f64 (/.f64 z y))))) (*.f64 4 (/.f64 1 y))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 x (Rewrite<= sub-neg_binary64 (-.f64 (/.f64 1 y) (/.f64 z y)))) (*.f64 4 (/.f64 1 y))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= *-commutative_binary64 (*.f64 (-.f64 (/.f64 1 y) (/.f64 z y)) x)) (*.f64 4 (/.f64 1 y))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 4 (/.f64 1 y)) (*.f64 (-.f64 (/.f64 1 y) (/.f64 z y)) x))): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification0.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -5 \cdot 10^{-70}:\\ \;\;\;\;\left|\mathsf{fma}\left(x, \frac{z}{y}, \frac{-4 - x}{y}\right)\right|\\ \mathbf{elif}\;x \leq 1.3 \cdot 10^{-160}:\\ \;\;\;\;\left|\frac{\left(x + 4\right) - x \cdot z}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\mathsf{fma}\left(x, \frac{z}{y}, \frac{-4 - x}{y}\right)\right|\\ \end{array} \]

Alternatives

Alternative 1
Error9.6
Cost7240
\[\begin{array}{l} t_0 := \left|\frac{x}{y} - \frac{x}{\frac{y}{z}}\right|\\ \mathbf{if}\;x \leq -1.35 \cdot 10^{-43}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 2.1 \cdot 10^{-55}:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error0.2
Cost7240
\[\begin{array}{l} t_0 := \left|\frac{x}{y} - \frac{x}{\frac{y}{z}}\right|\\ \mathbf{if}\;x \leq -2 \cdot 10^{+54}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 3.5 \cdot 10^{+16}:\\ \;\;\;\;\left|\frac{\left(x + 4\right) - x \cdot z}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error18.7
Cost7116
\[\begin{array}{l} t_0 := \left|\frac{x}{y}\right|\\ \mathbf{if}\;x \leq -1.52:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.7 \cdot 10^{-54}:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \mathbf{elif}\;x \leq 1.45 \cdot 10^{+27}:\\ \;\;\;\;\left|z \cdot \frac{x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error18.7
Cost7116
\[\begin{array}{l} t_0 := \left|\frac{x}{y}\right|\\ \mathbf{if}\;x \leq -1.52:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.15 \cdot 10^{-54}:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \mathbf{elif}\;x \leq 1.25 \cdot 10^{+27}:\\ \;\;\;\;\left|x \cdot \frac{z}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error9.7
Cost7112
\[\begin{array}{l} t_0 := \left|\frac{z + -1}{\frac{y}{x}}\right|\\ \mathbf{if}\;x \leq -1.35 \cdot 10^{-43}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.7 \cdot 10^{-54}:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error11.1
Cost6984
\[\begin{array}{l} \mathbf{if}\;z \leq -1.45 \cdot 10^{+33}:\\ \;\;\;\;\left|x \cdot \frac{z}{y}\right|\\ \mathbf{elif}\;z \leq 1.3 \cdot 10^{+90}:\\ \;\;\;\;\left|\frac{x + 4}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|z \cdot \frac{x}{y}\right|\\ \end{array} \]
Alternative 7
Error18.1
Cost6856
\[\begin{array}{l} t_0 := \left|\frac{x}{y}\right|\\ \mathbf{if}\;x \leq -1.52:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 4:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error32.0
Cost6592
\[\left|\frac{4}{y}\right| \]

Error

Reproduce

herbie shell --seed 2022329 
(FPCore (x y z)
  :name "fabs fraction 1"
  :precision binary64
  (fabs (- (/ (+ x 4.0) y) (* (/ x y) z))))