fabs fraction 1

Percentage Accurate: 91.7% → 96.9%
Time: 6.9s
Alternatives: 7
Speedup: 1.6×

Specification

?
\[\begin{array}{l} \\ \left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right| \end{array} \]
(FPCore (x y z) :precision binary64 (fabs (- (/ (+ x 4.0) y) (* (/ x y) z))))
double code(double x, double y, double z) {
	return fabs((((x + 4.0) / y) - ((x / y) * z)));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = abs((((x + 4.0d0) / y) - ((x / y) * z)))
end function
public static double code(double x, double y, double z) {
	return Math.abs((((x + 4.0) / y) - ((x / y) * z)));
}
def code(x, y, z):
	return math.fabs((((x + 4.0) / y) - ((x / y) * z)))
function code(x, y, z)
	return abs(Float64(Float64(Float64(x + 4.0) / y) - Float64(Float64(x / y) * z)))
end
function tmp = code(x, y, z)
	tmp = abs((((x + 4.0) / y) - ((x / y) * z)));
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]
\begin{array}{l}

\\
\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right|
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 7 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 91.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right| \end{array} \]
(FPCore (x y z) :precision binary64 (fabs (- (/ (+ x 4.0) y) (* (/ x y) z))))
double code(double x, double y, double z) {
	return fabs((((x + 4.0) / y) - ((x / y) * z)));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = abs((((x + 4.0d0) / y) - ((x / y) * z)))
end function
public static double code(double x, double y, double z) {
	return Math.abs((((x + 4.0) / y) - ((x / y) * z)));
}
def code(x, y, z):
	return math.fabs((((x + 4.0) / y) - ((x / y) * z)))
function code(x, y, z)
	return abs(Float64(Float64(Float64(x + 4.0) / y) - Float64(Float64(x / y) * z)))
end
function tmp = code(x, y, z)
	tmp = abs((((x + 4.0) / y) - ((x / y) * z)));
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]
\begin{array}{l}

\\
\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right|
\end{array}

Alternative 1: 96.9% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -2 \cdot 10^{+18}:\\ \;\;\;\;\left|\frac{4}{y} - z \cdot \frac{x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{x - \mathsf{fma}\left(x, z, -4\right)}{y}\right|\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= z -2e+18)
   (fabs (- (/ 4.0 y) (* z (/ x y))))
   (fabs (/ (- x (fma x z -4.0)) y))))
double code(double x, double y, double z) {
	double tmp;
	if (z <= -2e+18) {
		tmp = fabs(((4.0 / y) - (z * (x / y))));
	} else {
		tmp = fabs(((x - fma(x, z, -4.0)) / y));
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if (z <= -2e+18)
		tmp = abs(Float64(Float64(4.0 / y) - Float64(z * Float64(x / y))));
	else
		tmp = abs(Float64(Float64(x - fma(x, z, -4.0)) / y));
	end
	return tmp
end
code[x_, y_, z_] := If[LessEqual[z, -2e+18], N[Abs[N[(N[(4.0 / y), $MachinePrecision] - N[(z * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Abs[N[(N[(x - N[(x * z + -4.0), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq -2 \cdot 10^{+18}:\\
\;\;\;\;\left|\frac{4}{y} - z \cdot \frac{x}{y}\right|\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -2e18

    1. Initial program 99.8%

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

      \[\leadsto \left|\color{blue}{\frac{4}{y}} - \frac{x}{y} \cdot z\right| \]
    4. Step-by-step derivation
      1. lower-/.f6499.8

        \[\leadsto \left|\color{blue}{\frac{4}{y}} - \frac{x}{y} \cdot z\right| \]
    5. Simplified99.8%

      \[\leadsto \left|\color{blue}{\frac{4}{y}} - \frac{x}{y} \cdot z\right| \]

    if -2e18 < z

    1. Initial program 89.4%

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

      \[\leadsto \left|\color{blue}{x \cdot \left(\frac{1}{y} - \frac{z}{y}\right) + 4 \cdot \frac{1}{y}}\right| \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} + x \cdot \left(\frac{1}{y} - \frac{z}{y}\right)}\right| \]
      2. sub-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + x \cdot \color{blue}{\left(\frac{1}{y} + \left(\mathsf{neg}\left(\frac{z}{y}\right)\right)\right)}\right| \]
      3. mul-1-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + x \cdot \left(\frac{1}{y} + \color{blue}{-1 \cdot \frac{z}{y}}\right)\right| \]
      4. +-commutativeN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + x \cdot \color{blue}{\left(-1 \cdot \frac{z}{y} + \frac{1}{y}\right)}\right| \]
      5. distribute-lft-inN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \color{blue}{\left(x \cdot \left(-1 \cdot \frac{z}{y}\right) + x \cdot \frac{1}{y}\right)}\right| \]
      6. mul-1-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \left(x \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{z}{y}\right)\right)} + x \cdot \frac{1}{y}\right)\right| \]
      7. distribute-rgt-neg-inN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\color{blue}{\left(\mathsf{neg}\left(x \cdot \frac{z}{y}\right)\right)} + x \cdot \frac{1}{y}\right)\right| \]
      8. associate-/l*N/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\left(\mathsf{neg}\left(\color{blue}{\frac{x \cdot z}{y}}\right)\right) + x \cdot \frac{1}{y}\right)\right| \]
      9. mul-1-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\color{blue}{-1 \cdot \frac{x \cdot z}{y}} + x \cdot \frac{1}{y}\right)\right| \]
      10. associate-+r+N/A

        \[\leadsto \left|\color{blue}{\left(4 \cdot \frac{1}{y} + -1 \cdot \frac{x \cdot z}{y}\right) + x \cdot \frac{1}{y}}\right| \]
      11. +-commutativeN/A

        \[\leadsto \left|\color{blue}{\left(-1 \cdot \frac{x \cdot z}{y} + 4 \cdot \frac{1}{y}\right)} + x \cdot \frac{1}{y}\right| \]
      12. associate-*r/N/A

        \[\leadsto \left|\left(-1 \cdot \frac{x \cdot z}{y} + 4 \cdot \frac{1}{y}\right) + \color{blue}{\frac{x \cdot 1}{y}}\right| \]
      13. *-rgt-identityN/A

        \[\leadsto \left|\left(-1 \cdot \frac{x \cdot z}{y} + 4 \cdot \frac{1}{y}\right) + \frac{\color{blue}{x}}{y}\right| \]
      14. +-commutativeN/A

        \[\leadsto \left|\color{blue}{\frac{x}{y} + \left(-1 \cdot \frac{x \cdot z}{y} + 4 \cdot \frac{1}{y}\right)}\right| \]
      15. associate-+r+N/A

        \[\leadsto \left|\color{blue}{\left(\frac{x}{y} + -1 \cdot \frac{x \cdot z}{y}\right) + 4 \cdot \frac{1}{y}}\right| \]
    5. Simplified98.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -2 \cdot 10^{+18}:\\ \;\;\;\;\left|\frac{4}{y} - z \cdot \frac{x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{x - \mathsf{fma}\left(x, z, -4\right)}{y}\right|\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 94.7% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left|\frac{\mathsf{fma}\left(x, z, -4\right)}{y}\right|\\ \mathbf{if}\;z \leq -280000:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 6.5 \cdot 10^{-34}:\\ \;\;\;\;\left|\frac{4 + x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (fabs (/ (fma x z -4.0) y))))
   (if (<= z -280000.0) t_0 (if (<= z 6.5e-34) (fabs (/ (+ 4.0 x) y)) t_0))))
double code(double x, double y, double z) {
	double t_0 = fabs((fma(x, z, -4.0) / y));
	double tmp;
	if (z <= -280000.0) {
		tmp = t_0;
	} else if (z <= 6.5e-34) {
		tmp = fabs(((4.0 + x) / y));
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x, y, z)
	t_0 = abs(Float64(fma(x, z, -4.0) / y))
	tmp = 0.0
	if (z <= -280000.0)
		tmp = t_0;
	elseif (z <= 6.5e-34)
		tmp = abs(Float64(Float64(4.0 + x) / y));
	else
		tmp = t_0;
	end
	return tmp
end
code[x_, y_, z_] := Block[{t$95$0 = N[Abs[N[(N[(x * z + -4.0), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[z, -280000.0], t$95$0, If[LessEqual[z, 6.5e-34], N[Abs[N[(N[(4.0 + x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left|\frac{\mathsf{fma}\left(x, z, -4\right)}{y}\right|\\
\mathbf{if}\;z \leq -280000:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq 6.5 \cdot 10^{-34}:\\
\;\;\;\;\left|\frac{4 + x}{y}\right|\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -2.8e5 or 6.49999999999999985e-34 < z

    1. Initial program 89.6%

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

      \[\leadsto \left|\color{blue}{\frac{4}{y}} - \frac{x}{y} \cdot z\right| \]
    4. Step-by-step derivation
      1. lower-/.f6495.5

        \[\leadsto \left|\color{blue}{\frac{4}{y}} - \frac{x}{y} \cdot z\right| \]
    5. Simplified95.5%

      \[\leadsto \left|\color{blue}{\frac{4}{y}} - \frac{x}{y} \cdot z\right| \]
    6. Taylor expanded in y around 0

      \[\leadsto \color{blue}{\left|4 \cdot \frac{1}{y} - \frac{x \cdot z}{y}\right|} \]
    7. Step-by-step derivation
      1. fabs-subN/A

        \[\leadsto \color{blue}{\left|\frac{x \cdot z}{y} - 4 \cdot \frac{1}{y}\right|} \]
      2. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\frac{x \cdot z}{y} - 4 \cdot \frac{1}{y}\right|} \]
      3. associate-*r/N/A

        \[\leadsto \left|\frac{x \cdot z}{y} - \color{blue}{\frac{4 \cdot 1}{y}}\right| \]
      4. metadata-evalN/A

        \[\leadsto \left|\frac{x \cdot z}{y} - \frac{\color{blue}{4}}{y}\right| \]
      5. div-subN/A

        \[\leadsto \left|\color{blue}{\frac{x \cdot z - 4}{y}}\right| \]
      6. lower-/.f64N/A

        \[\leadsto \left|\color{blue}{\frac{x \cdot z - 4}{y}}\right| \]
      7. sub-negN/A

        \[\leadsto \left|\frac{\color{blue}{x \cdot z + \left(\mathsf{neg}\left(4\right)\right)}}{y}\right| \]
      8. lower-fma.f64N/A

        \[\leadsto \left|\frac{\color{blue}{\mathsf{fma}\left(x, z, \mathsf{neg}\left(4\right)\right)}}{y}\right| \]
      9. metadata-eval93.1

        \[\leadsto \left|\frac{\mathsf{fma}\left(x, z, \color{blue}{-4}\right)}{y}\right| \]
    8. Simplified93.1%

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

    if -2.8e5 < z < 6.49999999999999985e-34

    1. Initial program 93.3%

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

      \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} + \frac{x}{y}}\right| \]
    4. Step-by-step derivation
      1. remove-double-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\frac{x}{y}\right)\right)\right)\right)}\right| \]
      2. mul-1-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\mathsf{neg}\left(\color{blue}{-1 \cdot \frac{x}{y}}\right)\right)\right| \]
      3. sub-negN/A

        \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} - -1 \cdot \frac{x}{y}}\right| \]
      4. associate-*r/N/A

        \[\leadsto \left|\color{blue}{\frac{4 \cdot 1}{y}} - -1 \cdot \frac{x}{y}\right| \]
      5. metadata-evalN/A

        \[\leadsto \left|\frac{\color{blue}{4}}{y} - -1 \cdot \frac{x}{y}\right| \]
      6. associate-*r/N/A

        \[\leadsto \left|\frac{4}{y} - \color{blue}{\frac{-1 \cdot x}{y}}\right| \]
      7. div-subN/A

        \[\leadsto \left|\color{blue}{\frac{4 - -1 \cdot x}{y}}\right| \]
      8. unsub-negN/A

        \[\leadsto \left|\frac{\color{blue}{4 + \left(\mathsf{neg}\left(-1 \cdot x\right)\right)}}{y}\right| \]
      9. neg-mul-1N/A

        \[\leadsto \left|\frac{4 + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right)\right)}{y}\right| \]
      10. remove-double-negN/A

        \[\leadsto \left|\frac{4 + \color{blue}{x}}{y}\right| \]
      11. lower-/.f64N/A

        \[\leadsto \left|\color{blue}{\frac{4 + x}{y}}\right| \]
      12. +-commutativeN/A

        \[\leadsto \left|\frac{\color{blue}{x + 4}}{y}\right| \]
      13. lower-+.f6499.3

        \[\leadsto \left|\frac{\color{blue}{x + 4}}{y}\right| \]
    5. Simplified99.3%

      \[\leadsto \left|\color{blue}{\frac{x + 4}{y}}\right| \]
  3. Recombined 2 regimes into one program.
  4. Final simplification96.4%

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

Alternative 3: 84.4% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left|\frac{z \cdot x}{y}\right|\\ \mathbf{if}\;z \leq -1.05 \cdot 10^{+50}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 5.1 \cdot 10^{+47}:\\ \;\;\;\;\left|\frac{4 + x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (fabs (/ (* z x) y))))
   (if (<= z -1.05e+50) t_0 (if (<= z 5.1e+47) (fabs (/ (+ 4.0 x) y)) t_0))))
double code(double x, double y, double z) {
	double t_0 = fabs(((z * x) / y));
	double tmp;
	if (z <= -1.05e+50) {
		tmp = t_0;
	} else if (z <= 5.1e+47) {
		tmp = fabs(((4.0 + x) / y));
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = abs(((z * x) / y))
    if (z <= (-1.05d+50)) then
        tmp = t_0
    else if (z <= 5.1d+47) then
        tmp = abs(((4.0d0 + x) / y))
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = Math.abs(((z * x) / y));
	double tmp;
	if (z <= -1.05e+50) {
		tmp = t_0;
	} else if (z <= 5.1e+47) {
		tmp = Math.abs(((4.0 + x) / y));
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = math.fabs(((z * x) / y))
	tmp = 0
	if z <= -1.05e+50:
		tmp = t_0
	elif z <= 5.1e+47:
		tmp = math.fabs(((4.0 + x) / y))
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = abs(Float64(Float64(z * x) / y))
	tmp = 0.0
	if (z <= -1.05e+50)
		tmp = t_0;
	elseif (z <= 5.1e+47)
		tmp = abs(Float64(Float64(4.0 + x) / y));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = abs(((z * x) / y));
	tmp = 0.0;
	if (z <= -1.05e+50)
		tmp = t_0;
	elseif (z <= 5.1e+47)
		tmp = abs(((4.0 + x) / y));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[Abs[N[(N[(z * x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[z, -1.05e+50], t$95$0, If[LessEqual[z, 5.1e+47], N[Abs[N[(N[(4.0 + x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left|\frac{z \cdot x}{y}\right|\\
\mathbf{if}\;z \leq -1.05 \cdot 10^{+50}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq 5.1 \cdot 10^{+47}:\\
\;\;\;\;\left|\frac{4 + x}{y}\right|\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -1.05e50 or 5.1000000000000001e47 < z

    1. Initial program 89.0%

      \[\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right| \]
    2. Add Preprocessing
    3. Taylor expanded in z around inf

      \[\leadsto \left|\color{blue}{-1 \cdot \frac{x \cdot z}{y}}\right| \]
    4. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto \left|\color{blue}{\frac{-1 \cdot \left(x \cdot z\right)}{y}}\right| \]
      2. lower-/.f64N/A

        \[\leadsto \left|\color{blue}{\frac{-1 \cdot \left(x \cdot z\right)}{y}}\right| \]
      3. associate-*r*N/A

        \[\leadsto \left|\frac{\color{blue}{\left(-1 \cdot x\right) \cdot z}}{y}\right| \]
      4. *-commutativeN/A

        \[\leadsto \left|\frac{\color{blue}{z \cdot \left(-1 \cdot x\right)}}{y}\right| \]
      5. lower-*.f64N/A

        \[\leadsto \left|\frac{\color{blue}{z \cdot \left(-1 \cdot x\right)}}{y}\right| \]
      6. neg-mul-1N/A

        \[\leadsto \left|\frac{z \cdot \color{blue}{\left(\mathsf{neg}\left(x\right)\right)}}{y}\right| \]
      7. lower-neg.f6476.7

        \[\leadsto \left|\frac{z \cdot \color{blue}{\left(-x\right)}}{y}\right| \]
    5. Simplified76.7%

      \[\leadsto \left|\color{blue}{\frac{z \cdot \left(-x\right)}{y}}\right| \]
    6. Taylor expanded in z around 0

      \[\leadsto \color{blue}{\left|-1 \cdot \frac{x \cdot z}{y}\right|} \]
    7. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \left|\color{blue}{\mathsf{neg}\left(\frac{x \cdot z}{y}\right)}\right| \]
      2. fabs-negN/A

        \[\leadsto \color{blue}{\left|\frac{x \cdot z}{y}\right|} \]
      3. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\frac{x \cdot z}{y}\right|} \]
      4. lower-/.f64N/A

        \[\leadsto \left|\color{blue}{\frac{x \cdot z}{y}}\right| \]
      5. lower-*.f6476.7

        \[\leadsto \left|\frac{\color{blue}{x \cdot z}}{y}\right| \]
    8. Simplified76.7%

      \[\leadsto \color{blue}{\left|\frac{x \cdot z}{y}\right|} \]

    if -1.05e50 < z < 5.1000000000000001e47

    1. Initial program 92.8%

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

      \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} + \frac{x}{y}}\right| \]
    4. Step-by-step derivation
      1. remove-double-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\frac{x}{y}\right)\right)\right)\right)}\right| \]
      2. mul-1-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\mathsf{neg}\left(\color{blue}{-1 \cdot \frac{x}{y}}\right)\right)\right| \]
      3. sub-negN/A

        \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} - -1 \cdot \frac{x}{y}}\right| \]
      4. associate-*r/N/A

        \[\leadsto \left|\color{blue}{\frac{4 \cdot 1}{y}} - -1 \cdot \frac{x}{y}\right| \]
      5. metadata-evalN/A

        \[\leadsto \left|\frac{\color{blue}{4}}{y} - -1 \cdot \frac{x}{y}\right| \]
      6. associate-*r/N/A

        \[\leadsto \left|\frac{4}{y} - \color{blue}{\frac{-1 \cdot x}{y}}\right| \]
      7. div-subN/A

        \[\leadsto \left|\color{blue}{\frac{4 - -1 \cdot x}{y}}\right| \]
      8. unsub-negN/A

        \[\leadsto \left|\frac{\color{blue}{4 + \left(\mathsf{neg}\left(-1 \cdot x\right)\right)}}{y}\right| \]
      9. neg-mul-1N/A

        \[\leadsto \left|\frac{4 + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right)\right)}{y}\right| \]
      10. remove-double-negN/A

        \[\leadsto \left|\frac{4 + \color{blue}{x}}{y}\right| \]
      11. lower-/.f64N/A

        \[\leadsto \left|\color{blue}{\frac{4 + x}{y}}\right| \]
      12. +-commutativeN/A

        \[\leadsto \left|\frac{\color{blue}{x + 4}}{y}\right| \]
      13. lower-+.f6495.0

        \[\leadsto \left|\frac{\color{blue}{x + 4}}{y}\right| \]
    5. Simplified95.0%

      \[\leadsto \left|\color{blue}{\frac{x + 4}{y}}\right| \]
  3. Recombined 2 regimes into one program.
  4. Final simplification88.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -1.05 \cdot 10^{+50}:\\ \;\;\;\;\left|\frac{z \cdot x}{y}\right|\\ \mathbf{elif}\;z \leq 5.1 \cdot 10^{+47}:\\ \;\;\;\;\left|\frac{4 + x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{z \cdot x}{y}\right|\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 69.0% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left|\frac{x}{y}\right|\\ \mathbf{if}\;x \leq -10:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 4:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (fabs (/ x y))))
   (if (<= x -10.0) t_0 (if (<= x 4.0) (fabs (/ 4.0 y)) t_0))))
double code(double x, double y, double z) {
	double t_0 = fabs((x / y));
	double tmp;
	if (x <= -10.0) {
		tmp = t_0;
	} else if (x <= 4.0) {
		tmp = fabs((4.0 / y));
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = abs((x / y))
    if (x <= (-10.0d0)) then
        tmp = t_0
    else if (x <= 4.0d0) then
        tmp = abs((4.0d0 / y))
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = Math.abs((x / y));
	double tmp;
	if (x <= -10.0) {
		tmp = t_0;
	} else if (x <= 4.0) {
		tmp = Math.abs((4.0 / y));
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = math.fabs((x / y))
	tmp = 0
	if x <= -10.0:
		tmp = t_0
	elif x <= 4.0:
		tmp = math.fabs((4.0 / y))
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = abs(Float64(x / y))
	tmp = 0.0
	if (x <= -10.0)
		tmp = t_0;
	elseif (x <= 4.0)
		tmp = abs(Float64(4.0 / y));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = abs((x / y));
	tmp = 0.0;
	if (x <= -10.0)
		tmp = t_0;
	elseif (x <= 4.0)
		tmp = abs((4.0 / y));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[x, -10.0], t$95$0, If[LessEqual[x, 4.0], N[Abs[N[(4.0 / y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left|\frac{x}{y}\right|\\
\mathbf{if}\;x \leq -10:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;x \leq 4:\\
\;\;\;\;\left|\frac{4}{y}\right|\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -10 or 4 < x

    1. Initial program 87.7%

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

      \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} + \frac{x}{y}}\right| \]
    4. Step-by-step derivation
      1. remove-double-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\frac{x}{y}\right)\right)\right)\right)}\right| \]
      2. mul-1-negN/A

        \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\mathsf{neg}\left(\color{blue}{-1 \cdot \frac{x}{y}}\right)\right)\right| \]
      3. sub-negN/A

        \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} - -1 \cdot \frac{x}{y}}\right| \]
      4. associate-*r/N/A

        \[\leadsto \left|\color{blue}{\frac{4 \cdot 1}{y}} - -1 \cdot \frac{x}{y}\right| \]
      5. metadata-evalN/A

        \[\leadsto \left|\frac{\color{blue}{4}}{y} - -1 \cdot \frac{x}{y}\right| \]
      6. associate-*r/N/A

        \[\leadsto \left|\frac{4}{y} - \color{blue}{\frac{-1 \cdot x}{y}}\right| \]
      7. div-subN/A

        \[\leadsto \left|\color{blue}{\frac{4 - -1 \cdot x}{y}}\right| \]
      8. unsub-negN/A

        \[\leadsto \left|\frac{\color{blue}{4 + \left(\mathsf{neg}\left(-1 \cdot x\right)\right)}}{y}\right| \]
      9. neg-mul-1N/A

        \[\leadsto \left|\frac{4 + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right)\right)}{y}\right| \]
      10. remove-double-negN/A

        \[\leadsto \left|\frac{4 + \color{blue}{x}}{y}\right| \]
      11. lower-/.f64N/A

        \[\leadsto \left|\color{blue}{\frac{4 + x}{y}}\right| \]
      12. +-commutativeN/A

        \[\leadsto \left|\frac{\color{blue}{x + 4}}{y}\right| \]
      13. lower-+.f6468.7

        \[\leadsto \left|\frac{\color{blue}{x + 4}}{y}\right| \]
    5. Simplified68.7%

      \[\leadsto \left|\color{blue}{\frac{x + 4}{y}}\right| \]
    6. Taylor expanded in x around inf

      \[\leadsto \left|\color{blue}{\frac{x}{y}}\right| \]
    7. Step-by-step derivation
      1. lower-/.f6467.1

        \[\leadsto \left|\color{blue}{\frac{x}{y}}\right| \]
    8. Simplified67.1%

      \[\leadsto \left|\color{blue}{\frac{x}{y}}\right| \]

    if -10 < x < 4

    1. Initial program 96.1%

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

      \[\leadsto \left|\color{blue}{\frac{4}{y}}\right| \]
    4. Step-by-step derivation
      1. lower-/.f6478.7

        \[\leadsto \left|\color{blue}{\frac{4}{y}}\right| \]
    5. Simplified78.7%

      \[\leadsto \left|\color{blue}{\frac{4}{y}}\right| \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 96.3% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \left|\frac{x - \mathsf{fma}\left(x, z, -4\right)}{y}\right| \end{array} \]
(FPCore (x y z) :precision binary64 (fabs (/ (- x (fma x z -4.0)) y)))
double code(double x, double y, double z) {
	return fabs(((x - fma(x, z, -4.0)) / y));
}
function code(x, y, z)
	return abs(Float64(Float64(x - fma(x, z, -4.0)) / y))
end
code[x_, y_, z_] := N[Abs[N[(N[(x - N[(x * z + -4.0), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\left|\frac{x - \mathsf{fma}\left(x, z, -4\right)}{y}\right|
\end{array}
Derivation
  1. Initial program 91.5%

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

    \[\leadsto \left|\color{blue}{x \cdot \left(\frac{1}{y} - \frac{z}{y}\right) + 4 \cdot \frac{1}{y}}\right| \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} + x \cdot \left(\frac{1}{y} - \frac{z}{y}\right)}\right| \]
    2. sub-negN/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + x \cdot \color{blue}{\left(\frac{1}{y} + \left(\mathsf{neg}\left(\frac{z}{y}\right)\right)\right)}\right| \]
    3. mul-1-negN/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + x \cdot \left(\frac{1}{y} + \color{blue}{-1 \cdot \frac{z}{y}}\right)\right| \]
    4. +-commutativeN/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + x \cdot \color{blue}{\left(-1 \cdot \frac{z}{y} + \frac{1}{y}\right)}\right| \]
    5. distribute-lft-inN/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + \color{blue}{\left(x \cdot \left(-1 \cdot \frac{z}{y}\right) + x \cdot \frac{1}{y}\right)}\right| \]
    6. mul-1-negN/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + \left(x \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{z}{y}\right)\right)} + x \cdot \frac{1}{y}\right)\right| \]
    7. distribute-rgt-neg-inN/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\color{blue}{\left(\mathsf{neg}\left(x \cdot \frac{z}{y}\right)\right)} + x \cdot \frac{1}{y}\right)\right| \]
    8. associate-/l*N/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\left(\mathsf{neg}\left(\color{blue}{\frac{x \cdot z}{y}}\right)\right) + x \cdot \frac{1}{y}\right)\right| \]
    9. mul-1-negN/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\color{blue}{-1 \cdot \frac{x \cdot z}{y}} + x \cdot \frac{1}{y}\right)\right| \]
    10. associate-+r+N/A

      \[\leadsto \left|\color{blue}{\left(4 \cdot \frac{1}{y} + -1 \cdot \frac{x \cdot z}{y}\right) + x \cdot \frac{1}{y}}\right| \]
    11. +-commutativeN/A

      \[\leadsto \left|\color{blue}{\left(-1 \cdot \frac{x \cdot z}{y} + 4 \cdot \frac{1}{y}\right)} + x \cdot \frac{1}{y}\right| \]
    12. associate-*r/N/A

      \[\leadsto \left|\left(-1 \cdot \frac{x \cdot z}{y} + 4 \cdot \frac{1}{y}\right) + \color{blue}{\frac{x \cdot 1}{y}}\right| \]
    13. *-rgt-identityN/A

      \[\leadsto \left|\left(-1 \cdot \frac{x \cdot z}{y} + 4 \cdot \frac{1}{y}\right) + \frac{\color{blue}{x}}{y}\right| \]
    14. +-commutativeN/A

      \[\leadsto \left|\color{blue}{\frac{x}{y} + \left(-1 \cdot \frac{x \cdot z}{y} + 4 \cdot \frac{1}{y}\right)}\right| \]
    15. associate-+r+N/A

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

    \[\leadsto \left|\color{blue}{\frac{x - \mathsf{fma}\left(x, z, -4\right)}{y}}\right| \]
  6. Add Preprocessing

Alternative 6: 69.9% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \left|\frac{4 + x}{y}\right| \end{array} \]
(FPCore (x y z) :precision binary64 (fabs (/ (+ 4.0 x) y)))
double code(double x, double y, double z) {
	return fabs(((4.0 + x) / y));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = abs(((4.0d0 + x) / y))
end function
public static double code(double x, double y, double z) {
	return Math.abs(((4.0 + x) / y));
}
def code(x, y, z):
	return math.fabs(((4.0 + x) / y))
function code(x, y, z)
	return abs(Float64(Float64(4.0 + x) / y))
end
function tmp = code(x, y, z)
	tmp = abs(((4.0 + x) / y));
end
code[x_, y_, z_] := N[Abs[N[(N[(4.0 + x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\left|\frac{4 + x}{y}\right|
\end{array}
Derivation
  1. Initial program 91.5%

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

    \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} + \frac{x}{y}}\right| \]
  4. Step-by-step derivation
    1. remove-double-negN/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\frac{x}{y}\right)\right)\right)\right)}\right| \]
    2. mul-1-negN/A

      \[\leadsto \left|4 \cdot \frac{1}{y} + \left(\mathsf{neg}\left(\color{blue}{-1 \cdot \frac{x}{y}}\right)\right)\right| \]
    3. sub-negN/A

      \[\leadsto \left|\color{blue}{4 \cdot \frac{1}{y} - -1 \cdot \frac{x}{y}}\right| \]
    4. associate-*r/N/A

      \[\leadsto \left|\color{blue}{\frac{4 \cdot 1}{y}} - -1 \cdot \frac{x}{y}\right| \]
    5. metadata-evalN/A

      \[\leadsto \left|\frac{\color{blue}{4}}{y} - -1 \cdot \frac{x}{y}\right| \]
    6. associate-*r/N/A

      \[\leadsto \left|\frac{4}{y} - \color{blue}{\frac{-1 \cdot x}{y}}\right| \]
    7. div-subN/A

      \[\leadsto \left|\color{blue}{\frac{4 - -1 \cdot x}{y}}\right| \]
    8. unsub-negN/A

      \[\leadsto \left|\frac{\color{blue}{4 + \left(\mathsf{neg}\left(-1 \cdot x\right)\right)}}{y}\right| \]
    9. neg-mul-1N/A

      \[\leadsto \left|\frac{4 + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right)\right)}{y}\right| \]
    10. remove-double-negN/A

      \[\leadsto \left|\frac{4 + \color{blue}{x}}{y}\right| \]
    11. lower-/.f64N/A

      \[\leadsto \left|\color{blue}{\frac{4 + x}{y}}\right| \]
    12. +-commutativeN/A

      \[\leadsto \left|\frac{\color{blue}{x + 4}}{y}\right| \]
    13. lower-+.f6473.8

      \[\leadsto \left|\frac{\color{blue}{x + 4}}{y}\right| \]
  5. Simplified73.8%

    \[\leadsto \left|\color{blue}{\frac{x + 4}{y}}\right| \]
  6. Final simplification73.8%

    \[\leadsto \left|\frac{4 + x}{y}\right| \]
  7. Add Preprocessing

Alternative 7: 40.3% accurate, 2.6× speedup?

\[\begin{array}{l} \\ \left|\frac{4}{y}\right| \end{array} \]
(FPCore (x y z) :precision binary64 (fabs (/ 4.0 y)))
double code(double x, double y, double z) {
	return fabs((4.0 / y));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = abs((4.0d0 / y))
end function
public static double code(double x, double y, double z) {
	return Math.abs((4.0 / y));
}
def code(x, y, z):
	return math.fabs((4.0 / y))
function code(x, y, z)
	return abs(Float64(4.0 / y))
end
function tmp = code(x, y, z)
	tmp = abs((4.0 / y));
end
code[x_, y_, z_] := N[Abs[N[(4.0 / y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\left|\frac{4}{y}\right|
\end{array}
Derivation
  1. Initial program 91.5%

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

    \[\leadsto \left|\color{blue}{\frac{4}{y}}\right| \]
  4. Step-by-step derivation
    1. lower-/.f6438.6

      \[\leadsto \left|\color{blue}{\frac{4}{y}}\right| \]
  5. Simplified38.6%

    \[\leadsto \left|\color{blue}{\frac{4}{y}}\right| \]
  6. Add Preprocessing

Reproduce

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