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

?

Percentage Accurate: 69.1% → 99.6%
Time: 13.8s
Precision: binary64
Cost: 1608

?

\[x + \frac{y \cdot \left(\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525\right)}{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304} \]
\[\begin{array}{l} \mathbf{if}\;z \leq -420000000:\\ \;\;\;\;x + \frac{y}{14.431876219268936 - \frac{15.646356830292042}{z}}\\ \mathbf{elif}\;z \leq 100:\\ \;\;\;\;\frac{y \cdot \left(z \cdot \left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) + 0.279195317918525\right)}{z \cdot \left(z + 6.012459259764103\right) + 3.350343815022304} + x\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{14.431876219268936}\\ \end{array} \]
(FPCore (x y z)
 :precision binary64
 (+
  x
  (/
   (*
    y
    (+
     (* (+ (* z 0.0692910599291889) 0.4917317610505968) z)
     0.279195317918525))
   (+ (* (+ z 6.012459259764103) z) 3.350343815022304))))
(FPCore (x y z)
 :precision binary64
 (if (<= z -420000000.0)
   (+ x (/ y (- 14.431876219268936 (/ 15.646356830292042 z))))
   (if (<= z 100.0)
     (+
      (/
       (*
        y
        (+
         (* z (+ (* z 0.0692910599291889) 0.4917317610505968))
         0.279195317918525))
       (+ (* z (+ z 6.012459259764103)) 3.350343815022304))
      x)
     (+ x (/ y 14.431876219268936)))))
double code(double x, double y, double z) {
	return x + ((y * ((((z * 0.0692910599291889) + 0.4917317610505968) * z) + 0.279195317918525)) / (((z + 6.012459259764103) * z) + 3.350343815022304));
}
double code(double x, double y, double z) {
	double tmp;
	if (z <= -420000000.0) {
		tmp = x + (y / (14.431876219268936 - (15.646356830292042 / z)));
	} else if (z <= 100.0) {
		tmp = ((y * ((z * ((z * 0.0692910599291889) + 0.4917317610505968)) + 0.279195317918525)) / ((z * (z + 6.012459259764103)) + 3.350343815022304)) + x;
	} else {
		tmp = x + (y / 14.431876219268936);
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + ((y * ((((z * 0.0692910599291889d0) + 0.4917317610505968d0) * z) + 0.279195317918525d0)) / (((z + 6.012459259764103d0) * z) + 3.350343815022304d0))
end function
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 (z <= (-420000000.0d0)) then
        tmp = x + (y / (14.431876219268936d0 - (15.646356830292042d0 / z)))
    else if (z <= 100.0d0) then
        tmp = ((y * ((z * ((z * 0.0692910599291889d0) + 0.4917317610505968d0)) + 0.279195317918525d0)) / ((z * (z + 6.012459259764103d0)) + 3.350343815022304d0)) + x
    else
        tmp = x + (y / 14.431876219268936d0)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	return x + ((y * ((((z * 0.0692910599291889) + 0.4917317610505968) * z) + 0.279195317918525)) / (((z + 6.012459259764103) * z) + 3.350343815022304));
}
public static double code(double x, double y, double z) {
	double tmp;
	if (z <= -420000000.0) {
		tmp = x + (y / (14.431876219268936 - (15.646356830292042 / z)));
	} else if (z <= 100.0) {
		tmp = ((y * ((z * ((z * 0.0692910599291889) + 0.4917317610505968)) + 0.279195317918525)) / ((z * (z + 6.012459259764103)) + 3.350343815022304)) + x;
	} else {
		tmp = x + (y / 14.431876219268936);
	}
	return tmp;
}
def code(x, y, z):
	return x + ((y * ((((z * 0.0692910599291889) + 0.4917317610505968) * z) + 0.279195317918525)) / (((z + 6.012459259764103) * z) + 3.350343815022304))
def code(x, y, z):
	tmp = 0
	if z <= -420000000.0:
		tmp = x + (y / (14.431876219268936 - (15.646356830292042 / z)))
	elif z <= 100.0:
		tmp = ((y * ((z * ((z * 0.0692910599291889) + 0.4917317610505968)) + 0.279195317918525)) / ((z * (z + 6.012459259764103)) + 3.350343815022304)) + x
	else:
		tmp = x + (y / 14.431876219268936)
	return tmp
function code(x, y, z)
	return Float64(x + Float64(Float64(y * Float64(Float64(Float64(Float64(z * 0.0692910599291889) + 0.4917317610505968) * z) + 0.279195317918525)) / Float64(Float64(Float64(z + 6.012459259764103) * z) + 3.350343815022304)))
end
function code(x, y, z)
	tmp = 0.0
	if (z <= -420000000.0)
		tmp = Float64(x + Float64(y / Float64(14.431876219268936 - Float64(15.646356830292042 / z))));
	elseif (z <= 100.0)
		tmp = Float64(Float64(Float64(y * Float64(Float64(z * Float64(Float64(z * 0.0692910599291889) + 0.4917317610505968)) + 0.279195317918525)) / Float64(Float64(z * Float64(z + 6.012459259764103)) + 3.350343815022304)) + x);
	else
		tmp = Float64(x + Float64(y / 14.431876219268936));
	end
	return tmp
end
function tmp = code(x, y, z)
	tmp = x + ((y * ((((z * 0.0692910599291889) + 0.4917317610505968) * z) + 0.279195317918525)) / (((z + 6.012459259764103) * z) + 3.350343815022304));
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (z <= -420000000.0)
		tmp = x + (y / (14.431876219268936 - (15.646356830292042 / z)));
	elseif (z <= 100.0)
		tmp = ((y * ((z * ((z * 0.0692910599291889) + 0.4917317610505968)) + 0.279195317918525)) / ((z * (z + 6.012459259764103)) + 3.350343815022304)) + x;
	else
		tmp = x + (y / 14.431876219268936);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := N[(x + N[(N[(y * N[(N[(N[(N[(z * 0.0692910599291889), $MachinePrecision] + 0.4917317610505968), $MachinePrecision] * z), $MachinePrecision] + 0.279195317918525), $MachinePrecision]), $MachinePrecision] / N[(N[(N[(z + 6.012459259764103), $MachinePrecision] * z), $MachinePrecision] + 3.350343815022304), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_] := If[LessEqual[z, -420000000.0], N[(x + N[(y / N[(14.431876219268936 - N[(15.646356830292042 / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 100.0], N[(N[(N[(y * N[(N[(z * N[(N[(z * 0.0692910599291889), $MachinePrecision] + 0.4917317610505968), $MachinePrecision]), $MachinePrecision] + 0.279195317918525), $MachinePrecision]), $MachinePrecision] / N[(N[(z * N[(z + 6.012459259764103), $MachinePrecision]), $MachinePrecision] + 3.350343815022304), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision], N[(x + N[(y / 14.431876219268936), $MachinePrecision]), $MachinePrecision]]]
x + \frac{y \cdot \left(\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525\right)}{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304}
\begin{array}{l}
\mathbf{if}\;z \leq -420000000:\\
\;\;\;\;x + \frac{y}{14.431876219268936 - \frac{15.646356830292042}{z}}\\

\mathbf{elif}\;z \leq 100:\\
\;\;\;\;\frac{y \cdot \left(z \cdot \left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) + 0.279195317918525\right)}{z \cdot \left(z + 6.012459259764103\right) + 3.350343815022304} + x\\

\mathbf{else}:\\
\;\;\;\;x + \frac{y}{14.431876219268936}\\


\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 12 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

Original69.1%
Target99.4%
Herbie99.6%
\[\begin{array}{l} \mathbf{if}\;z < -8120153.652456675:\\ \;\;\;\;\left(\frac{0.07512208616047561}{z} + 0.0692910599291889\right) \cdot y - \left(\frac{0.40462203869992125 \cdot y}{z \cdot z} - x\right)\\ \mathbf{elif}\;z < 6.576118972787377 \cdot 10^{+20}:\\ \;\;\;\;x + \left(y \cdot \left(\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525\right)\right) \cdot \frac{1}{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304}\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{0.07512208616047561}{z} + 0.0692910599291889\right) \cdot y - \left(\frac{0.40462203869992125 \cdot y}{z \cdot z} - x\right)\\ \end{array} \]

Derivation?

  1. Split input into 3 regimes
  2. if z < -4.2e8

    1. Initial program 52.3%

      \[x + \frac{y \cdot \left(\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525\right)}{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304} \]
    2. Simplified61.9%

      \[\leadsto \color{blue}{x + \frac{y}{\frac{\mathsf{fma}\left(z + 6.012459259764103, z, 3.350343815022304\right)}{\mathsf{fma}\left(\mathsf{fma}\left(z, 0.0692910599291889, 0.4917317610505968\right), z, 0.279195317918525\right)}}} \]
      Step-by-step derivation

      [Start]52.3%

      \[ x + \frac{y \cdot \left(\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525\right)}{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304} \]

      associate-/l* [=>]61.9%

      \[ x + \color{blue}{\frac{y}{\frac{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304}{\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525}}} \]

      fma-def [=>]61.9%

      \[ x + \frac{y}{\frac{\color{blue}{\mathsf{fma}\left(z + 6.012459259764103, z, 3.350343815022304\right)}}{\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525}} \]

      fma-def [=>]61.9%

      \[ x + \frac{y}{\frac{\mathsf{fma}\left(z + 6.012459259764103, z, 3.350343815022304\right)}{\color{blue}{\mathsf{fma}\left(z \cdot 0.0692910599291889 + 0.4917317610505968, z, 0.279195317918525\right)}}} \]

      fma-def [=>]61.9%

      \[ x + \frac{y}{\frac{\mathsf{fma}\left(z + 6.012459259764103, z, 3.350343815022304\right)}{\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(z, 0.0692910599291889, 0.4917317610505968\right)}, z, 0.279195317918525\right)}} \]
    3. Taylor expanded in z around inf 99.9%

      \[\leadsto x + \frac{y}{\color{blue}{14.431876219268936 - 15.646356830292042 \cdot \frac{1}{z}}} \]
    4. Simplified99.9%

      \[\leadsto x + \frac{y}{\color{blue}{14.431876219268936 - \frac{15.646356830292042}{z}}} \]
      Step-by-step derivation

      [Start]99.9%

      \[ x + \frac{y}{14.431876219268936 - 15.646356830292042 \cdot \frac{1}{z}} \]

      associate-*r/ [=>]99.9%

      \[ x + \frac{y}{14.431876219268936 - \color{blue}{\frac{15.646356830292042 \cdot 1}{z}}} \]

      metadata-eval [=>]99.9%

      \[ x + \frac{y}{14.431876219268936 - \frac{\color{blue}{15.646356830292042}}{z}} \]

    if -4.2e8 < z < 100

    1. Initial program 99.7%

      \[x + \frac{y \cdot \left(\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525\right)}{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304} \]

    if 100 < z

    1. Initial program 48.2%

      \[x + \frac{y \cdot \left(\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525\right)}{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304} \]
    2. Simplified59.7%

      \[\leadsto \color{blue}{x + \frac{y}{\frac{\mathsf{fma}\left(z + 6.012459259764103, z, 3.350343815022304\right)}{\mathsf{fma}\left(\mathsf{fma}\left(z, 0.0692910599291889, 0.4917317610505968\right), z, 0.279195317918525\right)}}} \]
      Step-by-step derivation

      [Start]48.2%

      \[ x + \frac{y \cdot \left(\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525\right)}{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304} \]

      associate-/l* [=>]59.7%

      \[ x + \color{blue}{\frac{y}{\frac{\left(z + 6.012459259764103\right) \cdot z + 3.350343815022304}{\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525}}} \]

      fma-def [=>]59.7%

      \[ x + \frac{y}{\frac{\color{blue}{\mathsf{fma}\left(z + 6.012459259764103, z, 3.350343815022304\right)}}{\left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) \cdot z + 0.279195317918525}} \]

      fma-def [=>]59.7%

      \[ x + \frac{y}{\frac{\mathsf{fma}\left(z + 6.012459259764103, z, 3.350343815022304\right)}{\color{blue}{\mathsf{fma}\left(z \cdot 0.0692910599291889 + 0.4917317610505968, z, 0.279195317918525\right)}}} \]

      fma-def [=>]59.7%

      \[ x + \frac{y}{\frac{\mathsf{fma}\left(z + 6.012459259764103, z, 3.350343815022304\right)}{\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(z, 0.0692910599291889, 0.4917317610505968\right)}, z, 0.279195317918525\right)}} \]
    3. Taylor expanded in z around inf 100.0%

      \[\leadsto x + \frac{y}{\color{blue}{14.431876219268936}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification99.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -420000000:\\ \;\;\;\;x + \frac{y}{14.431876219268936 - \frac{15.646356830292042}{z}}\\ \mathbf{elif}\;z \leq 100:\\ \;\;\;\;\frac{y \cdot \left(z \cdot \left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) + 0.279195317918525\right)}{z \cdot \left(z + 6.012459259764103\right) + 3.350343815022304} + x\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{14.431876219268936}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.6%
Cost1608
\[\begin{array}{l} \mathbf{if}\;z \leq -420000000:\\ \;\;\;\;x + \frac{y}{14.431876219268936 - \frac{15.646356830292042}{z}}\\ \mathbf{elif}\;z \leq 100:\\ \;\;\;\;\frac{y \cdot \left(z \cdot \left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) + 0.279195317918525\right)}{z \cdot \left(z + 6.012459259764103\right) + 3.350343815022304} + x\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{14.431876219268936}\\ \end{array} \]
Alternative 2
Accuracy99.8%
Cost27716
\[\begin{array}{l} \mathbf{if}\;\frac{y \cdot \left(z \cdot \left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) + 0.279195317918525\right)}{z \cdot \left(z + 6.012459259764103\right) + 3.350343815022304} \leq 2 \cdot 10^{+307}:\\ \;\;\;\;\mathsf{fma}\left(y, \frac{\mathsf{fma}\left(z, \mathsf{fma}\left(z, 0.0692910599291889, 0.4917317610505968\right), 0.279195317918525\right)}{\mathsf{fma}\left(z, z + 6.012459259764103, 3.350343815022304\right)}, x\right)\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{14.431876219268936}\\ \end{array} \]
Alternative 3
Accuracy99.5%
Cost15684
\[\begin{array}{l} \mathbf{if}\;\frac{y \cdot \left(z \cdot \left(z \cdot 0.0692910599291889 + 0.4917317610505968\right) + 0.279195317918525\right)}{z \cdot \left(z + 6.012459259764103\right) + 3.350343815022304} \leq 2 \cdot 10^{+307}:\\ \;\;\;\;x + \frac{y}{\frac{\mathsf{fma}\left(z + 6.012459259764103, z, 3.350343815022304\right)}{\mathsf{fma}\left(\frac{0.24180012482592123 - 0.004801250986110448 \cdot \left(z \cdot z\right)}{0.4917317610505968 - z \cdot 0.0692910599291889}, z, 0.279195317918525\right)}}\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{14.431876219268936}\\ \end{array} \]
Alternative 4
Accuracy99.3%
Cost1096
\[\begin{array}{l} \mathbf{if}\;z \leq -120000000:\\ \;\;\;\;x + \frac{y}{14.431876219268936 - \frac{15.646356830292042}{z}}\\ \mathbf{elif}\;z \leq 3.2:\\ \;\;\;\;x + \frac{y}{z \cdot 0.39999999996247915 + 12.000000000000014}\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{14.431876219268936 + \frac{\frac{101.23733352003822}{z} - 15.646356830292042}{z}}\\ \end{array} \]
Alternative 5
Accuracy99.0%
Cost841
\[\begin{array}{l} \mathbf{if}\;z \leq -120000000 \lor \neg \left(z \leq 3.2\right):\\ \;\;\;\;x + \frac{y}{14.431876219268936}\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{z \cdot 0.39999999996247915 + 12.000000000000014}\\ \end{array} \]
Alternative 6
Accuracy99.2%
Cost841
\[\begin{array}{l} \mathbf{if}\;z \leq -120000000 \lor \neg \left(z \leq 3.2\right):\\ \;\;\;\;x + \frac{y}{14.431876219268936 - \frac{15.646356830292042}{z}}\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{z \cdot 0.39999999996247915 + 12.000000000000014}\\ \end{array} \]
Alternative 7
Accuracy60.9%
Cost720
\[\begin{array}{l} \mathbf{if}\;x \leq -4.2 \cdot 10^{-189}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq -3 \cdot 10^{-302}:\\ \;\;\;\;y \cdot 0.08333333333333323\\ \mathbf{elif}\;x \leq 4.8 \cdot 10^{-231}:\\ \;\;\;\;y \cdot 0.0692910599291889\\ \mathbf{elif}\;x \leq 4.9 \cdot 10^{-26}:\\ \;\;\;\;y \cdot 0.08333333333333323\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 8
Accuracy61.0%
Cost720
\[\begin{array}{l} \mathbf{if}\;x \leq -2.7 \cdot 10^{-189}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq -7 \cdot 10^{-300}:\\ \;\;\;\;\frac{y}{12.000000000000014}\\ \mathbf{elif}\;x \leq 9.6 \cdot 10^{-229}:\\ \;\;\;\;y \cdot 0.0692910599291889\\ \mathbf{elif}\;x \leq 3.1 \cdot 10^{-41}:\\ \;\;\;\;\frac{y}{12.000000000000014}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 9
Accuracy98.9%
Cost585
\[\begin{array}{l} \mathbf{if}\;z \leq -120000000 \lor \neg \left(z \leq 3.2\right):\\ \;\;\;\;x + \frac{y}{14.431876219268936}\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{12.000000000000014}\\ \end{array} \]
Alternative 10
Accuracy60.6%
Cost456
\[\begin{array}{l} \mathbf{if}\;x \leq -1.22 \cdot 10^{-191}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq 1.04 \cdot 10^{-26}:\\ \;\;\;\;y \cdot 0.08333333333333323\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 11
Accuracy79.7%
Cost320
\[x + \frac{y}{12.000000000000014} \]
Alternative 12
Accuracy50.9%
Cost64
\[x \]

Reproduce?

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

  :herbie-target
  (if (< z -8120153.652456675) (- (* (+ (/ 0.07512208616047561 z) 0.0692910599291889) y) (- (/ (* 0.40462203869992125 y) (* z z)) x)) (if (< z 6.576118972787377e+20) (+ x (* (* y (+ (* (+ (* z 0.0692910599291889) 0.4917317610505968) z) 0.279195317918525)) (/ 1.0 (+ (* (+ z 6.012459259764103) z) 3.350343815022304)))) (- (* (+ (/ 0.07512208616047561 z) 0.0692910599291889) y) (- (/ (* 0.40462203869992125 y) (* z z)) x))))

  (+ x (/ (* y (+ (* (+ (* z 0.0692910599291889) 0.4917317610505968) z) 0.279195317918525)) (+ (* (+ z 6.012459259764103) z) 3.350343815022304))))