Numeric.SpecFunctions:invIncompleteGamma from math-functions-0.1.5.2, A

Percentage Accurate: 99.9% → 99.9%
Time: 6.7s
Alternatives: 6
Speedup: 1.0×

Specification

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

\\
1 - x \cdot \left(0.253 + x \cdot 0.12\right)
\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: 99.9% accurate, 1.0× speedup?

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

\\
1 - x \cdot \left(0.253 + x \cdot 0.12\right)
\end{array}

Alternative 1: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} \\ 1 - \mathsf{fma}\left(x \cdot 0.12, x, x \cdot 0.253\right) \end{array} \]
(FPCore (x) :precision binary64 (- 1.0 (fma (* x 0.12) x (* x 0.253))))
double code(double x) {
	return 1.0 - fma((x * 0.12), x, (x * 0.253));
}
function code(x)
	return Float64(1.0 - fma(Float64(x * 0.12), x, Float64(x * 0.253)))
end
code[x_] := N[(1.0 - N[(N[(x * 0.12), $MachinePrecision] * x + N[(x * 0.253), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
1 - \mathsf{fma}\left(x \cdot 0.12, x, x \cdot 0.253\right)
\end{array}
Derivation
  1. Initial program 99.9%

    \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. +-commutative99.9%

      \[\leadsto 1 - x \cdot \color{blue}{\left(x \cdot 0.12 + 0.253\right)} \]
    2. distribute-rgt-in99.9%

      \[\leadsto 1 - \color{blue}{\left(\left(x \cdot 0.12\right) \cdot x + 0.253 \cdot x\right)} \]
    3. *-commutative99.9%

      \[\leadsto 1 - \left(\left(x \cdot 0.12\right) \cdot x + \color{blue}{x \cdot 0.253}\right) \]
    4. fma-define99.9%

      \[\leadsto 1 - \color{blue}{\mathsf{fma}\left(x \cdot 0.12, x, x \cdot 0.253\right)} \]
  4. Applied egg-rr99.9%

    \[\leadsto 1 - \color{blue}{\mathsf{fma}\left(x \cdot 0.12, x, x \cdot 0.253\right)} \]
  5. Add Preprocessing

Alternative 2: 98.2% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -4.1 \lor \neg \left(x \leq 2\right):\\ \;\;\;\;-0.12 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;1 - x \cdot 0.253\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (or (<= x -4.1) (not (<= x 2.0))) (* -0.12 (* x x)) (- 1.0 (* x 0.253))))
double code(double x) {
	double tmp;
	if ((x <= -4.1) || !(x <= 2.0)) {
		tmp = -0.12 * (x * x);
	} else {
		tmp = 1.0 - (x * 0.253);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((x <= (-4.1d0)) .or. (.not. (x <= 2.0d0))) then
        tmp = (-0.12d0) * (x * x)
    else
        tmp = 1.0d0 - (x * 0.253d0)
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((x <= -4.1) || !(x <= 2.0)) {
		tmp = -0.12 * (x * x);
	} else {
		tmp = 1.0 - (x * 0.253);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (x <= -4.1) or not (x <= 2.0):
		tmp = -0.12 * (x * x)
	else:
		tmp = 1.0 - (x * 0.253)
	return tmp
function code(x)
	tmp = 0.0
	if ((x <= -4.1) || !(x <= 2.0))
		tmp = Float64(-0.12 * Float64(x * x));
	else
		tmp = Float64(1.0 - Float64(x * 0.253));
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((x <= -4.1) || ~((x <= 2.0)))
		tmp = -0.12 * (x * x);
	else
		tmp = 1.0 - (x * 0.253);
	end
	tmp_2 = tmp;
end
code[x_] := If[Or[LessEqual[x, -4.1], N[Not[LessEqual[x, 2.0]], $MachinePrecision]], N[(-0.12 * N[(x * x), $MachinePrecision]), $MachinePrecision], N[(1.0 - N[(x * 0.253), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.1 \lor \neg \left(x \leq 2\right):\\
\;\;\;\;-0.12 \cdot \left(x \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;1 - x \cdot 0.253\\


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

    1. Initial program 99.8%

      \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 98.3%

      \[\leadsto \color{blue}{-0.12 \cdot {x}^{2}} \]
    4. Step-by-step derivation
      1. *-commutative98.3%

        \[\leadsto \color{blue}{{x}^{2} \cdot -0.12} \]
    5. Simplified98.3%

      \[\leadsto \color{blue}{{x}^{2} \cdot -0.12} \]
    6. Step-by-step derivation
      1. unpow299.8%

        \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \left(-1 \cdot \frac{0.253 - \frac{1}{x}}{x} - 0.12\right) \]
    7. Applied egg-rr98.3%

      \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot -0.12 \]

    if -4.0999999999999996 < x < 2

    1. Initial program 100.0%

      \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 99.1%

      \[\leadsto 1 - \color{blue}{0.253 \cdot x} \]
    4. Step-by-step derivation
      1. *-commutative99.1%

        \[\leadsto 1 - \color{blue}{x \cdot 0.253} \]
    5. Simplified99.1%

      \[\leadsto 1 - \color{blue}{x \cdot 0.253} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4.1 \lor \neg \left(x \leq 2\right):\\ \;\;\;\;-0.12 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;1 - x \cdot 0.253\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 97.7% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -4.1 \lor \neg \left(x \leq 2\right):\\ \;\;\;\;-0.12 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (or (<= x -4.1) (not (<= x 2.0))) (* -0.12 (* x x)) 1.0))
double code(double x) {
	double tmp;
	if ((x <= -4.1) || !(x <= 2.0)) {
		tmp = -0.12 * (x * x);
	} else {
		tmp = 1.0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((x <= (-4.1d0)) .or. (.not. (x <= 2.0d0))) then
        tmp = (-0.12d0) * (x * x)
    else
        tmp = 1.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((x <= -4.1) || !(x <= 2.0)) {
		tmp = -0.12 * (x * x);
	} else {
		tmp = 1.0;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (x <= -4.1) or not (x <= 2.0):
		tmp = -0.12 * (x * x)
	else:
		tmp = 1.0
	return tmp
function code(x)
	tmp = 0.0
	if ((x <= -4.1) || !(x <= 2.0))
		tmp = Float64(-0.12 * Float64(x * x));
	else
		tmp = 1.0;
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((x <= -4.1) || ~((x <= 2.0)))
		tmp = -0.12 * (x * x);
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
code[x_] := If[Or[LessEqual[x, -4.1], N[Not[LessEqual[x, 2.0]], $MachinePrecision]], N[(-0.12 * N[(x * x), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.1 \lor \neg \left(x \leq 2\right):\\
\;\;\;\;-0.12 \cdot \left(x \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;1\\


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

    1. Initial program 99.8%

      \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 98.3%

      \[\leadsto \color{blue}{-0.12 \cdot {x}^{2}} \]
    4. Step-by-step derivation
      1. *-commutative98.3%

        \[\leadsto \color{blue}{{x}^{2} \cdot -0.12} \]
    5. Simplified98.3%

      \[\leadsto \color{blue}{{x}^{2} \cdot -0.12} \]
    6. Step-by-step derivation
      1. unpow299.8%

        \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \left(-1 \cdot \frac{0.253 - \frac{1}{x}}{x} - 0.12\right) \]
    7. Applied egg-rr98.3%

      \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot -0.12 \]

    if -4.0999999999999996 < x < 2

    1. Initial program 100.0%

      \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 98.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4.1 \lor \neg \left(x \leq 2\right):\\ \;\;\;\;-0.12 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 98.6% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -4.1:\\ \;\;\;\;x \cdot \left(x \cdot -0.12 - 0.253\right)\\ \mathbf{elif}\;x \leq 2:\\ \;\;\;\;1 - x \cdot 0.253\\ \mathbf{else}:\\ \;\;\;\;-0.12 \cdot \left(x \cdot x\right)\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -4.1)
   (* x (- (* x -0.12) 0.253))
   (if (<= x 2.0) (- 1.0 (* x 0.253)) (* -0.12 (* x x)))))
double code(double x) {
	double tmp;
	if (x <= -4.1) {
		tmp = x * ((x * -0.12) - 0.253);
	} else if (x <= 2.0) {
		tmp = 1.0 - (x * 0.253);
	} else {
		tmp = -0.12 * (x * x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-4.1d0)) then
        tmp = x * ((x * (-0.12d0)) - 0.253d0)
    else if (x <= 2.0d0) then
        tmp = 1.0d0 - (x * 0.253d0)
    else
        tmp = (-0.12d0) * (x * x)
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= -4.1) {
		tmp = x * ((x * -0.12) - 0.253);
	} else if (x <= 2.0) {
		tmp = 1.0 - (x * 0.253);
	} else {
		tmp = -0.12 * (x * x);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= -4.1:
		tmp = x * ((x * -0.12) - 0.253)
	elif x <= 2.0:
		tmp = 1.0 - (x * 0.253)
	else:
		tmp = -0.12 * (x * x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -4.1)
		tmp = Float64(x * Float64(Float64(x * -0.12) - 0.253));
	elseif (x <= 2.0)
		tmp = Float64(1.0 - Float64(x * 0.253));
	else
		tmp = Float64(-0.12 * Float64(x * x));
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -4.1)
		tmp = x * ((x * -0.12) - 0.253);
	elseif (x <= 2.0)
		tmp = 1.0 - (x * 0.253);
	else
		tmp = -0.12 * (x * x);
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, -4.1], N[(x * N[(N[(x * -0.12), $MachinePrecision] - 0.253), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 2.0], N[(1.0 - N[(x * 0.253), $MachinePrecision]), $MachinePrecision], N[(-0.12 * N[(x * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.1:\\
\;\;\;\;x \cdot \left(x \cdot -0.12 - 0.253\right)\\

\mathbf{elif}\;x \leq 2:\\
\;\;\;\;1 - x \cdot 0.253\\

\mathbf{else}:\\
\;\;\;\;-0.12 \cdot \left(x \cdot x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -4.0999999999999996

    1. Initial program 99.8%

      \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around -inf 99.8%

      \[\leadsto \color{blue}{{x}^{2} \cdot \left(-1 \cdot \frac{0.253 - \frac{1}{x}}{x} - 0.12\right)} \]
    4. Step-by-step derivation
      1. unpow299.8%

        \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \left(-1 \cdot \frac{0.253 - \frac{1}{x}}{x} - 0.12\right) \]
    5. Applied egg-rr99.8%

      \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \left(-1 \cdot \frac{0.253 - \frac{1}{x}}{x} - 0.12\right) \]
    6. Taylor expanded in x around inf 98.5%

      \[\leadsto \left(x \cdot x\right) \cdot \left(\color{blue}{\frac{-0.253}{x}} - 0.12\right) \]
    7. Taylor expanded in x around 0 98.6%

      \[\leadsto \color{blue}{x \cdot \left(-0.12 \cdot x - 0.253\right)} \]

    if -4.0999999999999996 < x < 2

    1. Initial program 100.0%

      \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 99.1%

      \[\leadsto 1 - \color{blue}{0.253 \cdot x} \]
    4. Step-by-step derivation
      1. *-commutative99.1%

        \[\leadsto 1 - \color{blue}{x \cdot 0.253} \]
    5. Simplified99.1%

      \[\leadsto 1 - \color{blue}{x \cdot 0.253} \]

    if 2 < x

    1. Initial program 99.7%

      \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 99.8%

      \[\leadsto \color{blue}{-0.12 \cdot {x}^{2}} \]
    4. Step-by-step derivation
      1. *-commutative99.8%

        \[\leadsto \color{blue}{{x}^{2} \cdot -0.12} \]
    5. Simplified99.8%

      \[\leadsto \color{blue}{{x}^{2} \cdot -0.12} \]
    6. Step-by-step derivation
      1. unpow299.8%

        \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \left(-1 \cdot \frac{0.253 - \frac{1}{x}}{x} - 0.12\right) \]
    7. Applied egg-rr99.8%

      \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot -0.12 \]
  3. Recombined 3 regimes into one program.
  4. Final simplification99.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4.1:\\ \;\;\;\;x \cdot \left(x \cdot -0.12 - 0.253\right)\\ \mathbf{elif}\;x \leq 2:\\ \;\;\;\;1 - x \cdot 0.253\\ \mathbf{else}:\\ \;\;\;\;-0.12 \cdot \left(x \cdot x\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 99.9% accurate, 1.0× speedup?

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

\\
1 - x \cdot \left(x \cdot 0.12 + 0.253\right)
\end{array}
Derivation
  1. Initial program 99.9%

    \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
  2. Add Preprocessing
  3. Final simplification99.9%

    \[\leadsto 1 - x \cdot \left(x \cdot 0.12 + 0.253\right) \]
  4. Add Preprocessing

Alternative 6: 49.2% accurate, 9.0× speedup?

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

\\
1
\end{array}
Derivation
  1. Initial program 99.9%

    \[1 - x \cdot \left(0.253 + x \cdot 0.12\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 49.6%

    \[\leadsto \color{blue}{1} \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2024135 
(FPCore (x)
  :name "Numeric.SpecFunctions:invIncompleteGamma from math-functions-0.1.5.2, A"
  :precision binary64
  (- 1.0 (* x (+ 0.253 (* x 0.12)))))