Numeric.SpecFunctions:invIncompleteBetaWorker from math-functions-0.1.5.2, G

Percentage Accurate: 84.9% → 99.7%
Time: 10.7s
Alternatives: 6
Speedup: 15.6×

Specification

?
\[\begin{array}{l} \\ x + \frac{e^{y \cdot \log \left(\frac{y}{z + y}\right)}}{y} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (+ x (/ (exp (* y (log (/ y (+ z y))))) y)))
double code(double x, double y, double z) {
	return x + (exp((y * log((y / (z + y))))) / y);
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + (exp((y * log((y / (z + y))))) / y)
end function
public static double code(double x, double y, double z) {
	return x + (Math.exp((y * Math.log((y / (z + y))))) / y);
}
def code(x, y, z):
	return x + (math.exp((y * math.log((y / (z + y))))) / y)
function code(x, y, z)
	return Float64(x + Float64(exp(Float64(y * log(Float64(y / Float64(z + y))))) / y))
end
function tmp = code(x, y, z)
	tmp = x + (exp((y * log((y / (z + y))))) / y);
end
code[x_, y_, z_] := N[(x + N[(N[Exp[N[(y * N[Log[N[(y / N[(z + y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + \frac{e^{y \cdot \log \left(\frac{y}{z + y}\right)}}{y}
\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 6 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: 84.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x + \frac{e^{y \cdot \log \left(\frac{y}{z + y}\right)}}{y} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (+ x (/ (exp (* y (log (/ y (+ z y))))) y)))
double code(double x, double y, double z) {
	return x + (exp((y * log((y / (z + y))))) / y);
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + (exp((y * log((y / (z + y))))) / y)
end function
public static double code(double x, double y, double z) {
	return x + (Math.exp((y * Math.log((y / (z + y))))) / y);
}
def code(x, y, z):
	return x + (math.exp((y * math.log((y / (z + y))))) / y)
function code(x, y, z)
	return Float64(x + Float64(exp(Float64(y * log(Float64(y / Float64(z + y))))) / y))
end
function tmp = code(x, y, z)
	tmp = x + (exp((y * log((y / (z + y))))) / y);
end
code[x_, y_, z_] := N[(x + N[(N[Exp[N[(y * N[Log[N[(y / N[(z + y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + \frac{e^{y \cdot \log \left(\frac{y}{z + y}\right)}}{y}
\end{array}

Alternative 1: 99.7% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{e^{-z}}{y} + x\\ \mathbf{if}\;y \leq -0.88:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 0.0005:\\ \;\;\;\;\frac{1}{y} + x\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (+ (/ (exp (- z)) y) x)))
   (if (<= y -0.88) t_0 (if (<= y 0.0005) (+ (/ 1.0 y) x) t_0))))
double code(double x, double y, double z) {
	double t_0 = (exp(-z) / y) + x;
	double tmp;
	if (y <= -0.88) {
		tmp = t_0;
	} else if (y <= 0.0005) {
		tmp = (1.0 / y) + x;
	} 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 = (exp(-z) / y) + x
    if (y <= (-0.88d0)) then
        tmp = t_0
    else if (y <= 0.0005d0) then
        tmp = (1.0d0 / y) + x
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = (Math.exp(-z) / y) + x;
	double tmp;
	if (y <= -0.88) {
		tmp = t_0;
	} else if (y <= 0.0005) {
		tmp = (1.0 / y) + x;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = (math.exp(-z) / y) + x
	tmp = 0
	if y <= -0.88:
		tmp = t_0
	elif y <= 0.0005:
		tmp = (1.0 / y) + x
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = Float64(Float64(exp(Float64(-z)) / y) + x)
	tmp = 0.0
	if (y <= -0.88)
		tmp = t_0;
	elseif (y <= 0.0005)
		tmp = Float64(Float64(1.0 / y) + x);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = (exp(-z) / y) + x;
	tmp = 0.0;
	if (y <= -0.88)
		tmp = t_0;
	elseif (y <= 0.0005)
		tmp = (1.0 / y) + x;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(N[Exp[(-z)], $MachinePrecision] / y), $MachinePrecision] + x), $MachinePrecision]}, If[LessEqual[y, -0.88], t$95$0, If[LessEqual[y, 0.0005], N[(N[(1.0 / y), $MachinePrecision] + x), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{e^{-z}}{y} + x\\
\mathbf{if}\;y \leq -0.88:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 0.0005:\\
\;\;\;\;\frac{1}{y} + x\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -0.880000000000000004 or 5.0000000000000001e-4 < y

    1. Initial program 87.9%

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

      \[\leadsto x + \frac{e^{\color{blue}{-1 \cdot z}}}{y} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto x + \frac{e^{\color{blue}{\mathsf{neg}\left(z\right)}}}{y} \]
      2. lower-neg.f64100.0

        \[\leadsto x + \frac{e^{\color{blue}{-z}}}{y} \]
    5. Applied rewrites100.0%

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

    if -0.880000000000000004 < y < 5.0000000000000001e-4

    1. Initial program 83.3%

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

      \[\leadsto x + \frac{\color{blue}{1}}{y} \]
    4. Step-by-step derivation
      1. Applied rewrites97.7%

        \[\leadsto x + \frac{\color{blue}{1}}{y} \]
    5. Recombined 2 regimes into one program.
    6. Final simplification98.9%

      \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -0.88:\\ \;\;\;\;\frac{e^{-z}}{y} + x\\ \mathbf{elif}\;y \leq 0.0005:\\ \;\;\;\;\frac{1}{y} + x\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{-z}}{y} + x\\ \end{array} \]
    7. Add Preprocessing

    Alternative 2: 88.5% accurate, 6.0× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -0.78:\\ \;\;\;\;\frac{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666, z, 0.5\right), z, -1\right), z, 1\right)}{y} + x\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{y} + x\\ \end{array} \end{array} \]
    (FPCore (x y z)
     :precision binary64
     (if (<= y -0.78)
       (+ (/ (fma (fma (fma -0.16666666666666666 z 0.5) z -1.0) z 1.0) y) x)
       (+ (/ 1.0 y) x)))
    double code(double x, double y, double z) {
    	double tmp;
    	if (y <= -0.78) {
    		tmp = (fma(fma(fma(-0.16666666666666666, z, 0.5), z, -1.0), z, 1.0) / y) + x;
    	} else {
    		tmp = (1.0 / y) + x;
    	}
    	return tmp;
    }
    
    function code(x, y, z)
    	tmp = 0.0
    	if (y <= -0.78)
    		tmp = Float64(Float64(fma(fma(fma(-0.16666666666666666, z, 0.5), z, -1.0), z, 1.0) / y) + x);
    	else
    		tmp = Float64(Float64(1.0 / y) + x);
    	end
    	return tmp
    end
    
    code[x_, y_, z_] := If[LessEqual[y, -0.78], N[(N[(N[(N[(N[(-0.16666666666666666 * z + 0.5), $MachinePrecision] * z + -1.0), $MachinePrecision] * z + 1.0), $MachinePrecision] / y), $MachinePrecision] + x), $MachinePrecision], N[(N[(1.0 / y), $MachinePrecision] + x), $MachinePrecision]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;y \leq -0.78:\\
    \;\;\;\;\frac{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666, z, 0.5\right), z, -1\right), z, 1\right)}{y} + x\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{1}{y} + x\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if y < -0.78000000000000003

      1. Initial program 84.5%

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

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

          \[\leadsto x + \frac{\color{blue}{z \cdot \left(z \cdot \left(\frac{1}{2} + \left(-1 \cdot \left(z \cdot \left(\frac{1}{6} + \left(\frac{1}{3} \cdot \frac{1}{{y}^{2}} + \frac{1}{2} \cdot \frac{1}{y}\right)\right)\right) + \frac{1}{2} \cdot \frac{1}{y}\right)\right) - 1\right) + 1}}{y} \]
        2. *-commutativeN/A

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

          \[\leadsto x + \frac{\color{blue}{\mathsf{fma}\left(z \cdot \left(\frac{1}{2} + \left(-1 \cdot \left(z \cdot \left(\frac{1}{6} + \left(\frac{1}{3} \cdot \frac{1}{{y}^{2}} + \frac{1}{2} \cdot \frac{1}{y}\right)\right)\right) + \frac{1}{2} \cdot \frac{1}{y}\right)\right) - 1, z, 1\right)}}{y} \]
      5. Applied rewrites81.1%

        \[\leadsto x + \frac{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-\left(\left(0.16666666666666666 + \frac{0.5}{y}\right) + \frac{0.3333333333333333}{y \cdot y}\right), z, \frac{0.5}{y} + 0.5\right), z, -1\right), z, 1\right)}}{y} \]
      6. Taylor expanded in y around inf

        \[\leadsto x + \frac{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2} + \frac{-1}{6} \cdot z, z, -1\right), z, 1\right)}{y} \]
      7. Step-by-step derivation
        1. Applied rewrites81.1%

          \[\leadsto x + \frac{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666, z, 0.5\right), z, -1\right), z, 1\right)}{y} \]

        if -0.78000000000000003 < y

        1. Initial program 85.1%

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

          \[\leadsto x + \frac{\color{blue}{1}}{y} \]
        4. Step-by-step derivation
          1. Applied rewrites91.4%

            \[\leadsto x + \frac{\color{blue}{1}}{y} \]
        5. Recombined 2 regimes into one program.
        6. Final simplification88.5%

          \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -0.78:\\ \;\;\;\;\frac{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666, z, 0.5\right), z, -1\right), z, 1\right)}{y} + x\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{y} + x\\ \end{array} \]
        7. Add Preprocessing

        Developer Target 1: 91.5% accurate, 0.7× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\frac{y}{z + y} < 7.11541576 \cdot 10^{-315}:\\ \;\;\;\;x + \frac{e^{\frac{-1}{z}}}{y}\\ \mathbf{else}:\\ \;\;\;\;x + \frac{e^{\log \left({\left(\frac{y}{y + z}\right)}^{y}\right)}}{y}\\ \end{array} \end{array} \]
        (FPCore (x y z)
         :precision binary64
         (if (< (/ y (+ z y)) 7.11541576e-315)
           (+ x (/ (exp (/ -1.0 z)) y))
           (+ x (/ (exp (log (pow (/ y (+ y z)) y))) y))))
        double code(double x, double y, double z) {
        	double tmp;
        	if ((y / (z + y)) < 7.11541576e-315) {
        		tmp = x + (exp((-1.0 / z)) / y);
        	} else {
        		tmp = x + (exp(log(pow((y / (y + z)), y))) / 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
            real(8) :: tmp
            if ((y / (z + y)) < 7.11541576d-315) then
                tmp = x + (exp(((-1.0d0) / z)) / y)
            else
                tmp = x + (exp(log(((y / (y + z)) ** y))) / y)
            end if
            code = tmp
        end function
        
        public static double code(double x, double y, double z) {
        	double tmp;
        	if ((y / (z + y)) < 7.11541576e-315) {
        		tmp = x + (Math.exp((-1.0 / z)) / y);
        	} else {
        		tmp = x + (Math.exp(Math.log(Math.pow((y / (y + z)), y))) / y);
        	}
        	return tmp;
        }
        
        def code(x, y, z):
        	tmp = 0
        	if (y / (z + y)) < 7.11541576e-315:
        		tmp = x + (math.exp((-1.0 / z)) / y)
        	else:
        		tmp = x + (math.exp(math.log(math.pow((y / (y + z)), y))) / y)
        	return tmp
        
        function code(x, y, z)
        	tmp = 0.0
        	if (Float64(y / Float64(z + y)) < 7.11541576e-315)
        		tmp = Float64(x + Float64(exp(Float64(-1.0 / z)) / y));
        	else
        		tmp = Float64(x + Float64(exp(log((Float64(y / Float64(y + z)) ^ y))) / y));
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, y, z)
        	tmp = 0.0;
        	if ((y / (z + y)) < 7.11541576e-315)
        		tmp = x + (exp((-1.0 / z)) / y);
        	else
        		tmp = x + (exp(log(((y / (y + z)) ^ y))) / y);
        	end
        	tmp_2 = tmp;
        end
        
        code[x_, y_, z_] := If[Less[N[(y / N[(z + y), $MachinePrecision]), $MachinePrecision], 7.11541576e-315], N[(x + N[(N[Exp[N[(-1.0 / z), $MachinePrecision]], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision], N[(x + N[(N[Exp[N[Log[N[Power[N[(y / N[(y + z), $MachinePrecision]), $MachinePrecision], y], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;\frac{y}{z + y} < 7.11541576 \cdot 10^{-315}:\\
        \;\;\;\;x + \frac{e^{\frac{-1}{z}}}{y}\\
        
        \mathbf{else}:\\
        \;\;\;\;x + \frac{e^{\log \left({\left(\frac{y}{y + z}\right)}^{y}\right)}}{y}\\
        
        
        \end{array}
        \end{array}
        

        Reproduce

        ?
        herbie shell --seed 2024230 
        (FPCore (x y z)
          :name "Numeric.SpecFunctions:invIncompleteBetaWorker from math-functions-0.1.5.2, G"
          :precision binary64
        
          :alt
          (! :herbie-platform default (if (< (/ y (+ z y)) 17788539399477/2500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (+ x (/ (exp (/ -1 z)) y)) (+ x (/ (exp (log (pow (/ y (+ y z)) y))) y))))
        
          (+ x (/ (exp (* y (log (/ y (+ z y))))) y)))