Numeric.AD.Rank1.Halley:findZero from ad-4.2.4

Percentage Accurate: 82.7% → 94.5%
Time: 6.0s
Alternatives: 3
Speedup: 1.4×

Specification

?
\[\begin{array}{l} \\ x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (- x (/ (* (* y 2.0) z) (- (* (* z 2.0) z) (* y t)))))
double code(double x, double y, double z, double t) {
	return x - (((y * 2.0) * z) / (((z * 2.0) * z) - (y * t)));
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = x - (((y * 2.0d0) * z) / (((z * 2.0d0) * z) - (y * t)))
end function
public static double code(double x, double y, double z, double t) {
	return x - (((y * 2.0) * z) / (((z * 2.0) * z) - (y * t)));
}
def code(x, y, z, t):
	return x - (((y * 2.0) * z) / (((z * 2.0) * z) - (y * t)))
function code(x, y, z, t)
	return Float64(x - Float64(Float64(Float64(y * 2.0) * z) / Float64(Float64(Float64(z * 2.0) * z) - Float64(y * t))))
end
function tmp = code(x, y, z, t)
	tmp = x - (((y * 2.0) * z) / (((z * 2.0) * z) - (y * t)));
end
code[x_, y_, z_, t_] := N[(x - N[(N[(N[(y * 2.0), $MachinePrecision] * z), $MachinePrecision] / N[(N[(N[(z * 2.0), $MachinePrecision] * z), $MachinePrecision] - N[(y * t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t}
\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 3 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: 82.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (- x (/ (* (* y 2.0) z) (- (* (* z 2.0) z) (* y t)))))
double code(double x, double y, double z, double t) {
	return x - (((y * 2.0) * z) / (((z * 2.0) * z) - (y * t)));
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = x - (((y * 2.0d0) * z) / (((z * 2.0d0) * z) - (y * t)))
end function
public static double code(double x, double y, double z, double t) {
	return x - (((y * 2.0) * z) / (((z * 2.0) * z) - (y * t)));
}
def code(x, y, z, t):
	return x - (((y * 2.0) * z) / (((z * 2.0) * z) - (y * t)))
function code(x, y, z, t)
	return Float64(x - Float64(Float64(Float64(y * 2.0) * z) / Float64(Float64(Float64(z * 2.0) * z) - Float64(y * t))))
end
function tmp = code(x, y, z, t)
	tmp = x - (((y * 2.0) * z) / (((z * 2.0) * z) - (y * t)));
end
code[x_, y_, z_, t_] := N[(x - N[(N[(N[(y * 2.0), $MachinePrecision] * z), $MachinePrecision] / N[(N[(N[(z * 2.0), $MachinePrecision] * z), $MachinePrecision] - N[(y * t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t}
\end{array}

Alternative 1: 94.5% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \leq 5 \cdot 10^{+271}:\\
\;\;\;\;\mathsf{fma}\left(z + z, \frac{y}{\mathsf{fma}\left(-2, z \cdot z, t \cdot y\right)}, x\right)\\

\mathbf{else}:\\
\;\;\;\;x - \frac{y}{z}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 x (/.f64 (*.f64 (*.f64 y #s(literal 2 binary64)) z) (-.f64 (*.f64 (*.f64 z #s(literal 2 binary64)) z) (*.f64 y t)))) < 5.0000000000000003e271

    1. Initial program 96.1%

      \[x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \]
    2. Add Preprocessing
    3. Applied rewrites98.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z \cdot 2, \frac{y}{\mathsf{fma}\left(-2, z \cdot z, t \cdot y\right)}, x\right)} \]
    4. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{z \cdot 2}, \frac{y}{\mathsf{fma}\left(-2, z \cdot z, t \cdot y\right)}, x\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{2 \cdot z}, \frac{y}{\mathsf{fma}\left(-2, z \cdot z, t \cdot y\right)}, x\right) \]
      3. count-2-revN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{z + z}, \frac{y}{\mathsf{fma}\left(-2, z \cdot z, t \cdot y\right)}, x\right) \]
      4. lower-+.f6498.0

        \[\leadsto \mathsf{fma}\left(\color{blue}{z + z}, \frac{y}{\mathsf{fma}\left(-2, z \cdot z, t \cdot y\right)}, x\right) \]
    5. Applied rewrites98.0%

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

    if 5.0000000000000003e271 < (-.f64 x (/.f64 (*.f64 (*.f64 y #s(literal 2 binary64)) z) (-.f64 (*.f64 (*.f64 z #s(literal 2 binary64)) z) (*.f64 y t))))

    1. Initial program 3.0%

      \[x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0

      \[\leadsto x - \color{blue}{\frac{y}{z}} \]
    4. Step-by-step derivation
      1. lower-/.f6485.4

        \[\leadsto x - \color{blue}{\frac{y}{z}} \]
    5. Applied rewrites85.4%

      \[\leadsto x - \color{blue}{\frac{y}{z}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 88.8% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -1.35 \cdot 10^{+52} \lor \neg \left(z \leq 25000000\right):\\ \;\;\;\;x - \frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{z}{t}, 2, x\right)\\ \end{array} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (if (or (<= z -1.35e+52) (not (<= z 25000000.0)))
   (- x (/ y z))
   (fma (/ z t) 2.0 x)))
double code(double x, double y, double z, double t) {
	double tmp;
	if ((z <= -1.35e+52) || !(z <= 25000000.0)) {
		tmp = x - (y / z);
	} else {
		tmp = fma((z / t), 2.0, x);
	}
	return tmp;
}
function code(x, y, z, t)
	tmp = 0.0
	if ((z <= -1.35e+52) || !(z <= 25000000.0))
		tmp = Float64(x - Float64(y / z));
	else
		tmp = fma(Float64(z / t), 2.0, x);
	end
	return tmp
end
code[x_, y_, z_, t_] := If[Or[LessEqual[z, -1.35e+52], N[Not[LessEqual[z, 25000000.0]], $MachinePrecision]], N[(x - N[(y / z), $MachinePrecision]), $MachinePrecision], N[(N[(z / t), $MachinePrecision] * 2.0 + x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.35 \cdot 10^{+52} \lor \neg \left(z \leq 25000000\right):\\
\;\;\;\;x - \frac{y}{z}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -1.35e52 or 2.5e7 < z

    1. Initial program 67.7%

      \[x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0

      \[\leadsto x - \color{blue}{\frac{y}{z}} \]
    4. Step-by-step derivation
      1. lower-/.f6494.2

        \[\leadsto x - \color{blue}{\frac{y}{z}} \]
    5. Applied rewrites94.2%

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

    if -1.35e52 < z < 2.5e7

    1. Initial program 94.1%

      \[x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf

      \[\leadsto \color{blue}{x - -2 \cdot \frac{z}{t}} \]
    4. Step-by-step derivation
      1. fp-cancel-sub-sign-invN/A

        \[\leadsto \color{blue}{x + \left(\mathsf{neg}\left(-2\right)\right) \cdot \frac{z}{t}} \]
      2. metadata-evalN/A

        \[\leadsto x + \color{blue}{2} \cdot \frac{z}{t} \]
      3. +-commutativeN/A

        \[\leadsto \color{blue}{2 \cdot \frac{z}{t} + x} \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{\frac{z}{t} \cdot 2} + x \]
      5. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{z}{t}, 2, x\right)} \]
      6. lower-/.f6488.5

        \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{z}{t}}, 2, x\right) \]
    5. Applied rewrites88.5%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{z}{t}, 2, x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.1%

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

Alternative 3: 62.2% accurate, 2.9× speedup?

\[\begin{array}{l} \\ x - \frac{y}{z} \end{array} \]
(FPCore (x y z t) :precision binary64 (- x (/ y z)))
double code(double x, double y, double z, double t) {
	return x - (y / z);
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = x - (y / z)
end function
public static double code(double x, double y, double z, double t) {
	return x - (y / z);
}
def code(x, y, z, t):
	return x - (y / z)
function code(x, y, z, t)
	return Float64(x - Float64(y / z))
end
function tmp = code(x, y, z, t)
	tmp = x - (y / z);
end
code[x_, y_, z_, t_] := N[(x - N[(y / z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{y}{z}
\end{array}
Derivation
  1. Initial program 81.9%

    \[x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \]
  2. Add Preprocessing
  3. Taylor expanded in y around 0

    \[\leadsto x - \color{blue}{\frac{y}{z}} \]
  4. Step-by-step derivation
    1. lower-/.f6462.0

      \[\leadsto x - \color{blue}{\frac{y}{z}} \]
  5. Applied rewrites62.0%

    \[\leadsto x - \color{blue}{\frac{y}{z}} \]
  6. Add Preprocessing

Developer Target 1: 99.9% accurate, 0.8× speedup?

\[\begin{array}{l} \\ x - \frac{1}{\frac{z}{y} - \frac{\frac{t}{2}}{z}} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (- x (/ 1.0 (- (/ z y) (/ (/ t 2.0) z)))))
double code(double x, double y, double z, double t) {
	return x - (1.0 / ((z / y) - ((t / 2.0) / z)));
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = x - (1.0d0 / ((z / y) - ((t / 2.0d0) / z)))
end function
public static double code(double x, double y, double z, double t) {
	return x - (1.0 / ((z / y) - ((t / 2.0) / z)));
}
def code(x, y, z, t):
	return x - (1.0 / ((z / y) - ((t / 2.0) / z)))
function code(x, y, z, t)
	return Float64(x - Float64(1.0 / Float64(Float64(z / y) - Float64(Float64(t / 2.0) / z))))
end
function tmp = code(x, y, z, t)
	tmp = x - (1.0 / ((z / y) - ((t / 2.0) / z)));
end
code[x_, y_, z_, t_] := N[(x - N[(1.0 / N[(N[(z / y), $MachinePrecision] - N[(N[(t / 2.0), $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{1}{\frac{z}{y} - \frac{\frac{t}{2}}{z}}
\end{array}

Reproduce

?
herbie shell --seed 2024326 
(FPCore (x y z t)
  :name "Numeric.AD.Rank1.Halley:findZero from ad-4.2.4"
  :precision binary64

  :alt
  (! :herbie-platform default (- x (/ 1 (- (/ z y) (/ (/ t 2) z)))))

  (- x (/ (* (* y 2.0) z) (- (* (* z 2.0) z) (* y t)))))