fabs fraction 1

?

Percentage Accurate: 91.5% → 98.1%
Time: 6.4s
Precision: binary64
Cost: 7108

?

\[\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right| \]
\[\begin{array}{l} \mathbf{if}\;x \leq -1.15 \cdot 10^{+89}:\\ \;\;\;\;\left|\left(1 - z\right) \cdot \frac{x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{\left(x + 4\right) - x \cdot z}{y}\right|\\ \end{array} \]
(FPCore (x y z) :precision binary64 (fabs (- (/ (+ x 4.0) y) (* (/ x y) z))))
(FPCore (x y z)
 :precision binary64
 (if (<= x -1.15e+89)
   (fabs (* (- 1.0 z) (/ x y)))
   (fabs (/ (- (+ x 4.0) (* x z)) y))))
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 tmp;
	if (x <= -1.15e+89) {
		tmp = fabs(((1.0 - z) * (x / y)));
	} else {
		tmp = fabs((((x + 4.0) - (x * z)) / y));
	}
	return tmp;
}
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
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (x <= (-1.15d+89)) then
        tmp = abs(((1.0d0 - z) * (x / y)))
    else
        tmp = abs((((x + 4.0d0) - (x * z)) / y))
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	return Math.abs((((x + 4.0) / y) - ((x / y) * z)));
}
public static double code(double x, double y, double z) {
	double tmp;
	if (x <= -1.15e+89) {
		tmp = Math.abs(((1.0 - z) * (x / y)));
	} else {
		tmp = Math.abs((((x + 4.0) - (x * z)) / y));
	}
	return tmp;
}
def code(x, y, z):
	return math.fabs((((x + 4.0) / y) - ((x / y) * z)))
def code(x, y, z):
	tmp = 0
	if x <= -1.15e+89:
		tmp = math.fabs(((1.0 - z) * (x / y)))
	else:
		tmp = math.fabs((((x + 4.0) - (x * z)) / y))
	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)
	tmp = 0.0
	if (x <= -1.15e+89)
		tmp = abs(Float64(Float64(1.0 - z) * Float64(x / y)));
	else
		tmp = abs(Float64(Float64(Float64(x + 4.0) - Float64(x * z)) / y));
	end
	return tmp
end
function tmp = code(x, y, z)
	tmp = abs((((x + 4.0) / y) - ((x / y) * z)));
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (x <= -1.15e+89)
		tmp = abs(((1.0 - z) * (x / y)));
	else
		tmp = abs((((x + 4.0) - (x * z)) / y));
	end
	tmp_2 = 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_] := If[LessEqual[x, -1.15e+89], N[Abs[N[(N[(1.0 - z), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Abs[N[(N[(N[(x + 4.0), $MachinePrecision] - N[(x * z), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]]
\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right|
\begin{array}{l}
\mathbf{if}\;x \leq -1.15 \cdot 10^{+89}:\\
\;\;\;\;\left|\left(1 - z\right) \cdot \frac{x}{y}\right|\\

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


\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 7 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

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 2 regimes
  2. if x < -1.1499999999999999e89

    1. Initial program 83.3%

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

      \[\leadsto \left|\color{blue}{\left(\frac{1}{y} - \frac{z}{y}\right) \cdot x}\right| \]
    3. Simplified99.9%

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

      [Start]99.9%

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

      *-commutative [=>]99.9%

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

      sub-neg [=>]99.9%

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

      mul-1-neg [<=]99.9%

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

      distribute-rgt-in [=>]87.4%

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

      associate-*l/ [=>]87.5%

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

      *-lft-identity [=>]87.5%

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

      associate-*r* [<=]87.5%

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

      associate-*l/ [=>]75.8%

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

      associate-*r/ [=>]75.8%

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

      associate-*r* [=>]75.8%

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

      neg-mul-1 [<=]75.8%

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

      associate-*r/ [<=]83.3%

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

      distribute-rgt1-in [=>]99.9%

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

      +-commutative [=>]99.9%

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

      sub-neg [<=]99.9%

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

    if -1.1499999999999999e89 < x

    1. Initial program 94.3%

      \[\left|\frac{x + 4}{y} - \frac{x}{y} \cdot z\right| \]
    2. Applied egg-rr99.4%

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

      [Start]94.3%

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

      associate-*l/ [=>]98.0%

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

      sub-div [=>]99.4%

      \[ \left|\color{blue}{\frac{\left(x + 4\right) - x \cdot z}{y}}\right| \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.15 \cdot 10^{+89}:\\ \;\;\;\;\left|\left(1 - z\right) \cdot \frac{x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{\left(x + 4\right) - x \cdot z}{y}\right|\\ \end{array} \]

Alternatives

Alternative 1
Accuracy98.1%
Cost7108
\[\begin{array}{l} \mathbf{if}\;x \leq -1.15 \cdot 10^{+89}:\\ \;\;\;\;\left|\left(1 - z\right) \cdot \frac{x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{\left(x + 4\right) - x \cdot z}{y}\right|\\ \end{array} \]
Alternative 2
Accuracy68.9%
Cost7381
\[\begin{array}{l} t_0 := \left|z \cdot \frac{x}{y}\right|\\ \mathbf{if}\;x \leq -1.6 \cdot 10^{-22}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.6 \cdot 10^{-55}:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \mathbf{elif}\;x \leq 2500000000 \lor \neg \left(x \leq 6.8 \cdot 10^{+31}\right) \land x \leq 1.45 \cdot 10^{+101}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{x}{y}\right|\\ \end{array} \]
Alternative 3
Accuracy69.0%
Cost7381
\[\begin{array}{l} t_0 := \left|z \cdot \frac{x}{y}\right|\\ \mathbf{if}\;x \leq -3.5 \cdot 10^{-14}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.2 \cdot 10^{-56}:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \mathbf{elif}\;x \leq 1040000000:\\ \;\;\;\;\left|\frac{x \cdot z}{y}\right|\\ \mathbf{elif}\;x \leq 6.4 \cdot 10^{+31} \lor \neg \left(x \leq 9.5 \cdot 10^{+100}\right):\\ \;\;\;\;\left|\frac{x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Accuracy86.3%
Cost7113
\[\begin{array}{l} \mathbf{if}\;x \leq -1.65 \cdot 10^{-18} \lor \neg \left(x \leq 2.25 \cdot 10^{-57}\right):\\ \;\;\;\;\left|\left(1 - z\right) \cdot \frac{x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \end{array} \]
Alternative 5
Accuracy86.2%
Cost6984
\[\begin{array}{l} \mathbf{if}\;z \leq -2.5 \cdot 10^{+55}:\\ \;\;\;\;\left|\frac{x}{\frac{y}{z}}\right|\\ \mathbf{elif}\;z \leq 9.8 \cdot 10^{+79}:\\ \;\;\;\;\left|\frac{-4 - x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|z \cdot \frac{x}{y}\right|\\ \end{array} \]
Alternative 6
Accuracy69.5%
Cost6857
\[\begin{array}{l} \mathbf{if}\;x \leq -1.5 \lor \neg \left(x \leq 4\right):\\ \;\;\;\;\left|\frac{x}{y}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{4}{y}\right|\\ \end{array} \]
Alternative 7
Accuracy40.5%
Cost6592
\[\left|\frac{4}{y}\right| \]

Reproduce?

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