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

Percentage Accurate: 81.5% → 94.6%
Time: 2.6s
Alternatives: 4
Speedup: 1.4×

Specification

?
\[x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (- 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)
use fmin_fmax_functions
    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]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	x - (((y * (2)) * z) / (((z * (2)) * z) - (y * t)))
END code
x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t}

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 4 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: 81.5% accurate, 1.0× speedup?

\[x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (- 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)
use fmin_fmax_functions
    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]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	x - (((y * (2)) * z) / (((z * (2)) * z) - (y * t)))
END code
x - \frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t}

Alternative 1: 94.6% accurate, 0.5× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \leq 5 \cdot 10^{+224}:\\ \;\;\;\;\mathsf{fma}\left(z, \frac{y + y}{\mathsf{fma}\left(-2, z \cdot z, t \cdot y\right)}, x\right)\\ \mathbf{else}:\\ \;\;\;\;x - \frac{y}{z}\\ \end{array} \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (if (<= (/ (* (* y 2.0) z) (- (* (* z 2.0) z) (* y t))) 5e+224)
  (fma z (/ (+ y 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 ((((y * 2.0) * z) / (((z * 2.0) * z) - (y * t))) <= 5e+224) {
		tmp = fma(z, ((y + 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(Float64(Float64(y * 2.0) * z) / Float64(Float64(Float64(z * 2.0) * z) - Float64(y * t))) <= 5e+224)
		tmp = fma(z, Float64(Float64(y + 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[(N[(N[(y * 2.0), $MachinePrecision] * z), $MachinePrecision] / N[(N[(N[(z * 2.0), $MachinePrecision] * z), $MachinePrecision] - N[(y * t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e+224], N[(z * N[(N[(y + y), $MachinePrecision] / N[(-2.0 * N[(z * z), $MachinePrecision] + N[(t * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision], N[(x - N[(y / z), $MachinePrecision]), $MachinePrecision]]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	LET tmp = IF ((((y * (2)) * z) / (((z * (2)) * z) - (y * t))) <= (499999999999999964227111724318263497804707306223243456268197521522525585749208789151208295153553467188676004711794318067127242311470730588919109020314930679307514026089293096804165265079253323065443524458327730161833343975424)) THEN ((z * ((y + y) / (((-2) * (z * z)) + (t * y)))) + x) ELSE (x - (y / z)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{\left(y \cdot 2\right) \cdot z}{\left(z \cdot 2\right) \cdot z - y \cdot t} \leq 5 \cdot 10^{+224}:\\
\;\;\;\;\mathsf{fma}\left(z, \frac{y + y}{\mathsf{fma}\left(-2, z \cdot z, t \cdot y\right)}, x\right)\\

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


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

    1. Initial program 81.5%

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

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

    if 4.9999999999999996e224 < (/.f64 (*.f64 (*.f64 y #s(literal 2 binary64)) z) (-.f64 (*.f64 (*.f64 z #s(literal 2 binary64)) z) (*.f64 y t)))

    1. Initial program 81.5%

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

      \[\leadsto x - \frac{y}{z} \]
    3. Step-by-step derivation
      1. Applied rewrites63.2%

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

    Alternative 2: 89.1% accurate, 1.4× speedup?

    \[\begin{array}{l} t_1 := x - \frac{y}{z}\\ \mathbf{if}\;z \leq -4.7517252514640575 \cdot 10^{-51}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;z \leq 2.530602361761182 \cdot 10^{-18}:\\ \;\;\;\;x - -2 \cdot \frac{z}{t}\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \]
    (FPCore (x y z t)
      :precision binary64
      :pre TRUE
      (let* ((t_1 (- x (/ y z))))
      (if (<= z -4.7517252514640575e-51)
        t_1
        (if (<= z 2.530602361761182e-18) (- x (* -2.0 (/ z t))) t_1))))
    double code(double x, double y, double z, double t) {
    	double t_1 = x - (y / z);
    	double tmp;
    	if (z <= -4.7517252514640575e-51) {
    		tmp = t_1;
    	} else if (z <= 2.530602361761182e-18) {
    		tmp = x - (-2.0 * (z / t));
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    real(8) function code(x, y, z, t)
    use fmin_fmax_functions
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8), intent (in) :: z
        real(8), intent (in) :: t
        real(8) :: t_1
        real(8) :: tmp
        t_1 = x - (y / z)
        if (z <= (-4.7517252514640575d-51)) then
            tmp = t_1
        else if (z <= 2.530602361761182d-18) then
            tmp = x - ((-2.0d0) * (z / t))
        else
            tmp = t_1
        end if
        code = tmp
    end function
    
    public static double code(double x, double y, double z, double t) {
    	double t_1 = x - (y / z);
    	double tmp;
    	if (z <= -4.7517252514640575e-51) {
    		tmp = t_1;
    	} else if (z <= 2.530602361761182e-18) {
    		tmp = x - (-2.0 * (z / t));
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    def code(x, y, z, t):
    	t_1 = x - (y / z)
    	tmp = 0
    	if z <= -4.7517252514640575e-51:
    		tmp = t_1
    	elif z <= 2.530602361761182e-18:
    		tmp = x - (-2.0 * (z / t))
    	else:
    		tmp = t_1
    	return tmp
    
    function code(x, y, z, t)
    	t_1 = Float64(x - Float64(y / z))
    	tmp = 0.0
    	if (z <= -4.7517252514640575e-51)
    		tmp = t_1;
    	elseif (z <= 2.530602361761182e-18)
    		tmp = Float64(x - Float64(-2.0 * Float64(z / t)));
    	else
    		tmp = t_1;
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y, z, t)
    	t_1 = x - (y / z);
    	tmp = 0.0;
    	if (z <= -4.7517252514640575e-51)
    		tmp = t_1;
    	elseif (z <= 2.530602361761182e-18)
    		tmp = x - (-2.0 * (z / t));
    	else
    		tmp = t_1;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_, z_, t_] := Block[{t$95$1 = N[(x - N[(y / z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -4.7517252514640575e-51], t$95$1, If[LessEqual[z, 2.530602361761182e-18], N[(x - N[(-2.0 * N[(z / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$1]]]
    
    f(x, y, z, t):
    	x in [-inf, +inf],
    	y in [-inf, +inf],
    	z in [-inf, +inf],
    	t in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x, y, z, t: real): real =
    	LET t_1 = (x - (y / z)) IN
    		LET tmp_1 = IF (z <= (25306023617611818642487936766263350008500203008692495953990686530232778750360012054443359375e-109)) THEN (x - ((-2) * (z / t))) ELSE t_1 ENDIF IN
    		LET tmp = IF (z <= (-47517252514640574709340682892707614207666252737264753948897617690316581059762296295005391603421749531157242891306368187248135846224850187269339585327543318271636962890625e-220)) THEN t_1 ELSE tmp_1 ENDIF IN
    	tmp
    END code
    \begin{array}{l}
    t_1 := x - \frac{y}{z}\\
    \mathbf{if}\;z \leq -4.7517252514640575 \cdot 10^{-51}:\\
    \;\;\;\;t\_1\\
    
    \mathbf{elif}\;z \leq 2.530602361761182 \cdot 10^{-18}:\\
    \;\;\;\;x - -2 \cdot \frac{z}{t}\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_1\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if z < -4.7517252514640575e-51 or 2.5306023617611819e-18 < z

      1. Initial program 81.5%

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

        \[\leadsto x - \frac{y}{z} \]
      3. Step-by-step derivation
        1. Applied rewrites63.2%

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

        if -4.7517252514640575e-51 < z < 2.5306023617611819e-18

        1. Initial program 81.5%

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

          \[\leadsto x - -2 \cdot \frac{z}{t} \]
        3. Step-by-step derivation
          1. Applied rewrites62.8%

            \[\leadsto x - -2 \cdot \frac{z}{t} \]
        4. Recombined 2 regimes into one program.
        5. Add Preprocessing

        Alternative 3: 89.0% accurate, 1.4× speedup?

        \[\begin{array}{l} t_1 := x - \frac{y}{z}\\ \mathbf{if}\;z \leq -4.7517252514640575 \cdot 10^{-51}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;z \leq 2.530602361761182 \cdot 10^{-18}:\\ \;\;\;\;\mathsf{fma}\left(z, \frac{2}{t}, x\right)\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \]
        (FPCore (x y z t)
          :precision binary64
          :pre TRUE
          (let* ((t_1 (- x (/ y z))))
          (if (<= z -4.7517252514640575e-51)
            t_1
            (if (<= z 2.530602361761182e-18) (fma z (/ 2.0 t) x) t_1))))
        double code(double x, double y, double z, double t) {
        	double t_1 = x - (y / z);
        	double tmp;
        	if (z <= -4.7517252514640575e-51) {
        		tmp = t_1;
        	} else if (z <= 2.530602361761182e-18) {
        		tmp = fma(z, (2.0 / t), x);
        	} else {
        		tmp = t_1;
        	}
        	return tmp;
        }
        
        function code(x, y, z, t)
        	t_1 = Float64(x - Float64(y / z))
        	tmp = 0.0
        	if (z <= -4.7517252514640575e-51)
        		tmp = t_1;
        	elseif (z <= 2.530602361761182e-18)
        		tmp = fma(z, Float64(2.0 / t), x);
        	else
        		tmp = t_1;
        	end
        	return tmp
        end
        
        code[x_, y_, z_, t_] := Block[{t$95$1 = N[(x - N[(y / z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -4.7517252514640575e-51], t$95$1, If[LessEqual[z, 2.530602361761182e-18], N[(z * N[(2.0 / t), $MachinePrecision] + x), $MachinePrecision], t$95$1]]]
        
        f(x, y, z, t):
        	x in [-inf, +inf],
        	y in [-inf, +inf],
        	z in [-inf, +inf],
        	t in [-inf, +inf]
        code: THEORY
        BEGIN
        f(x, y, z, t: real): real =
        	LET t_1 = (x - (y / z)) IN
        		LET tmp_1 = IF (z <= (25306023617611818642487936766263350008500203008692495953990686530232778750360012054443359375e-109)) THEN ((z * ((2) / t)) + x) ELSE t_1 ENDIF IN
        		LET tmp = IF (z <= (-47517252514640574709340682892707614207666252737264753948897617690316581059762296295005391603421749531157242891306368187248135846224850187269339585327543318271636962890625e-220)) THEN t_1 ELSE tmp_1 ENDIF IN
        	tmp
        END code
        \begin{array}{l}
        t_1 := x - \frac{y}{z}\\
        \mathbf{if}\;z \leq -4.7517252514640575 \cdot 10^{-51}:\\
        \;\;\;\;t\_1\\
        
        \mathbf{elif}\;z \leq 2.530602361761182 \cdot 10^{-18}:\\
        \;\;\;\;\mathsf{fma}\left(z, \frac{2}{t}, x\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;t\_1\\
        
        
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if z < -4.7517252514640575e-51 or 2.5306023617611819e-18 < z

          1. Initial program 81.5%

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

            \[\leadsto x - \frac{y}{z} \]
          3. Step-by-step derivation
            1. Applied rewrites63.2%

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

            if -4.7517252514640575e-51 < z < 2.5306023617611819e-18

            1. Initial program 81.5%

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

              \[\leadsto \mathsf{fma}\left(z, \frac{y + y}{\mathsf{fma}\left(-2, z \cdot z, t \cdot y\right)}, x\right) \]
            3. Taylor expanded in y around inf

              \[\leadsto \mathsf{fma}\left(z, \frac{2}{t}, x\right) \]
            4. Step-by-step derivation
              1. Applied rewrites62.8%

                \[\leadsto \mathsf{fma}\left(z, \frac{2}{t}, x\right) \]
            5. Recombined 2 regimes into one program.
            6. Add Preprocessing

            Alternative 4: 63.2% accurate, 3.4× speedup?

            \[x - \frac{y}{z} \]
            (FPCore (x y z t)
              :precision binary64
              :pre TRUE
              (- 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)
            use fmin_fmax_functions
                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]
            
            f(x, y, z, t):
            	x in [-inf, +inf],
            	y in [-inf, +inf],
            	z in [-inf, +inf],
            	t in [-inf, +inf]
            code: THEORY
            BEGIN
            f(x, y, z, t: real): real =
            	x - (y / z)
            END code
            x - \frac{y}{z}
            
            Derivation
            1. Initial program 81.5%

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

              \[\leadsto x - \frac{y}{z} \]
            3. Step-by-step derivation
              1. Applied rewrites63.2%

                \[\leadsto x - \frac{y}{z} \]
              2. Add Preprocessing

              Reproduce

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