exact-inv-half-inv-exp

Percentage Accurate: 100.0% → 100.0%
Time: 1.3min
Alternatives: 7
Speedup: 1.0×

Specification

?
\[\frac{0.5}{e^{2 \cdot Ce}} \]
(FPCore (Ce)
  :precision binary64
  :pre TRUE
  (/ 0.5 (exp (* 2.0 Ce))))
double code(double Ce) {
	return 0.5 / exp((2.0 * Ce));
}
real(8) function code(ce)
use fmin_fmax_functions
    real(8), intent (in) :: ce
    code = 0.5d0 / exp((2.0d0 * ce))
end function
public static double code(double Ce) {
	return 0.5 / Math.exp((2.0 * Ce));
}
def code(Ce):
	return 0.5 / math.exp((2.0 * Ce))
function code(Ce)
	return Float64(0.5 / exp(Float64(2.0 * Ce)))
end
function tmp = code(Ce)
	tmp = 0.5 / exp((2.0 * Ce));
end
code[Ce_] := N[(0.5 / N[Exp[N[(2.0 * Ce), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
f(Ce):
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Ce: real): real =
	(5e-1) / (exp(((2) * Ce)))
END code
\frac{0.5}{e^{2 \cdot Ce}}

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 7 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?

\[\frac{0.5}{e^{2 \cdot Ce}} \]
(FPCore (Ce)
  :precision binary64
  :pre TRUE
  (/ 0.5 (exp (* 2.0 Ce))))
double code(double Ce) {
	return 0.5 / exp((2.0 * Ce));
}
real(8) function code(ce)
use fmin_fmax_functions
    real(8), intent (in) :: ce
    code = 0.5d0 / exp((2.0d0 * ce))
end function
public static double code(double Ce) {
	return 0.5 / Math.exp((2.0 * Ce));
}
def code(Ce):
	return 0.5 / math.exp((2.0 * Ce))
function code(Ce)
	return Float64(0.5 / exp(Float64(2.0 * Ce)))
end
function tmp = code(Ce)
	tmp = 0.5 / exp((2.0 * Ce));
end
code[Ce_] := N[(0.5 / N[Exp[N[(2.0 * Ce), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
f(Ce):
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Ce: real): real =
	(5e-1) / (exp(((2) * Ce)))
END code
\frac{0.5}{e^{2 \cdot Ce}}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[e^{-2 \cdot Ce} \cdot 0.5 \]
(FPCore (Ce)
  :precision binary64
  :pre TRUE
  (* (exp (* -2.0 Ce)) 0.5))
double code(double Ce) {
	return exp((-2.0 * Ce)) * 0.5;
}
real(8) function code(ce)
use fmin_fmax_functions
    real(8), intent (in) :: ce
    code = exp(((-2.0d0) * ce)) * 0.5d0
end function
public static double code(double Ce) {
	return Math.exp((-2.0 * Ce)) * 0.5;
}
def code(Ce):
	return math.exp((-2.0 * Ce)) * 0.5
function code(Ce)
	return Float64(exp(Float64(-2.0 * Ce)) * 0.5)
end
function tmp = code(Ce)
	tmp = exp((-2.0 * Ce)) * 0.5;
end
code[Ce_] := N[(N[Exp[N[(-2.0 * Ce), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]
f(Ce):
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Ce: real): real =
	(exp(((-2) * Ce))) * (5e-1)
END code
e^{-2 \cdot Ce} \cdot 0.5
Derivation
  1. Initial program 100.0%

    \[\frac{0.5}{e^{2 \cdot Ce}} \]
  2. Applied rewrites100.0%

    \[\leadsto e^{-2 \cdot Ce} \cdot 0.5 \]
  3. Add Preprocessing

Alternative 2: 98.6% accurate, 0.8× speedup?

\[\begin{array}{l} \mathbf{if}\;2 \cdot Ce \leq 0.2:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(-0.6666666666666666, Ce, 1\right), Ce \cdot Ce, 0.5\right) - Ce\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
(FPCore (Ce)
  :precision binary64
  :pre TRUE
  (if (<= (* 2.0 Ce) 0.2)
  (- (fma (fma -0.6666666666666666 Ce 1.0) (* Ce Ce) 0.5) Ce)
  0.0))
double code(double Ce) {
	double tmp;
	if ((2.0 * Ce) <= 0.2) {
		tmp = fma(fma(-0.6666666666666666, Ce, 1.0), (Ce * Ce), 0.5) - Ce;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
function code(Ce)
	tmp = 0.0
	if (Float64(2.0 * Ce) <= 0.2)
		tmp = Float64(fma(fma(-0.6666666666666666, Ce, 1.0), Float64(Ce * Ce), 0.5) - Ce);
	else
		tmp = 0.0;
	end
	return tmp
end
code[Ce_] := If[LessEqual[N[(2.0 * Ce), $MachinePrecision], 0.2], N[(N[(N[(-0.6666666666666666 * Ce + 1.0), $MachinePrecision] * N[(Ce * Ce), $MachinePrecision] + 0.5), $MachinePrecision] - Ce), $MachinePrecision], 0.0]
f(Ce):
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Ce: real): real =
	LET tmp = IF (((2) * Ce) <= (200000000000000011102230246251565404236316680908203125e-54)) THEN ((((((-66666666666666662965923251249478198587894439697265625e-53) * Ce) + (1)) * (Ce * Ce)) + (5e-1)) - Ce) ELSE (0) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;2 \cdot Ce \leq 0.2:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(-0.6666666666666666, Ce, 1\right), Ce \cdot Ce, 0.5\right) - Ce\\

\mathbf{else}:\\
\;\;\;\;0\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 #s(literal 2 binary64) Ce) < 0.20000000000000001

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} + Ce \cdot \left(Ce \cdot \left(1 + \frac{-2}{3} \cdot Ce\right) - 1\right) \]
    3. Applied rewrites67.0%

      \[\leadsto 0.5 + Ce \cdot \left(Ce \cdot \left(1 + -0.6666666666666666 \cdot Ce\right) - 1\right) \]
    4. Applied rewrites67.0%

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(-0.6666666666666666, Ce, 1\right), Ce \cdot Ce, 0.5\right) - Ce \]

    if 0.20000000000000001 < (*.f64 #s(literal 2 binary64) Ce)

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} \]
    3. Applied rewrites66.2%

      \[\leadsto 0.5 \]
    4. Taylor expanded in undef-var around zero

      \[\leadsto 0 \]
    5. Applied rewrites34.3%

      \[\leadsto 0 \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 98.6% accurate, 0.8× speedup?

\[\begin{array}{l} \mathbf{if}\;2 \cdot Ce \leq 0.2:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.6666666666666666, Ce, 1\right), Ce, -1\right), Ce, 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
(FPCore (Ce)
  :precision binary64
  :pre TRUE
  (if (<= (* 2.0 Ce) 0.2)
  (fma (fma (fma -0.6666666666666666 Ce 1.0) Ce -1.0) Ce 0.5)
  0.0))
double code(double Ce) {
	double tmp;
	if ((2.0 * Ce) <= 0.2) {
		tmp = fma(fma(fma(-0.6666666666666666, Ce, 1.0), Ce, -1.0), Ce, 0.5);
	} else {
		tmp = 0.0;
	}
	return tmp;
}
function code(Ce)
	tmp = 0.0
	if (Float64(2.0 * Ce) <= 0.2)
		tmp = fma(fma(fma(-0.6666666666666666, Ce, 1.0), Ce, -1.0), Ce, 0.5);
	else
		tmp = 0.0;
	end
	return tmp
end
code[Ce_] := If[LessEqual[N[(2.0 * Ce), $MachinePrecision], 0.2], N[(N[(N[(-0.6666666666666666 * Ce + 1.0), $MachinePrecision] * Ce + -1.0), $MachinePrecision] * Ce + 0.5), $MachinePrecision], 0.0]
f(Ce):
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Ce: real): real =
	LET tmp = IF (((2) * Ce) <= (200000000000000011102230246251565404236316680908203125e-54)) THEN (((((((-66666666666666662965923251249478198587894439697265625e-53) * Ce) + (1)) * Ce) + (-1)) * Ce) + (5e-1)) ELSE (0) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;2 \cdot Ce \leq 0.2:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.6666666666666666, Ce, 1\right), Ce, -1\right), Ce, 0.5\right)\\

\mathbf{else}:\\
\;\;\;\;0\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 #s(literal 2 binary64) Ce) < 0.20000000000000001

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} + Ce \cdot \left(Ce \cdot \left(1 + \frac{-2}{3} \cdot Ce\right) - 1\right) \]
    3. Applied rewrites67.0%

      \[\leadsto 0.5 + Ce \cdot \left(Ce \cdot \left(1 + -0.6666666666666666 \cdot Ce\right) - 1\right) \]
    4. Applied rewrites67.0%

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.6666666666666666, Ce, 1\right), Ce, -1\right), Ce, 0.5\right) \]

    if 0.20000000000000001 < (*.f64 #s(literal 2 binary64) Ce)

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} \]
    3. Applied rewrites66.2%

      \[\leadsto 0.5 \]
    4. Taylor expanded in undef-var around zero

      \[\leadsto 0 \]
    5. Applied rewrites34.3%

      \[\leadsto 0 \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 98.5% accurate, 1.2× speedup?

\[\begin{array}{l} \mathbf{if}\;2 \cdot Ce \leq 0.2:\\ \;\;\;\;\mathsf{fma}\left(Ce, Ce, 0.5 - Ce\right)\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
(FPCore (Ce)
  :precision binary64
  :pre TRUE
  (if (<= (* 2.0 Ce) 0.2) (fma Ce Ce (- 0.5 Ce)) 0.0))
double code(double Ce) {
	double tmp;
	if ((2.0 * Ce) <= 0.2) {
		tmp = fma(Ce, Ce, (0.5 - Ce));
	} else {
		tmp = 0.0;
	}
	return tmp;
}
function code(Ce)
	tmp = 0.0
	if (Float64(2.0 * Ce) <= 0.2)
		tmp = fma(Ce, Ce, Float64(0.5 - Ce));
	else
		tmp = 0.0;
	end
	return tmp
end
code[Ce_] := If[LessEqual[N[(2.0 * Ce), $MachinePrecision], 0.2], N[(Ce * Ce + N[(0.5 - Ce), $MachinePrecision]), $MachinePrecision], 0.0]
f(Ce):
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Ce: real): real =
	LET tmp = IF (((2) * Ce) <= (200000000000000011102230246251565404236316680908203125e-54)) THEN ((Ce * Ce) + ((5e-1) - Ce)) ELSE (0) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;2 \cdot Ce \leq 0.2:\\
\;\;\;\;\mathsf{fma}\left(Ce, Ce, 0.5 - Ce\right)\\

\mathbf{else}:\\
\;\;\;\;0\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 #s(literal 2 binary64) Ce) < 0.20000000000000001

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} + Ce \cdot \left(Ce - 1\right) \]
    3. Applied rewrites66.9%

      \[\leadsto 0.5 + Ce \cdot \left(Ce - 1\right) \]
    4. Applied rewrites66.9%

      \[\leadsto \mathsf{fma}\left(Ce, Ce, 0.5 - Ce\right) \]

    if 0.20000000000000001 < (*.f64 #s(literal 2 binary64) Ce)

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} \]
    3. Applied rewrites66.2%

      \[\leadsto 0.5 \]
    4. Taylor expanded in undef-var around zero

      \[\leadsto 0 \]
    5. Applied rewrites34.3%

      \[\leadsto 0 \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 98.2% accurate, 1.8× speedup?

\[\begin{array}{l} \mathbf{if}\;2 \cdot Ce \leq 0.2:\\ \;\;\;\;0.5 - Ce\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
(FPCore (Ce)
  :precision binary64
  :pre TRUE
  (if (<= (* 2.0 Ce) 0.2) (- 0.5 Ce) 0.0))
double code(double Ce) {
	double tmp;
	if ((2.0 * Ce) <= 0.2) {
		tmp = 0.5 - Ce;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
real(8) function code(ce)
use fmin_fmax_functions
    real(8), intent (in) :: ce
    real(8) :: tmp
    if ((2.0d0 * ce) <= 0.2d0) then
        tmp = 0.5d0 - ce
    else
        tmp = 0.0d0
    end if
    code = tmp
end function
public static double code(double Ce) {
	double tmp;
	if ((2.0 * Ce) <= 0.2) {
		tmp = 0.5 - Ce;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
def code(Ce):
	tmp = 0
	if (2.0 * Ce) <= 0.2:
		tmp = 0.5 - Ce
	else:
		tmp = 0.0
	return tmp
function code(Ce)
	tmp = 0.0
	if (Float64(2.0 * Ce) <= 0.2)
		tmp = Float64(0.5 - Ce);
	else
		tmp = 0.0;
	end
	return tmp
end
function tmp_2 = code(Ce)
	tmp = 0.0;
	if ((2.0 * Ce) <= 0.2)
		tmp = 0.5 - Ce;
	else
		tmp = 0.0;
	end
	tmp_2 = tmp;
end
code[Ce_] := If[LessEqual[N[(2.0 * Ce), $MachinePrecision], 0.2], N[(0.5 - Ce), $MachinePrecision], 0.0]
f(Ce):
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Ce: real): real =
	LET tmp = IF (((2) * Ce) <= (200000000000000011102230246251565404236316680908203125e-54)) THEN ((5e-1) - Ce) ELSE (0) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;2 \cdot Ce \leq 0.2:\\
\;\;\;\;0.5 - Ce\\

\mathbf{else}:\\
\;\;\;\;0\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 #s(literal 2 binary64) Ce) < 0.20000000000000001

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} + -1 \cdot Ce \]
    3. Applied rewrites66.7%

      \[\leadsto 0.5 + -1 \cdot Ce \]
    4. Applied rewrites66.7%

      \[\leadsto 0.5 - Ce \]

    if 0.20000000000000001 < (*.f64 #s(literal 2 binary64) Ce)

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} \]
    3. Applied rewrites66.2%

      \[\leadsto 0.5 \]
    4. Taylor expanded in undef-var around zero

      \[\leadsto 0 \]
    5. Applied rewrites34.3%

      \[\leadsto 0 \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 97.4% accurate, 2.4× speedup?

\[\begin{array}{l} \mathbf{if}\;2 \cdot Ce \leq 169.95083588869068:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
(FPCore (Ce)
  :precision binary64
  :pre TRUE
  (if (<= (* 2.0 Ce) 169.95083588869068) 0.5 0.0))
double code(double Ce) {
	double tmp;
	if ((2.0 * Ce) <= 169.95083588869068) {
		tmp = 0.5;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
real(8) function code(ce)
use fmin_fmax_functions
    real(8), intent (in) :: ce
    real(8) :: tmp
    if ((2.0d0 * ce) <= 169.95083588869068d0) then
        tmp = 0.5d0
    else
        tmp = 0.0d0
    end if
    code = tmp
end function
public static double code(double Ce) {
	double tmp;
	if ((2.0 * Ce) <= 169.95083588869068) {
		tmp = 0.5;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
def code(Ce):
	tmp = 0
	if (2.0 * Ce) <= 169.95083588869068:
		tmp = 0.5
	else:
		tmp = 0.0
	return tmp
function code(Ce)
	tmp = 0.0
	if (Float64(2.0 * Ce) <= 169.95083588869068)
		tmp = 0.5;
	else
		tmp = 0.0;
	end
	return tmp
end
function tmp_2 = code(Ce)
	tmp = 0.0;
	if ((2.0 * Ce) <= 169.95083588869068)
		tmp = 0.5;
	else
		tmp = 0.0;
	end
	tmp_2 = tmp;
end
code[Ce_] := If[LessEqual[N[(2.0 * Ce), $MachinePrecision], 169.95083588869068], 0.5, 0.0]
f(Ce):
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Ce: real): real =
	LET tmp = IF (((2) * Ce) <= (1699508358886906762563739903271198272705078125e-43)) THEN (5e-1) ELSE (0) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;2 \cdot Ce \leq 169.95083588869068:\\
\;\;\;\;0.5\\

\mathbf{else}:\\
\;\;\;\;0\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 #s(literal 2 binary64) Ce) < 169.95083588869068

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} \]
    3. Applied rewrites66.2%

      \[\leadsto 0.5 \]

    if 169.95083588869068 < (*.f64 #s(literal 2 binary64) Ce)

    1. Initial program 100.0%

      \[\frac{0.5}{e^{2 \cdot Ce}} \]
    2. Taylor expanded in Ce around 0

      \[\leadsto \frac{1}{2} \]
    3. Applied rewrites66.2%

      \[\leadsto 0.5 \]
    4. Taylor expanded in undef-var around zero

      \[\leadsto 0 \]
    5. Applied rewrites34.3%

      \[\leadsto 0 \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 66.2% accurate, 18.8× speedup?

\[0.5 \]
(FPCore (Ce)
  :precision binary64
  :pre TRUE
  0.5)
double code(double Ce) {
	return 0.5;
}
real(8) function code(ce)
use fmin_fmax_functions
    real(8), intent (in) :: ce
    code = 0.5d0
end function
public static double code(double Ce) {
	return 0.5;
}
def code(Ce):
	return 0.5
function code(Ce)
	return 0.5
end
function tmp = code(Ce)
	tmp = 0.5;
end
code[Ce_] := 0.5
f(Ce):
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Ce: real): real =
	5e-1
END code
0.5
Derivation
  1. Initial program 100.0%

    \[\frac{0.5}{e^{2 \cdot Ce}} \]
  2. Taylor expanded in Ce around 0

    \[\leadsto \frac{1}{2} \]
  3. Applied rewrites66.2%

    \[\leadsto 0.5 \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2026050 +o generate:egglog
(FPCore (Ce)
  :name "exact-inv-half-inv-exp"
  :precision binary64
  (/ 0.5 (exp (* 2.0 Ce))))