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

Percentage Accurate: 100.0% → 100.0%
Time: 3.4s
Alternatives: 6
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x} \end{array} \]
(FPCore (x)
 :precision binary64
 (- x (/ (+ 2.30753 (* x 0.27061)) (+ 1.0 (* (+ 0.99229 (* x 0.04481)) x)))))
double code(double x) {
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + ((0.99229 + (x * 0.04481)) * x)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x - ((2.30753d0 + (x * 0.27061d0)) / (1.0d0 + ((0.99229d0 + (x * 0.04481d0)) * x)))
end function
public static double code(double x) {
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + ((0.99229 + (x * 0.04481)) * x)));
}
def code(x):
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + ((0.99229 + (x * 0.04481)) * x)))
function code(x)
	return Float64(x - Float64(Float64(2.30753 + Float64(x * 0.27061)) / Float64(1.0 + Float64(Float64(0.99229 + Float64(x * 0.04481)) * x))))
end
function tmp = code(x)
	tmp = x - ((2.30753 + (x * 0.27061)) / (1.0 + ((0.99229 + (x * 0.04481)) * x)));
end
code[x_] := N[(x - N[(N[(2.30753 + N[(x * 0.27061), $MachinePrecision]), $MachinePrecision] / N[(1.0 + N[(N[(0.99229 + N[(x * 0.04481), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x}
\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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x} \end{array} \]
(FPCore (x)
 :precision binary64
 (- x (/ (+ 2.30753 (* x 0.27061)) (+ 1.0 (* (+ 0.99229 (* x 0.04481)) x)))))
double code(double x) {
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + ((0.99229 + (x * 0.04481)) * x)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x - ((2.30753d0 + (x * 0.27061d0)) / (1.0d0 + ((0.99229d0 + (x * 0.04481d0)) * x)))
end function
public static double code(double x) {
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + ((0.99229 + (x * 0.04481)) * x)));
}
def code(x):
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + ((0.99229 + (x * 0.04481)) * x)))
function code(x)
	return Float64(x - Float64(Float64(2.30753 + Float64(x * 0.27061)) / Float64(1.0 + Float64(Float64(0.99229 + Float64(x * 0.04481)) * x))))
end
function tmp = code(x)
	tmp = x - ((2.30753 + (x * 0.27061)) / (1.0 + ((0.99229 + (x * 0.04481)) * x)));
end
code[x_] := N[(x - N[(N[(2.30753 + N[(x * 0.27061), $MachinePrecision]), $MachinePrecision] / N[(1.0 + N[(N[(0.99229 + N[(x * 0.04481), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x}
\end{array}

Alternative 1: 100.0% accurate, 0.0× speedup?

\[\begin{array}{l} \\ x - {\left(\frac{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.04481, 0.99229\right), 1\right)}{\mathsf{fma}\left(x, 0.27061, 2.30753\right)}\right)}^{-1} \end{array} \]
(FPCore (x)
 :precision binary64
 (-
  x
  (pow (/ (fma x (fma x 0.04481 0.99229) 1.0) (fma x 0.27061 2.30753)) -1.0)))
double code(double x) {
	return x - pow((fma(x, fma(x, 0.04481, 0.99229), 1.0) / fma(x, 0.27061, 2.30753)), -1.0);
}
function code(x)
	return Float64(x - (Float64(fma(x, fma(x, 0.04481, 0.99229), 1.0) / fma(x, 0.27061, 2.30753)) ^ -1.0))
end
code[x_] := N[(x - N[Power[N[(N[(x * N[(x * 0.04481 + 0.99229), $MachinePrecision] + 1.0), $MachinePrecision] / N[(x * 0.27061 + 2.30753), $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - {\left(\frac{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.04481, 0.99229\right), 1\right)}{\mathsf{fma}\left(x, 0.27061, 2.30753\right)}\right)}^{-1}
\end{array}
Derivation
  1. Initial program 100.0%

    \[x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x} \]
  2. Step-by-step derivation
    1. clear-num100.0%

      \[\leadsto x - \color{blue}{\frac{1}{\frac{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x}{2.30753 + x \cdot 0.27061}}} \]
    2. inv-pow100.0%

      \[\leadsto x - \color{blue}{{\left(\frac{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x}{2.30753 + x \cdot 0.27061}\right)}^{-1}} \]
    3. +-commutative100.0%

      \[\leadsto x - {\left(\frac{\color{blue}{\left(0.99229 + x \cdot 0.04481\right) \cdot x + 1}}{2.30753 + x \cdot 0.27061}\right)}^{-1} \]
    4. *-commutative100.0%

      \[\leadsto x - {\left(\frac{\color{blue}{x \cdot \left(0.99229 + x \cdot 0.04481\right)} + 1}{2.30753 + x \cdot 0.27061}\right)}^{-1} \]
    5. fma-def100.0%

      \[\leadsto x - {\left(\frac{\color{blue}{\mathsf{fma}\left(x, 0.99229 + x \cdot 0.04481, 1\right)}}{2.30753 + x \cdot 0.27061}\right)}^{-1} \]
    6. +-commutative100.0%

      \[\leadsto x - {\left(\frac{\mathsf{fma}\left(x, \color{blue}{x \cdot 0.04481 + 0.99229}, 1\right)}{2.30753 + x \cdot 0.27061}\right)}^{-1} \]
    7. fma-def100.0%

      \[\leadsto x - {\left(\frac{\mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 0.04481, 0.99229\right)}, 1\right)}{2.30753 + x \cdot 0.27061}\right)}^{-1} \]
    8. +-commutative100.0%

      \[\leadsto x - {\left(\frac{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.04481, 0.99229\right), 1\right)}{\color{blue}{x \cdot 0.27061 + 2.30753}}\right)}^{-1} \]
    9. fma-def100.0%

      \[\leadsto x - {\left(\frac{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.04481, 0.99229\right), 1\right)}{\color{blue}{\mathsf{fma}\left(x, 0.27061, 2.30753\right)}}\right)}^{-1} \]
  3. Applied egg-rr100.0%

    \[\leadsto x - \color{blue}{{\left(\frac{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.04481, 0.99229\right), 1\right)}{\mathsf{fma}\left(x, 0.27061, 2.30753\right)}\right)}^{-1}} \]
  4. Final simplification100.0%

    \[\leadsto x - {\left(\frac{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.04481, 0.99229\right), 1\right)}{\mathsf{fma}\left(x, 0.27061, 2.30753\right)}\right)}^{-1} \]

Alternative 2: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x - \frac{2.30753 + x \cdot 0.27061}{1 + x \cdot \left(0.99229 + x \cdot 0.04481\right)} \end{array} \]
(FPCore (x)
 :precision binary64
 (- x (/ (+ 2.30753 (* x 0.27061)) (+ 1.0 (* x (+ 0.99229 (* x 0.04481)))))))
double code(double x) {
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + (x * (0.99229 + (x * 0.04481)))));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x - ((2.30753d0 + (x * 0.27061d0)) / (1.0d0 + (x * (0.99229d0 + (x * 0.04481d0)))))
end function
public static double code(double x) {
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + (x * (0.99229 + (x * 0.04481)))));
}
def code(x):
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + (x * (0.99229 + (x * 0.04481)))))
function code(x)
	return Float64(x - Float64(Float64(2.30753 + Float64(x * 0.27061)) / Float64(1.0 + Float64(x * Float64(0.99229 + Float64(x * 0.04481))))))
end
function tmp = code(x)
	tmp = x - ((2.30753 + (x * 0.27061)) / (1.0 + (x * (0.99229 + (x * 0.04481)))));
end
code[x_] := N[(x - N[(N[(2.30753 + N[(x * 0.27061), $MachinePrecision]), $MachinePrecision] / N[(1.0 + N[(x * N[(0.99229 + N[(x * 0.04481), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{2.30753 + x \cdot 0.27061}{1 + x \cdot \left(0.99229 + x \cdot 0.04481\right)}
\end{array}
Derivation
  1. Initial program 100.0%

    \[x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x} \]
  2. Final simplification100.0%

    \[\leadsto x - \frac{2.30753 + x \cdot 0.27061}{1 + x \cdot \left(0.99229 + x \cdot 0.04481\right)} \]

Alternative 3: 98.6% accurate, 1.3× speedup?

\[\begin{array}{l} \\ x - \frac{2.30753 + x \cdot 0.27061}{1 + x \cdot 0.99229} \end{array} \]
(FPCore (x)
 :precision binary64
 (- x (/ (+ 2.30753 (* x 0.27061)) (+ 1.0 (* x 0.99229)))))
double code(double x) {
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + (x * 0.99229)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x - ((2.30753d0 + (x * 0.27061d0)) / (1.0d0 + (x * 0.99229d0)))
end function
public static double code(double x) {
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + (x * 0.99229)));
}
def code(x):
	return x - ((2.30753 + (x * 0.27061)) / (1.0 + (x * 0.99229)))
function code(x)
	return Float64(x - Float64(Float64(2.30753 + Float64(x * 0.27061)) / Float64(1.0 + Float64(x * 0.99229))))
end
function tmp = code(x)
	tmp = x - ((2.30753 + (x * 0.27061)) / (1.0 + (x * 0.99229)));
end
code[x_] := N[(x - N[(N[(2.30753 + N[(x * 0.27061), $MachinePrecision]), $MachinePrecision] / N[(1.0 + N[(x * 0.99229), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{2.30753 + x \cdot 0.27061}{1 + x \cdot 0.99229}
\end{array}
Derivation
  1. Initial program 100.0%

    \[x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x} \]
  2. Taylor expanded in x around 0 98.9%

    \[\leadsto x - \frac{2.30753 + x \cdot 0.27061}{1 + \color{blue}{0.99229 \cdot x}} \]
  3. Step-by-step derivation
    1. *-commutative98.9%

      \[\leadsto x - \frac{2.30753 + x \cdot 0.27061}{1 + \color{blue}{x \cdot 0.99229}} \]
  4. Simplified98.9%

    \[\leadsto x - \frac{2.30753 + x \cdot 0.27061}{1 + \color{blue}{x \cdot 0.99229}} \]
  5. Final simplification98.9%

    \[\leadsto x - \frac{2.30753 + x \cdot 0.27061}{1 + x \cdot 0.99229} \]

Alternative 4: 98.2% accurate, 3.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1.05:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq 1.2:\\ \;\;\;\;-2.30753\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x) :precision binary64 (if (<= x -1.05) x (if (<= x 1.2) -2.30753 x)))
double code(double x) {
	double tmp;
	if (x <= -1.05) {
		tmp = x;
	} else if (x <= 1.2) {
		tmp = -2.30753;
	} else {
		tmp = x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-1.05d0)) then
        tmp = x
    else if (x <= 1.2d0) then
        tmp = -2.30753d0
    else
        tmp = x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= -1.05) {
		tmp = x;
	} else if (x <= 1.2) {
		tmp = -2.30753;
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= -1.05:
		tmp = x
	elif x <= 1.2:
		tmp = -2.30753
	else:
		tmp = x
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -1.05)
		tmp = x;
	elseif (x <= 1.2)
		tmp = -2.30753;
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -1.05)
		tmp = x;
	elseif (x <= 1.2)
		tmp = -2.30753;
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, -1.05], x, If[LessEqual[x, 1.2], -2.30753, x]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.05:\\
\;\;\;\;x\\

\mathbf{elif}\;x \leq 1.2:\\
\;\;\;\;-2.30753\\

\mathbf{else}:\\
\;\;\;\;x\\


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

    1. Initial program 100.0%

      \[x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x} \]
    2. Taylor expanded in x around 0 98.8%

      \[\leadsto x - \color{blue}{2.30753} \]
    3. Taylor expanded in x around inf 99.3%

      \[\leadsto \color{blue}{x} \]

    if -1.05000000000000004 < x < 1.19999999999999996

    1. Initial program 100.0%

      \[x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x} \]
    2. Taylor expanded in x around 0 98.6%

      \[\leadsto x - \color{blue}{2.30753} \]
    3. Taylor expanded in x around 0 98.5%

      \[\leadsto \color{blue}{-2.30753} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.05:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq 1.2:\\ \;\;\;\;-2.30753\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]

Alternative 5: 97.7% accurate, 5.7× speedup?

\[\begin{array}{l} \\ x - 2.30753 \end{array} \]
(FPCore (x) :precision binary64 (- x 2.30753))
double code(double x) {
	return x - 2.30753;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x - 2.30753d0
end function
public static double code(double x) {
	return x - 2.30753;
}
def code(x):
	return x - 2.30753
function code(x)
	return Float64(x - 2.30753)
end
function tmp = code(x)
	tmp = x - 2.30753;
end
code[x_] := N[(x - 2.30753), $MachinePrecision]
\begin{array}{l}

\\
x - 2.30753
\end{array}
Derivation
  1. Initial program 100.0%

    \[x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x} \]
  2. Taylor expanded in x around 0 98.7%

    \[\leadsto x - \color{blue}{2.30753} \]
  3. Final simplification98.7%

    \[\leadsto x - 2.30753 \]

Alternative 6: 51.4% accurate, 17.0× speedup?

\[\begin{array}{l} \\ -2.30753 \end{array} \]
(FPCore (x) :precision binary64 -2.30753)
double code(double x) {
	return -2.30753;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = -2.30753d0
end function
public static double code(double x) {
	return -2.30753;
}
def code(x):
	return -2.30753
function code(x)
	return -2.30753
end
function tmp = code(x)
	tmp = -2.30753;
end
code[x_] := -2.30753
\begin{array}{l}

\\
-2.30753
\end{array}
Derivation
  1. Initial program 100.0%

    \[x - \frac{2.30753 + x \cdot 0.27061}{1 + \left(0.99229 + x \cdot 0.04481\right) \cdot x} \]
  2. Taylor expanded in x around 0 98.7%

    \[\leadsto x - \color{blue}{2.30753} \]
  3. Taylor expanded in x around 0 47.5%

    \[\leadsto \color{blue}{-2.30753} \]
  4. Final simplification47.5%

    \[\leadsto -2.30753 \]

Reproduce

?
herbie shell --seed 2023196 
(FPCore (x)
  :name "Numeric.SpecFunctions:invIncompleteBetaWorker from math-functions-0.1.5.2, D"
  :precision binary64
  (- x (/ (+ 2.30753 (* x 0.27061)) (+ 1.0 (* (+ 0.99229 (* x 0.04481)) x)))))