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

?

Percentage Accurate: 72.4% → 99.8%
Time: 15.3s
Precision: binary64
Cost: 7492

?

\[1 - \log \left(1 - \frac{x - y}{1 - y}\right) \]
\[\begin{array}{l} \mathbf{if}\;\frac{x - y}{1 - y} \leq 0.999999:\\ \;\;\;\;1 - \mathsf{log1p}\left(\frac{y - x}{1 - y}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \log \left(\frac{x + -1}{y}\right)\\ \end{array} \]
(FPCore (x y) :precision binary64 (- 1.0 (log (- 1.0 (/ (- x y) (- 1.0 y))))))
(FPCore (x y)
 :precision binary64
 (if (<= (/ (- x y) (- 1.0 y)) 0.999999)
   (- 1.0 (log1p (/ (- y x) (- 1.0 y))))
   (- 1.0 (log (/ (+ x -1.0) y)))))
double code(double x, double y) {
	return 1.0 - log((1.0 - ((x - y) / (1.0 - y))));
}
double code(double x, double y) {
	double tmp;
	if (((x - y) / (1.0 - y)) <= 0.999999) {
		tmp = 1.0 - log1p(((y - x) / (1.0 - y)));
	} else {
		tmp = 1.0 - log(((x + -1.0) / y));
	}
	return tmp;
}
public static double code(double x, double y) {
	return 1.0 - Math.log((1.0 - ((x - y) / (1.0 - y))));
}
public static double code(double x, double y) {
	double tmp;
	if (((x - y) / (1.0 - y)) <= 0.999999) {
		tmp = 1.0 - Math.log1p(((y - x) / (1.0 - y)));
	} else {
		tmp = 1.0 - Math.log(((x + -1.0) / y));
	}
	return tmp;
}
def code(x, y):
	return 1.0 - math.log((1.0 - ((x - y) / (1.0 - y))))
def code(x, y):
	tmp = 0
	if ((x - y) / (1.0 - y)) <= 0.999999:
		tmp = 1.0 - math.log1p(((y - x) / (1.0 - y)))
	else:
		tmp = 1.0 - math.log(((x + -1.0) / y))
	return tmp
function code(x, y)
	return Float64(1.0 - log(Float64(1.0 - Float64(Float64(x - y) / Float64(1.0 - y)))))
end
function code(x, y)
	tmp = 0.0
	if (Float64(Float64(x - y) / Float64(1.0 - y)) <= 0.999999)
		tmp = Float64(1.0 - log1p(Float64(Float64(y - x) / Float64(1.0 - y))));
	else
		tmp = Float64(1.0 - log(Float64(Float64(x + -1.0) / y)));
	end
	return tmp
end
code[x_, y_] := N[(1.0 - N[Log[N[(1.0 - N[(N[(x - y), $MachinePrecision] / N[(1.0 - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[x_, y_] := If[LessEqual[N[(N[(x - y), $MachinePrecision] / N[(1.0 - y), $MachinePrecision]), $MachinePrecision], 0.999999], N[(1.0 - N[Log[1 + N[(N[(y - x), $MachinePrecision] / N[(1.0 - y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(1.0 - N[Log[N[(N[(x + -1.0), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
1 - \log \left(1 - \frac{x - y}{1 - y}\right)
\begin{array}{l}
\mathbf{if}\;\frac{x - y}{1 - y} \leq 0.999999:\\
\;\;\;\;1 - \mathsf{log1p}\left(\frac{y - x}{1 - y}\right)\\

\mathbf{else}:\\
\;\;\;\;1 - \log \left(\frac{x + -1}{y}\right)\\


\end{array}

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.

Herbie found 7 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original72.4%
Target99.8%
Herbie99.8%
\[\begin{array}{l} \mathbf{if}\;y < -81284752.61947241:\\ \;\;\;\;1 - \log \left(\frac{x}{y \cdot y} - \left(\frac{1}{y} - \frac{x}{y}\right)\right)\\ \mathbf{elif}\;y < 3.0094271212461764 \cdot 10^{+25}:\\ \;\;\;\;\log \left(\frac{e^{1}}{1 - \frac{x - y}{1 - y}}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \log \left(\frac{x}{y \cdot y} - \left(\frac{1}{y} - \frac{x}{y}\right)\right)\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if (/.f64 (-.f64 x y) (-.f64 1 y)) < 0.999998999999999971

    1. Initial program 99.7%

      \[1 - \log \left(1 - \frac{x - y}{1 - y}\right) \]
    2. Simplified99.8%

      \[\leadsto \color{blue}{1 - \mathsf{log1p}\left(\frac{y - x}{1 - y}\right)} \]
      Step-by-step derivation

      [Start]99.7%

      \[ 1 - \log \left(1 - \frac{x - y}{1 - y}\right) \]

      sub-neg [=>]99.7%

      \[ 1 - \log \color{blue}{\left(1 + \left(-\frac{x - y}{1 - y}\right)\right)} \]

      log1p-def [=>]99.8%

      \[ 1 - \color{blue}{\mathsf{log1p}\left(-\frac{x - y}{1 - y}\right)} \]

      neg-sub0 [=>]99.8%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{0 - \frac{x - y}{1 - y}}\right) \]

      div-sub [=>]99.8%

      \[ 1 - \mathsf{log1p}\left(0 - \color{blue}{\left(\frac{x}{1 - y} - \frac{y}{1 - y}\right)}\right) \]

      associate--r- [=>]99.8%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\left(0 - \frac{x}{1 - y}\right) + \frac{y}{1 - y}}\right) \]

      neg-sub0 [<=]99.8%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\left(-\frac{x}{1 - y}\right)} + \frac{y}{1 - y}\right) \]

      +-commutative [=>]99.8%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\frac{y}{1 - y} + \left(-\frac{x}{1 - y}\right)}\right) \]

      sub-neg [<=]99.8%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\frac{y}{1 - y} - \frac{x}{1 - y}}\right) \]

      div-sub [<=]99.8%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\frac{y - x}{1 - y}}\right) \]

    if 0.999998999999999971 < (/.f64 (-.f64 x y) (-.f64 1 y))

    1. Initial program 4.5%

      \[1 - \log \left(1 - \frac{x - y}{1 - y}\right) \]
    2. Simplified4.5%

      \[\leadsto \color{blue}{1 - \mathsf{log1p}\left(\frac{y - x}{1 - y}\right)} \]
      Step-by-step derivation

      [Start]4.5%

      \[ 1 - \log \left(1 - \frac{x - y}{1 - y}\right) \]

      sub-neg [=>]4.5%

      \[ 1 - \log \color{blue}{\left(1 + \left(-\frac{x - y}{1 - y}\right)\right)} \]

      log1p-def [=>]4.5%

      \[ 1 - \color{blue}{\mathsf{log1p}\left(-\frac{x - y}{1 - y}\right)} \]

      neg-sub0 [=>]4.5%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{0 - \frac{x - y}{1 - y}}\right) \]

      div-sub [=>]4.5%

      \[ 1 - \mathsf{log1p}\left(0 - \color{blue}{\left(\frac{x}{1 - y} - \frac{y}{1 - y}\right)}\right) \]

      associate--r- [=>]4.5%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\left(0 - \frac{x}{1 - y}\right) + \frac{y}{1 - y}}\right) \]

      neg-sub0 [<=]4.5%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\left(-\frac{x}{1 - y}\right)} + \frac{y}{1 - y}\right) \]

      +-commutative [=>]4.5%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\frac{y}{1 - y} + \left(-\frac{x}{1 - y}\right)}\right) \]

      sub-neg [<=]4.5%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\frac{y}{1 - y} - \frac{x}{1 - y}}\right) \]

      div-sub [<=]4.5%

      \[ 1 - \mathsf{log1p}\left(\color{blue}{\frac{y - x}{1 - y}}\right) \]
    3. Taylor expanded in y around inf 4.5%

      \[\leadsto 1 - \mathsf{log1p}\left(\color{blue}{\frac{x}{y} - \left(\frac{1}{y} + 1\right)}\right) \]
    4. Applied egg-rr4.5%

      \[\leadsto 1 - \color{blue}{\log \left(1 + \left(\frac{x}{y} - \left(1 + \frac{1}{y}\right)\right)\right)} \]
      Step-by-step derivation

      [Start]4.5%

      \[ 1 - \mathsf{log1p}\left(\frac{x}{y} - \left(\frac{1}{y} + 1\right)\right) \]

      log1p-udef [=>]4.5%

      \[ 1 - \color{blue}{\log \left(1 + \left(\frac{x}{y} - \left(\frac{1}{y} + 1\right)\right)\right)} \]

      +-commutative [=>]4.5%

      \[ 1 - \log \left(1 + \left(\frac{x}{y} - \color{blue}{\left(1 + \frac{1}{y}\right)}\right)\right) \]
    5. Taylor expanded in y around 0 100.0%

      \[\leadsto 1 - \log \color{blue}{\left(\frac{x - 1}{y}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x - y}{1 - y} \leq 0.999999:\\ \;\;\;\;1 - \mathsf{log1p}\left(\frac{y - x}{1 - y}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \log \left(\frac{x + -1}{y}\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.8%
Cost7492
\[\begin{array}{l} \mathbf{if}\;\frac{x - y}{1 - y} \leq 0.999999:\\ \;\;\;\;1 - \mathsf{log1p}\left(\frac{y - x}{1 - y}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \log \left(\frac{x + -1}{y}\right)\\ \end{array} \]
Alternative 2
Accuracy98.8%
Cost7113
\[\begin{array}{l} \mathbf{if}\;y \leq -1.75 \lor \neg \left(y \leq 1\right):\\ \;\;\;\;1 - \log \left(\frac{x + -1}{y}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \left(y + \mathsf{log1p}\left(-x\right)\right)\\ \end{array} \]
Alternative 3
Accuracy90.2%
Cost7048
\[\begin{array}{l} \mathbf{if}\;y \leq -27:\\ \;\;\;\;1 - \log \left(\frac{-1}{y}\right)\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;1 - \left(y + \mathsf{log1p}\left(-x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \log \left(\frac{x}{y}\right)\\ \end{array} \]
Alternative 4
Accuracy89.6%
Cost6984
\[\begin{array}{l} \mathbf{if}\;y \leq -9.2:\\ \;\;\;\;1 - \log \left(\frac{-1}{y}\right)\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;1 - \mathsf{log1p}\left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \log \left(\frac{x}{y}\right)\\ \end{array} \]
Alternative 5
Accuracy80.4%
Cost6852
\[\begin{array}{l} \mathbf{if}\;y \leq -25:\\ \;\;\;\;1 - \log \left(\frac{-1}{y}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \mathsf{log1p}\left(-x\right)\\ \end{array} \]
Alternative 6
Accuracy63.7%
Cost6656
\[1 - \mathsf{log1p}\left(-x\right) \]
Alternative 7
Accuracy43.9%
Cost192
\[x + 1 \]

Reproduce?

herbie shell --seed 2023272 
(FPCore (x y)
  :name "Numeric.SpecFunctions:invIncompleteGamma from math-functions-0.1.5.2, B"
  :precision binary64

  :herbie-target
  (if (< y -81284752.61947241) (- 1.0 (log (- (/ x (* y y)) (- (/ 1.0 y) (/ x y))))) (if (< y 3.0094271212461764e+25) (log (/ (exp 1.0) (- 1.0 (/ (- x y) (- 1.0 y))))) (- 1.0 (log (- (/ x (* y y)) (- (/ 1.0 y) (/ x y)))))))

  (- 1.0 (log (- 1.0 (/ (- x y) (- 1.0 y))))))