1/(1+exp(-x))

Percentage Accurate: 100.0% → 100.0%
Time: 1.8s
Alternatives: 8
Speedup: 1.0×

Specification

?
\[-1.79 \cdot 10^{+308} \leq x \land x \leq 1.79 \cdot 10^{+308}\]
\[\frac{1}{1 + e^{-x}} \]
(FPCore (x)
  :precision binary64
  (/ 1.0 (+ 1.0 (exp (- x)))))
double code(double x) {
	return 1.0 / (1.0 + exp(-x));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.0d0 / (1.0d0 + exp(-x))
end function
public static double code(double x) {
	return 1.0 / (1.0 + Math.exp(-x));
}
def code(x):
	return 1.0 / (1.0 + math.exp(-x))
function code(x)
	return Float64(1.0 / Float64(1.0 + exp(Float64(-x))))
end
function tmp = code(x)
	tmp = 1.0 / (1.0 + exp(-x));
end
code[x_] := N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{1}{1 + e^{-x}}

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 8 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{1}{1 + e^{-x}} \]
(FPCore (x)
  :precision binary64
  (/ 1.0 (+ 1.0 (exp (- x)))))
double code(double x) {
	return 1.0 / (1.0 + exp(-x));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.0d0 / (1.0d0 + exp(-x))
end function
public static double code(double x) {
	return 1.0 / (1.0 + Math.exp(-x));
}
def code(x):
	return 1.0 / (1.0 + math.exp(-x))
function code(x)
	return Float64(1.0 / Float64(1.0 + exp(Float64(-x))))
end
function tmp = code(x)
	tmp = 1.0 / (1.0 + exp(-x));
end
code[x_] := N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{1}{1 + e^{-x}}

Alternative 1: 98.9% accurate, 1.0× speedup?

\[\begin{array}{l} \mathbf{if}\;x \leq -0.65:\\ \;\;\;\;\frac{e^{x}}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{1}{x - -1} - -1}\\ \end{array} \]
(FPCore (x)
  :precision binary64
  (if (<= x -0.65) (/ (exp x) 2.0) (/ 1.0 (- (/ 1.0 (- x -1.0)) -1.0))))
double code(double x) {
	double tmp;
	if (x <= -0.65) {
		tmp = exp(x) / 2.0;
	} else {
		tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-0.65d0)) then
        tmp = exp(x) / 2.0d0
    else
        tmp = 1.0d0 / ((1.0d0 / (x - (-1.0d0))) - (-1.0d0))
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= -0.65) {
		tmp = Math.exp(x) / 2.0;
	} else {
		tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= -0.65:
		tmp = math.exp(x) / 2.0
	else:
		tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -0.65)
		tmp = Float64(exp(x) / 2.0);
	else
		tmp = Float64(1.0 / Float64(Float64(1.0 / Float64(x - -1.0)) - -1.0));
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -0.65)
		tmp = exp(x) / 2.0;
	else
		tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0);
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, -0.65], N[(N[Exp[x], $MachinePrecision] / 2.0), $MachinePrecision], N[(1.0 / N[(N[(1.0 / N[(x - -1.0), $MachinePrecision]), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;x \leq -0.65:\\
\;\;\;\;\frac{e^{x}}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{1}{x - -1} - -1}\\


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

    1. Initial program 100.0%

      \[\frac{1}{1 + e^{-x}} \]
    2. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{1}{1 + e^{-x}}} \]
      2. lift-+.f64N/A

        \[\leadsto \frac{1}{\color{blue}{1 + e^{-x}}} \]
      3. lift-exp.f64N/A

        \[\leadsto \frac{1}{1 + \color{blue}{e^{-x}}} \]
      4. lift-neg.f64N/A

        \[\leadsto \frac{1}{1 + e^{\color{blue}{\mathsf{neg}\left(x\right)}}} \]
      5. exp-negN/A

        \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{x}}}} \]
      6. add-to-fractionN/A

        \[\leadsto \frac{1}{\color{blue}{\frac{1 \cdot e^{x} + 1}{e^{x}}}} \]
      7. div-flip-revN/A

        \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
      8. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
      9. lower-exp.f64N/A

        \[\leadsto \frac{\color{blue}{e^{x}}}{1 \cdot e^{x} + 1} \]
      10. add-flipN/A

        \[\leadsto \frac{e^{x}}{\color{blue}{1 \cdot e^{x} - \left(\mathsf{neg}\left(1\right)\right)}} \]
      11. metadata-evalN/A

        \[\leadsto \frac{e^{x}}{1 \cdot e^{x} - \color{blue}{-1}} \]
      12. *-lft-identityN/A

        \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
      13. lower--.f64N/A

        \[\leadsto \frac{e^{x}}{\color{blue}{e^{x} - -1}} \]
      14. lower-exp.f6475.6%

        \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
    3. Applied rewrites75.6%

      \[\leadsto \color{blue}{\frac{e^{x}}{e^{x} - -1}} \]
    4. Taylor expanded in x around 0

      \[\leadsto \frac{e^{x}}{\color{blue}{2}} \]
    5. Step-by-step derivation
      1. Applied rewrites75.0%

        \[\leadsto \frac{e^{x}}{\color{blue}{2}} \]

      if -0.65000000000000002 < x

      1. Initial program 100.0%

        \[\frac{1}{1 + e^{-x}} \]
      2. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{1}{1 + e^{-x}}} \]
        2. lift-+.f64N/A

          \[\leadsto \frac{1}{\color{blue}{1 + e^{-x}}} \]
        3. lift-exp.f64N/A

          \[\leadsto \frac{1}{1 + \color{blue}{e^{-x}}} \]
        4. lift-neg.f64N/A

          \[\leadsto \frac{1}{1 + e^{\color{blue}{\mathsf{neg}\left(x\right)}}} \]
        5. exp-negN/A

          \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{x}}}} \]
        6. add-to-fractionN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1 \cdot e^{x} + 1}{e^{x}}}} \]
        7. div-flip-revN/A

          \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
        8. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
        9. lower-exp.f64N/A

          \[\leadsto \frac{\color{blue}{e^{x}}}{1 \cdot e^{x} + 1} \]
        10. add-flipN/A

          \[\leadsto \frac{e^{x}}{\color{blue}{1 \cdot e^{x} - \left(\mathsf{neg}\left(1\right)\right)}} \]
        11. metadata-evalN/A

          \[\leadsto \frac{e^{x}}{1 \cdot e^{x} - \color{blue}{-1}} \]
        12. *-lft-identityN/A

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
        13. lower--.f64N/A

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x} - -1}} \]
        14. lower-exp.f6475.6%

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
      3. Applied rewrites75.6%

        \[\leadsto \color{blue}{\frac{e^{x}}{e^{x} - -1}} \]
      4. Taylor expanded in x around 0

        \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
      5. Step-by-step derivation
        1. lower-+.f6451.8%

          \[\leadsto \frac{1 + \color{blue}{x}}{e^{x} - -1} \]
      6. Applied rewrites51.8%

        \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
      7. Taylor expanded in x around 0

        \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
      8. Step-by-step derivation
        1. lower-+.f6475.4%

          \[\leadsto \frac{1 + x}{\left(1 + \color{blue}{x}\right) - -1} \]
      9. Applied rewrites75.4%

        \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
      10. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{1 + x}{\left(1 + x\right) - -1}} \]
        2. div-flipN/A

          \[\leadsto \color{blue}{\frac{1}{\frac{\left(1 + x\right) - -1}{1 + x}}} \]
        3. lower-unsound-/.f64N/A

          \[\leadsto \color{blue}{\frac{1}{\frac{\left(1 + x\right) - -1}{1 + x}}} \]
        4. lower-unsound-/.f6475.4%

          \[\leadsto \frac{1}{\color{blue}{\frac{\left(1 + x\right) - -1}{1 + x}}} \]
        5. lift-+.f64N/A

          \[\leadsto \frac{1}{\frac{\left(1 + \color{blue}{x}\right) - -1}{1 + x}} \]
        6. +-commutativeN/A

          \[\leadsto \frac{1}{\frac{\left(x + \color{blue}{1}\right) - -1}{1 + x}} \]
        7. add-flipN/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{\left(\mathsf{neg}\left(1\right)\right)}\right) - -1}{1 + x}} \]
        8. metadata-evalN/A

          \[\leadsto \frac{1}{\frac{\left(x - -1\right) - -1}{1 + x}} \]
        9. lower--.f6475.4%

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{1 + x}} \]
        10. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(lift-+.f64, \left(1 + x\right)\right)}} \]
        11. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(+-commutative, \left(x + 1\right)\right)}} \]
        12. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(add-flip, \left(x - \left(\mathsf{neg}\left(1\right)\right)\right)\right)}} \]
        13. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{x - \mathsf{Rewrite=>}\left(metadata-eval, -1\right)}} \]
        14. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(lower--.f64, \left(x - -1\right)\right)}} \]
      11. Applied rewrites75.4%

        \[\leadsto \color{blue}{\frac{1}{\frac{\left(x - -1\right) - -1}{x - -1}}} \]
      12. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \frac{1}{\color{blue}{\frac{\left(x - -1\right) - -1}{x - -1}}} \]
        2. frac-2negN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{neg}\left(\left(\left(x - -1\right) - -1\right)\right)}{\mathsf{neg}\left(\left(x - -1\right)\right)}}} \]
        3. lift--.f64N/A

          \[\leadsto \frac{1}{\frac{\mathsf{neg}\left(\color{blue}{\left(\left(x - -1\right) - -1\right)}\right)}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        4. sub-negate-revN/A

          \[\leadsto \frac{1}{\frac{\color{blue}{-1 - \left(x - -1\right)}}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        5. div-subN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{-1}{\mathsf{neg}\left(\left(x - -1\right)\right)} - \frac{x - -1}{\mathsf{neg}\left(\left(x - -1\right)\right)}}} \]
        6. metadata-evalN/A

          \[\leadsto \frac{1}{\frac{\color{blue}{\mathsf{neg}\left(1\right)}}{\mathsf{neg}\left(\left(x - -1\right)\right)} - \frac{x - -1}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        7. frac-2negN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1}} - \frac{x - -1}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        8. distribute-neg-frac2N/A

          \[\leadsto \frac{1}{\frac{1}{x - -1} - \color{blue}{\left(\mathsf{neg}\left(\frac{x - -1}{x - -1}\right)\right)}} \]
        9. *-inversesN/A

          \[\leadsto \frac{1}{\frac{1}{x - -1} - \left(\mathsf{neg}\left(\color{blue}{1}\right)\right)} \]
        10. metadata-evalN/A

          \[\leadsto \frac{1}{\frac{1}{x - -1} - \color{blue}{-1}} \]
        11. lower--.f64N/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1} - -1}} \]
        12. lower-/.f6475.4%

          \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1}} - -1} \]
      13. Applied rewrites75.4%

        \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1} - -1}} \]
    6. Recombined 2 regimes into one program.
    7. Add Preprocessing

    Alternative 2: 87.5% accurate, 0.5× speedup?

    \[\begin{array}{l} \mathbf{if}\;\frac{1}{1 + e^{-x}} \leq 0.5526041551930219:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 2\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{1}{x - -1} - -1}\\ \end{array} \]
    (FPCore (x)
      :precision binary64
      (if (<= (/ 1.0 (+ 1.0 (exp (- x)))) 0.5526041551930219)
      (/ 1.0 (fma (fma 0.5 x -1.0) x 2.0))
      (/ 1.0 (- (/ 1.0 (- x -1.0)) -1.0))))
    double code(double x) {
    	double tmp;
    	if ((1.0 / (1.0 + exp(-x))) <= 0.5526041551930219) {
    		tmp = 1.0 / fma(fma(0.5, x, -1.0), x, 2.0);
    	} else {
    		tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0);
    	}
    	return tmp;
    }
    
    function code(x)
    	tmp = 0.0
    	if (Float64(1.0 / Float64(1.0 + exp(Float64(-x)))) <= 0.5526041551930219)
    		tmp = Float64(1.0 / fma(fma(0.5, x, -1.0), x, 2.0));
    	else
    		tmp = Float64(1.0 / Float64(Float64(1.0 / Float64(x - -1.0)) - -1.0));
    	end
    	return tmp
    end
    
    code[x_] := If[LessEqual[N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5526041551930219], N[(1.0 / N[(N[(0.5 * x + -1.0), $MachinePrecision] * x + 2.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(1.0 / N[(x - -1.0), $MachinePrecision]), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    \mathbf{if}\;\frac{1}{1 + e^{-x}} \leq 0.5526041551930219:\\
    \;\;\;\;\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 2\right)}\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{1}{\frac{1}{x - -1} - -1}\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x)))) < 0.55260415519302186

      1. Initial program 100.0%

        \[\frac{1}{1 + e^{-x}} \]
      2. Taylor expanded in x around 0

        \[\leadsto \frac{1}{\color{blue}{2 + x \cdot \left(\frac{1}{2} \cdot x - 1\right)}} \]
      3. Step-by-step derivation
        1. lower-+.f64N/A

          \[\leadsto \frac{1}{2 + \color{blue}{x \cdot \left(\frac{1}{2} \cdot x - 1\right)}} \]
        2. lower-*.f64N/A

          \[\leadsto \frac{1}{2 + x \cdot \color{blue}{\left(\frac{1}{2} \cdot x - 1\right)}} \]
        3. lower--.f64N/A

          \[\leadsto \frac{1}{2 + x \cdot \left(\frac{1}{2} \cdot x - \color{blue}{1}\right)} \]
        4. lower-*.f6464.4%

          \[\leadsto \frac{1}{2 + x \cdot \left(0.5 \cdot x - 1\right)} \]
      4. Applied rewrites64.4%

        \[\leadsto \frac{1}{\color{blue}{2 + x \cdot \left(0.5 \cdot x - 1\right)}} \]
      5. Step-by-step derivation
        1. lift-+.f64N/A

          \[\leadsto \frac{1}{2 + \color{blue}{x \cdot \left(\frac{1}{2} \cdot x - 1\right)}} \]
        2. +-commutativeN/A

          \[\leadsto \frac{1}{x \cdot \left(\frac{1}{2} \cdot x - 1\right) + \color{blue}{2}} \]
        3. lift-*.f64N/A

          \[\leadsto \frac{1}{x \cdot \left(\frac{1}{2} \cdot x - 1\right) + 2} \]
        4. *-commutativeN/A

          \[\leadsto \frac{1}{\left(\frac{1}{2} \cdot x - 1\right) \cdot x + 2} \]
        5. lower-fma.f6464.4%

          \[\leadsto \frac{1}{\mathsf{fma}\left(0.5 \cdot x - 1, \color{blue}{x}, 2\right)} \]
        6. lift--.f64N/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x - 1, x, 2\right)} \]
        7. sub-flipN/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x + \left(\mathsf{neg}\left(1\right)\right), x, 2\right)} \]
        8. lift-*.f64N/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x + \left(\mathsf{neg}\left(1\right)\right), x, 2\right)} \]
        9. metadata-evalN/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x + -1, x, 2\right)} \]
        10. lower-fma.f6464.4%

          \[\leadsto \frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 2\right)} \]
      6. Applied rewrites64.4%

        \[\leadsto \frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), \color{blue}{x}, 2\right)} \]

      if 0.55260415519302186 < (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x))))

      1. Initial program 100.0%

        \[\frac{1}{1 + e^{-x}} \]
      2. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{1}{1 + e^{-x}}} \]
        2. lift-+.f64N/A

          \[\leadsto \frac{1}{\color{blue}{1 + e^{-x}}} \]
        3. lift-exp.f64N/A

          \[\leadsto \frac{1}{1 + \color{blue}{e^{-x}}} \]
        4. lift-neg.f64N/A

          \[\leadsto \frac{1}{1 + e^{\color{blue}{\mathsf{neg}\left(x\right)}}} \]
        5. exp-negN/A

          \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{x}}}} \]
        6. add-to-fractionN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1 \cdot e^{x} + 1}{e^{x}}}} \]
        7. div-flip-revN/A

          \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
        8. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
        9. lower-exp.f64N/A

          \[\leadsto \frac{\color{blue}{e^{x}}}{1 \cdot e^{x} + 1} \]
        10. add-flipN/A

          \[\leadsto \frac{e^{x}}{\color{blue}{1 \cdot e^{x} - \left(\mathsf{neg}\left(1\right)\right)}} \]
        11. metadata-evalN/A

          \[\leadsto \frac{e^{x}}{1 \cdot e^{x} - \color{blue}{-1}} \]
        12. *-lft-identityN/A

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
        13. lower--.f64N/A

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x} - -1}} \]
        14. lower-exp.f6475.6%

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
      3. Applied rewrites75.6%

        \[\leadsto \color{blue}{\frac{e^{x}}{e^{x} - -1}} \]
      4. Taylor expanded in x around 0

        \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
      5. Step-by-step derivation
        1. lower-+.f6451.8%

          \[\leadsto \frac{1 + \color{blue}{x}}{e^{x} - -1} \]
      6. Applied rewrites51.8%

        \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
      7. Taylor expanded in x around 0

        \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
      8. Step-by-step derivation
        1. lower-+.f6475.4%

          \[\leadsto \frac{1 + x}{\left(1 + \color{blue}{x}\right) - -1} \]
      9. Applied rewrites75.4%

        \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
      10. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{1 + x}{\left(1 + x\right) - -1}} \]
        2. div-flipN/A

          \[\leadsto \color{blue}{\frac{1}{\frac{\left(1 + x\right) - -1}{1 + x}}} \]
        3. lower-unsound-/.f64N/A

          \[\leadsto \color{blue}{\frac{1}{\frac{\left(1 + x\right) - -1}{1 + x}}} \]
        4. lower-unsound-/.f6475.4%

          \[\leadsto \frac{1}{\color{blue}{\frac{\left(1 + x\right) - -1}{1 + x}}} \]
        5. lift-+.f64N/A

          \[\leadsto \frac{1}{\frac{\left(1 + \color{blue}{x}\right) - -1}{1 + x}} \]
        6. +-commutativeN/A

          \[\leadsto \frac{1}{\frac{\left(x + \color{blue}{1}\right) - -1}{1 + x}} \]
        7. add-flipN/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{\left(\mathsf{neg}\left(1\right)\right)}\right) - -1}{1 + x}} \]
        8. metadata-evalN/A

          \[\leadsto \frac{1}{\frac{\left(x - -1\right) - -1}{1 + x}} \]
        9. lower--.f6475.4%

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{1 + x}} \]
        10. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(lift-+.f64, \left(1 + x\right)\right)}} \]
        11. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(+-commutative, \left(x + 1\right)\right)}} \]
        12. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(add-flip, \left(x - \left(\mathsf{neg}\left(1\right)\right)\right)\right)}} \]
        13. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{x - \mathsf{Rewrite=>}\left(metadata-eval, -1\right)}} \]
        14. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(lower--.f64, \left(x - -1\right)\right)}} \]
      11. Applied rewrites75.4%

        \[\leadsto \color{blue}{\frac{1}{\frac{\left(x - -1\right) - -1}{x - -1}}} \]
      12. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \frac{1}{\color{blue}{\frac{\left(x - -1\right) - -1}{x - -1}}} \]
        2. frac-2negN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{neg}\left(\left(\left(x - -1\right) - -1\right)\right)}{\mathsf{neg}\left(\left(x - -1\right)\right)}}} \]
        3. lift--.f64N/A

          \[\leadsto \frac{1}{\frac{\mathsf{neg}\left(\color{blue}{\left(\left(x - -1\right) - -1\right)}\right)}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        4. sub-negate-revN/A

          \[\leadsto \frac{1}{\frac{\color{blue}{-1 - \left(x - -1\right)}}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        5. div-subN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{-1}{\mathsf{neg}\left(\left(x - -1\right)\right)} - \frac{x - -1}{\mathsf{neg}\left(\left(x - -1\right)\right)}}} \]
        6. metadata-evalN/A

          \[\leadsto \frac{1}{\frac{\color{blue}{\mathsf{neg}\left(1\right)}}{\mathsf{neg}\left(\left(x - -1\right)\right)} - \frac{x - -1}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        7. frac-2negN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1}} - \frac{x - -1}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        8. distribute-neg-frac2N/A

          \[\leadsto \frac{1}{\frac{1}{x - -1} - \color{blue}{\left(\mathsf{neg}\left(\frac{x - -1}{x - -1}\right)\right)}} \]
        9. *-inversesN/A

          \[\leadsto \frac{1}{\frac{1}{x - -1} - \left(\mathsf{neg}\left(\color{blue}{1}\right)\right)} \]
        10. metadata-evalN/A

          \[\leadsto \frac{1}{\frac{1}{x - -1} - \color{blue}{-1}} \]
        11. lower--.f64N/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1} - -1}} \]
        12. lower-/.f6475.4%

          \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1}} - -1} \]
      13. Applied rewrites75.4%

        \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1} - -1}} \]
    3. Recombined 2 regimes into one program.
    4. Add Preprocessing

    Alternative 3: 87.4% accurate, 0.5× speedup?

    \[\begin{array}{l} \mathbf{if}\;\frac{1}{1 + e^{-x}} \leq 10^{-5}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{1}{x - -1} - -1}\\ \end{array} \]
    (FPCore (x)
      :precision binary64
      (if (<= (/ 1.0 (+ 1.0 (exp (- x)))) 1e-5)
      (/ 1.0 (fma (* 0.5 x) x 2.0))
      (/ 1.0 (- (/ 1.0 (- x -1.0)) -1.0))))
    double code(double x) {
    	double tmp;
    	if ((1.0 / (1.0 + exp(-x))) <= 1e-5) {
    		tmp = 1.0 / fma((0.5 * x), x, 2.0);
    	} else {
    		tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0);
    	}
    	return tmp;
    }
    
    function code(x)
    	tmp = 0.0
    	if (Float64(1.0 / Float64(1.0 + exp(Float64(-x)))) <= 1e-5)
    		tmp = Float64(1.0 / fma(Float64(0.5 * x), x, 2.0));
    	else
    		tmp = Float64(1.0 / Float64(Float64(1.0 / Float64(x - -1.0)) - -1.0));
    	end
    	return tmp
    end
    
    code[x_] := If[LessEqual[N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1e-5], N[(1.0 / N[(N[(0.5 * x), $MachinePrecision] * x + 2.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(1.0 / N[(x - -1.0), $MachinePrecision]), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    \mathbf{if}\;\frac{1}{1 + e^{-x}} \leq 10^{-5}:\\
    \;\;\;\;\frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)}\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{1}{\frac{1}{x - -1} - -1}\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x)))) < 1.0000000000000001e-5

      1. Initial program 100.0%

        \[\frac{1}{1 + e^{-x}} \]
      2. Taylor expanded in x around 0

        \[\leadsto \frac{1}{\color{blue}{2 + x \cdot \left(\frac{1}{2} \cdot x - 1\right)}} \]
      3. Step-by-step derivation
        1. lower-+.f64N/A

          \[\leadsto \frac{1}{2 + \color{blue}{x \cdot \left(\frac{1}{2} \cdot x - 1\right)}} \]
        2. lower-*.f64N/A

          \[\leadsto \frac{1}{2 + x \cdot \color{blue}{\left(\frac{1}{2} \cdot x - 1\right)}} \]
        3. lower--.f64N/A

          \[\leadsto \frac{1}{2 + x \cdot \left(\frac{1}{2} \cdot x - \color{blue}{1}\right)} \]
        4. lower-*.f6464.4%

          \[\leadsto \frac{1}{2 + x \cdot \left(0.5 \cdot x - 1\right)} \]
      4. Applied rewrites64.4%

        \[\leadsto \frac{1}{\color{blue}{2 + x \cdot \left(0.5 \cdot x - 1\right)}} \]
      5. Step-by-step derivation
        1. lift-+.f64N/A

          \[\leadsto \frac{1}{2 + \color{blue}{x \cdot \left(\frac{1}{2} \cdot x - 1\right)}} \]
        2. +-commutativeN/A

          \[\leadsto \frac{1}{x \cdot \left(\frac{1}{2} \cdot x - 1\right) + \color{blue}{2}} \]
        3. lift-*.f64N/A

          \[\leadsto \frac{1}{x \cdot \left(\frac{1}{2} \cdot x - 1\right) + 2} \]
        4. *-commutativeN/A

          \[\leadsto \frac{1}{\left(\frac{1}{2} \cdot x - 1\right) \cdot x + 2} \]
        5. lower-fma.f6464.4%

          \[\leadsto \frac{1}{\mathsf{fma}\left(0.5 \cdot x - 1, \color{blue}{x}, 2\right)} \]
        6. lift--.f64N/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x - 1, x, 2\right)} \]
        7. sub-flipN/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x + \left(\mathsf{neg}\left(1\right)\right), x, 2\right)} \]
        8. lift-*.f64N/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x + \left(\mathsf{neg}\left(1\right)\right), x, 2\right)} \]
        9. metadata-evalN/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x + -1, x, 2\right)} \]
        10. lower-fma.f6464.4%

          \[\leadsto \frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 2\right)} \]
      6. Applied rewrites64.4%

        \[\leadsto \frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), \color{blue}{x}, 2\right)} \]
      7. Taylor expanded in x around inf

        \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x, x, 2\right)} \]
      8. Step-by-step derivation
        1. lower-*.f6463.8%

          \[\leadsto \frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)} \]
      9. Applied rewrites63.8%

        \[\leadsto \frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)} \]

      if 1.0000000000000001e-5 < (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x))))

      1. Initial program 100.0%

        \[\frac{1}{1 + e^{-x}} \]
      2. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{1}{1 + e^{-x}}} \]
        2. lift-+.f64N/A

          \[\leadsto \frac{1}{\color{blue}{1 + e^{-x}}} \]
        3. lift-exp.f64N/A

          \[\leadsto \frac{1}{1 + \color{blue}{e^{-x}}} \]
        4. lift-neg.f64N/A

          \[\leadsto \frac{1}{1 + e^{\color{blue}{\mathsf{neg}\left(x\right)}}} \]
        5. exp-negN/A

          \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{x}}}} \]
        6. add-to-fractionN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1 \cdot e^{x} + 1}{e^{x}}}} \]
        7. div-flip-revN/A

          \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
        8. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
        9. lower-exp.f64N/A

          \[\leadsto \frac{\color{blue}{e^{x}}}{1 \cdot e^{x} + 1} \]
        10. add-flipN/A

          \[\leadsto \frac{e^{x}}{\color{blue}{1 \cdot e^{x} - \left(\mathsf{neg}\left(1\right)\right)}} \]
        11. metadata-evalN/A

          \[\leadsto \frac{e^{x}}{1 \cdot e^{x} - \color{blue}{-1}} \]
        12. *-lft-identityN/A

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
        13. lower--.f64N/A

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x} - -1}} \]
        14. lower-exp.f6475.6%

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
      3. Applied rewrites75.6%

        \[\leadsto \color{blue}{\frac{e^{x}}{e^{x} - -1}} \]
      4. Taylor expanded in x around 0

        \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
      5. Step-by-step derivation
        1. lower-+.f6451.8%

          \[\leadsto \frac{1 + \color{blue}{x}}{e^{x} - -1} \]
      6. Applied rewrites51.8%

        \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
      7. Taylor expanded in x around 0

        \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
      8. Step-by-step derivation
        1. lower-+.f6475.4%

          \[\leadsto \frac{1 + x}{\left(1 + \color{blue}{x}\right) - -1} \]
      9. Applied rewrites75.4%

        \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
      10. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{1 + x}{\left(1 + x\right) - -1}} \]
        2. div-flipN/A

          \[\leadsto \color{blue}{\frac{1}{\frac{\left(1 + x\right) - -1}{1 + x}}} \]
        3. lower-unsound-/.f64N/A

          \[\leadsto \color{blue}{\frac{1}{\frac{\left(1 + x\right) - -1}{1 + x}}} \]
        4. lower-unsound-/.f6475.4%

          \[\leadsto \frac{1}{\color{blue}{\frac{\left(1 + x\right) - -1}{1 + x}}} \]
        5. lift-+.f64N/A

          \[\leadsto \frac{1}{\frac{\left(1 + \color{blue}{x}\right) - -1}{1 + x}} \]
        6. +-commutativeN/A

          \[\leadsto \frac{1}{\frac{\left(x + \color{blue}{1}\right) - -1}{1 + x}} \]
        7. add-flipN/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{\left(\mathsf{neg}\left(1\right)\right)}\right) - -1}{1 + x}} \]
        8. metadata-evalN/A

          \[\leadsto \frac{1}{\frac{\left(x - -1\right) - -1}{1 + x}} \]
        9. lower--.f6475.4%

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{1 + x}} \]
        10. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(lift-+.f64, \left(1 + x\right)\right)}} \]
        11. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(+-commutative, \left(x + 1\right)\right)}} \]
        12. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(add-flip, \left(x - \left(\mathsf{neg}\left(1\right)\right)\right)\right)}} \]
        13. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{x - \mathsf{Rewrite=>}\left(metadata-eval, -1\right)}} \]
        14. lower--.f64N/A

          \[\leadsto \frac{1}{\frac{\left(x - \color{blue}{-1}\right) - -1}{\mathsf{Rewrite=>}\left(lower--.f64, \left(x - -1\right)\right)}} \]
      11. Applied rewrites75.4%

        \[\leadsto \color{blue}{\frac{1}{\frac{\left(x - -1\right) - -1}{x - -1}}} \]
      12. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \frac{1}{\color{blue}{\frac{\left(x - -1\right) - -1}{x - -1}}} \]
        2. frac-2negN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{neg}\left(\left(\left(x - -1\right) - -1\right)\right)}{\mathsf{neg}\left(\left(x - -1\right)\right)}}} \]
        3. lift--.f64N/A

          \[\leadsto \frac{1}{\frac{\mathsf{neg}\left(\color{blue}{\left(\left(x - -1\right) - -1\right)}\right)}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        4. sub-negate-revN/A

          \[\leadsto \frac{1}{\frac{\color{blue}{-1 - \left(x - -1\right)}}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        5. div-subN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{-1}{\mathsf{neg}\left(\left(x - -1\right)\right)} - \frac{x - -1}{\mathsf{neg}\left(\left(x - -1\right)\right)}}} \]
        6. metadata-evalN/A

          \[\leadsto \frac{1}{\frac{\color{blue}{\mathsf{neg}\left(1\right)}}{\mathsf{neg}\left(\left(x - -1\right)\right)} - \frac{x - -1}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        7. frac-2negN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1}} - \frac{x - -1}{\mathsf{neg}\left(\left(x - -1\right)\right)}} \]
        8. distribute-neg-frac2N/A

          \[\leadsto \frac{1}{\frac{1}{x - -1} - \color{blue}{\left(\mathsf{neg}\left(\frac{x - -1}{x - -1}\right)\right)}} \]
        9. *-inversesN/A

          \[\leadsto \frac{1}{\frac{1}{x - -1} - \left(\mathsf{neg}\left(\color{blue}{1}\right)\right)} \]
        10. metadata-evalN/A

          \[\leadsto \frac{1}{\frac{1}{x - -1} - \color{blue}{-1}} \]
        11. lower--.f64N/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1} - -1}} \]
        12. lower-/.f6475.4%

          \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1}} - -1} \]
      13. Applied rewrites75.4%

        \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - -1} - -1}} \]
    3. Recombined 2 regimes into one program.
    4. Add Preprocessing

    Alternative 4: 87.4% accurate, 0.6× speedup?

    \[\begin{array}{l} \mathbf{if}\;\frac{1}{1 + e^{-x}} \leq 10^{-5}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1 + x}{\left(1 + x\right) - -1}\\ \end{array} \]
    (FPCore (x)
      :precision binary64
      (if (<= (/ 1.0 (+ 1.0 (exp (- x)))) 1e-5)
      (/ 1.0 (fma (* 0.5 x) x 2.0))
      (/ (+ 1.0 x) (- (+ 1.0 x) -1.0))))
    double code(double x) {
    	double tmp;
    	if ((1.0 / (1.0 + exp(-x))) <= 1e-5) {
    		tmp = 1.0 / fma((0.5 * x), x, 2.0);
    	} else {
    		tmp = (1.0 + x) / ((1.0 + x) - -1.0);
    	}
    	return tmp;
    }
    
    function code(x)
    	tmp = 0.0
    	if (Float64(1.0 / Float64(1.0 + exp(Float64(-x)))) <= 1e-5)
    		tmp = Float64(1.0 / fma(Float64(0.5 * x), x, 2.0));
    	else
    		tmp = Float64(Float64(1.0 + x) / Float64(Float64(1.0 + x) - -1.0));
    	end
    	return tmp
    end
    
    code[x_] := If[LessEqual[N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1e-5], N[(1.0 / N[(N[(0.5 * x), $MachinePrecision] * x + 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 + x), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    \mathbf{if}\;\frac{1}{1 + e^{-x}} \leq 10^{-5}:\\
    \;\;\;\;\frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)}\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{1 + x}{\left(1 + x\right) - -1}\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x)))) < 1.0000000000000001e-5

      1. Initial program 100.0%

        \[\frac{1}{1 + e^{-x}} \]
      2. Taylor expanded in x around 0

        \[\leadsto \frac{1}{\color{blue}{2 + x \cdot \left(\frac{1}{2} \cdot x - 1\right)}} \]
      3. Step-by-step derivation
        1. lower-+.f64N/A

          \[\leadsto \frac{1}{2 + \color{blue}{x \cdot \left(\frac{1}{2} \cdot x - 1\right)}} \]
        2. lower-*.f64N/A

          \[\leadsto \frac{1}{2 + x \cdot \color{blue}{\left(\frac{1}{2} \cdot x - 1\right)}} \]
        3. lower--.f64N/A

          \[\leadsto \frac{1}{2 + x \cdot \left(\frac{1}{2} \cdot x - \color{blue}{1}\right)} \]
        4. lower-*.f6464.4%

          \[\leadsto \frac{1}{2 + x \cdot \left(0.5 \cdot x - 1\right)} \]
      4. Applied rewrites64.4%

        \[\leadsto \frac{1}{\color{blue}{2 + x \cdot \left(0.5 \cdot x - 1\right)}} \]
      5. Step-by-step derivation
        1. lift-+.f64N/A

          \[\leadsto \frac{1}{2 + \color{blue}{x \cdot \left(\frac{1}{2} \cdot x - 1\right)}} \]
        2. +-commutativeN/A

          \[\leadsto \frac{1}{x \cdot \left(\frac{1}{2} \cdot x - 1\right) + \color{blue}{2}} \]
        3. lift-*.f64N/A

          \[\leadsto \frac{1}{x \cdot \left(\frac{1}{2} \cdot x - 1\right) + 2} \]
        4. *-commutativeN/A

          \[\leadsto \frac{1}{\left(\frac{1}{2} \cdot x - 1\right) \cdot x + 2} \]
        5. lower-fma.f6464.4%

          \[\leadsto \frac{1}{\mathsf{fma}\left(0.5 \cdot x - 1, \color{blue}{x}, 2\right)} \]
        6. lift--.f64N/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x - 1, x, 2\right)} \]
        7. sub-flipN/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x + \left(\mathsf{neg}\left(1\right)\right), x, 2\right)} \]
        8. lift-*.f64N/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x + \left(\mathsf{neg}\left(1\right)\right), x, 2\right)} \]
        9. metadata-evalN/A

          \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x + -1, x, 2\right)} \]
        10. lower-fma.f6464.4%

          \[\leadsto \frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 2\right)} \]
      6. Applied rewrites64.4%

        \[\leadsto \frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), \color{blue}{x}, 2\right)} \]
      7. Taylor expanded in x around inf

        \[\leadsto \frac{1}{\mathsf{fma}\left(\frac{1}{2} \cdot x, x, 2\right)} \]
      8. Step-by-step derivation
        1. lower-*.f6463.8%

          \[\leadsto \frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)} \]
      9. Applied rewrites63.8%

        \[\leadsto \frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)} \]

      if 1.0000000000000001e-5 < (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x))))

      1. Initial program 100.0%

        \[\frac{1}{1 + e^{-x}} \]
      2. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{1}{1 + e^{-x}}} \]
        2. lift-+.f64N/A

          \[\leadsto \frac{1}{\color{blue}{1 + e^{-x}}} \]
        3. lift-exp.f64N/A

          \[\leadsto \frac{1}{1 + \color{blue}{e^{-x}}} \]
        4. lift-neg.f64N/A

          \[\leadsto \frac{1}{1 + e^{\color{blue}{\mathsf{neg}\left(x\right)}}} \]
        5. exp-negN/A

          \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{x}}}} \]
        6. add-to-fractionN/A

          \[\leadsto \frac{1}{\color{blue}{\frac{1 \cdot e^{x} + 1}{e^{x}}}} \]
        7. div-flip-revN/A

          \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
        8. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
        9. lower-exp.f64N/A

          \[\leadsto \frac{\color{blue}{e^{x}}}{1 \cdot e^{x} + 1} \]
        10. add-flipN/A

          \[\leadsto \frac{e^{x}}{\color{blue}{1 \cdot e^{x} - \left(\mathsf{neg}\left(1\right)\right)}} \]
        11. metadata-evalN/A

          \[\leadsto \frac{e^{x}}{1 \cdot e^{x} - \color{blue}{-1}} \]
        12. *-lft-identityN/A

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
        13. lower--.f64N/A

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x} - -1}} \]
        14. lower-exp.f6475.6%

          \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
      3. Applied rewrites75.6%

        \[\leadsto \color{blue}{\frac{e^{x}}{e^{x} - -1}} \]
      4. Taylor expanded in x around 0

        \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
      5. Step-by-step derivation
        1. lower-+.f6451.8%

          \[\leadsto \frac{1 + \color{blue}{x}}{e^{x} - -1} \]
      6. Applied rewrites51.8%

        \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
      7. Taylor expanded in x around 0

        \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
      8. Step-by-step derivation
        1. lower-+.f6475.4%

          \[\leadsto \frac{1 + x}{\left(1 + \color{blue}{x}\right) - -1} \]
      9. Applied rewrites75.4%

        \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
    3. Recombined 2 regimes into one program.
    4. Add Preprocessing

    Alternative 5: 75.4% accurate, 1.5× speedup?

    \[\frac{1 + x}{\left(1 + x\right) - -1} \]
    (FPCore (x)
      :precision binary64
      (/ (+ 1.0 x) (- (+ 1.0 x) -1.0)))
    double code(double x) {
    	return (1.0 + x) / ((1.0 + x) - -1.0);
    }
    
    real(8) function code(x)
        real(8), intent (in) :: x
        code = (1.0d0 + x) / ((1.0d0 + x) - (-1.0d0))
    end function
    
    public static double code(double x) {
    	return (1.0 + x) / ((1.0 + x) - -1.0);
    }
    
    def code(x):
    	return (1.0 + x) / ((1.0 + x) - -1.0)
    
    function code(x)
    	return Float64(Float64(1.0 + x) / Float64(Float64(1.0 + x) - -1.0))
    end
    
    function tmp = code(x)
    	tmp = (1.0 + x) / ((1.0 + x) - -1.0);
    end
    
    code[x_] := N[(N[(1.0 + x), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]
    
    \frac{1 + x}{\left(1 + x\right) - -1}
    
    Derivation
    1. Initial program 100.0%

      \[\frac{1}{1 + e^{-x}} \]
    2. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{1}{1 + e^{-x}}} \]
      2. lift-+.f64N/A

        \[\leadsto \frac{1}{\color{blue}{1 + e^{-x}}} \]
      3. lift-exp.f64N/A

        \[\leadsto \frac{1}{1 + \color{blue}{e^{-x}}} \]
      4. lift-neg.f64N/A

        \[\leadsto \frac{1}{1 + e^{\color{blue}{\mathsf{neg}\left(x\right)}}} \]
      5. exp-negN/A

        \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{x}}}} \]
      6. add-to-fractionN/A

        \[\leadsto \frac{1}{\color{blue}{\frac{1 \cdot e^{x} + 1}{e^{x}}}} \]
      7. div-flip-revN/A

        \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
      8. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
      9. lower-exp.f64N/A

        \[\leadsto \frac{\color{blue}{e^{x}}}{1 \cdot e^{x} + 1} \]
      10. add-flipN/A

        \[\leadsto \frac{e^{x}}{\color{blue}{1 \cdot e^{x} - \left(\mathsf{neg}\left(1\right)\right)}} \]
      11. metadata-evalN/A

        \[\leadsto \frac{e^{x}}{1 \cdot e^{x} - \color{blue}{-1}} \]
      12. *-lft-identityN/A

        \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
      13. lower--.f64N/A

        \[\leadsto \frac{e^{x}}{\color{blue}{e^{x} - -1}} \]
      14. lower-exp.f6475.6%

        \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
    3. Applied rewrites75.6%

      \[\leadsto \color{blue}{\frac{e^{x}}{e^{x} - -1}} \]
    4. Taylor expanded in x around 0

      \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
    5. Step-by-step derivation
      1. lower-+.f6451.8%

        \[\leadsto \frac{1 + \color{blue}{x}}{e^{x} - -1} \]
    6. Applied rewrites51.8%

      \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
    7. Taylor expanded in x around 0

      \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
    8. Step-by-step derivation
      1. lower-+.f6475.4%

        \[\leadsto \frac{1 + x}{\left(1 + \color{blue}{x}\right) - -1} \]
    9. Applied rewrites75.4%

      \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
    10. Add Preprocessing

    Alternative 6: 75.4% accurate, 1.9× speedup?

    \[\frac{1 + x}{2 + x} \]
    (FPCore (x)
      :precision binary64
      (/ (+ 1.0 x) (+ 2.0 x)))
    double code(double x) {
    	return (1.0 + x) / (2.0 + x);
    }
    
    real(8) function code(x)
        real(8), intent (in) :: x
        code = (1.0d0 + x) / (2.0d0 + x)
    end function
    
    public static double code(double x) {
    	return (1.0 + x) / (2.0 + x);
    }
    
    def code(x):
    	return (1.0 + x) / (2.0 + x)
    
    function code(x)
    	return Float64(Float64(1.0 + x) / Float64(2.0 + x))
    end
    
    function tmp = code(x)
    	tmp = (1.0 + x) / (2.0 + x);
    end
    
    code[x_] := N[(N[(1.0 + x), $MachinePrecision] / N[(2.0 + x), $MachinePrecision]), $MachinePrecision]
    
    \frac{1 + x}{2 + x}
    
    Derivation
    1. Initial program 100.0%

      \[\frac{1}{1 + e^{-x}} \]
    2. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{1}{1 + e^{-x}}} \]
      2. lift-+.f64N/A

        \[\leadsto \frac{1}{\color{blue}{1 + e^{-x}}} \]
      3. lift-exp.f64N/A

        \[\leadsto \frac{1}{1 + \color{blue}{e^{-x}}} \]
      4. lift-neg.f64N/A

        \[\leadsto \frac{1}{1 + e^{\color{blue}{\mathsf{neg}\left(x\right)}}} \]
      5. exp-negN/A

        \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{x}}}} \]
      6. add-to-fractionN/A

        \[\leadsto \frac{1}{\color{blue}{\frac{1 \cdot e^{x} + 1}{e^{x}}}} \]
      7. div-flip-revN/A

        \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
      8. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{e^{x}}{1 \cdot e^{x} + 1}} \]
      9. lower-exp.f64N/A

        \[\leadsto \frac{\color{blue}{e^{x}}}{1 \cdot e^{x} + 1} \]
      10. add-flipN/A

        \[\leadsto \frac{e^{x}}{\color{blue}{1 \cdot e^{x} - \left(\mathsf{neg}\left(1\right)\right)}} \]
      11. metadata-evalN/A

        \[\leadsto \frac{e^{x}}{1 \cdot e^{x} - \color{blue}{-1}} \]
      12. *-lft-identityN/A

        \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
      13. lower--.f64N/A

        \[\leadsto \frac{e^{x}}{\color{blue}{e^{x} - -1}} \]
      14. lower-exp.f6475.6%

        \[\leadsto \frac{e^{x}}{\color{blue}{e^{x}} - -1} \]
    3. Applied rewrites75.6%

      \[\leadsto \color{blue}{\frac{e^{x}}{e^{x} - -1}} \]
    4. Taylor expanded in x around 0

      \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
    5. Step-by-step derivation
      1. lower-+.f6451.8%

        \[\leadsto \frac{1 + \color{blue}{x}}{e^{x} - -1} \]
    6. Applied rewrites51.8%

      \[\leadsto \frac{\color{blue}{1 + x}}{e^{x} - -1} \]
    7. Taylor expanded in x around 0

      \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
    8. Step-by-step derivation
      1. lower-+.f6475.4%

        \[\leadsto \frac{1 + x}{\left(1 + \color{blue}{x}\right) - -1} \]
    9. Applied rewrites75.4%

      \[\leadsto \frac{1 + x}{\color{blue}{\left(1 + x\right)} - -1} \]
    10. Taylor expanded in x around 0

      \[\leadsto \frac{1 + x}{\color{blue}{2 + x}} \]
    11. Step-by-step derivation
      1. lower-+.f6475.4%

        \[\leadsto \frac{1 + x}{2 + \color{blue}{x}} \]
    12. Applied rewrites75.4%

      \[\leadsto \frac{1 + x}{\color{blue}{2 + x}} \]
    13. Add Preprocessing

    Alternative 7: 55.3% accurate, 19.4× speedup?

    \[0.5 \]
    (FPCore (x)
      :precision binary64
      0.5)
    double code(double x) {
    	return 0.5;
    }
    
    real(8) function code(x)
        real(8), intent (in) :: x
        code = 0.5d0
    end function
    
    public static double code(double x) {
    	return 0.5;
    }
    
    def code(x):
    	return 0.5
    
    function code(x)
    	return 0.5
    end
    
    function tmp = code(x)
    	tmp = 0.5;
    end
    
    code[x_] := 0.5
    
    0.5
    
    Derivation
    1. Initial program 100.0%

      \[\frac{1}{1 + e^{-x}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{1}{2}} \]
    3. Step-by-step derivation
      1. Applied rewrites55.3%

        \[\leadsto \color{blue}{0.5} \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 1 
      (FPCore (x)
        :name "1/(1+exp(-x))"
        :precision binary64
        :pre (and (<= -1.79e+308 x) (<= x 1.79e+308))
        (/ 1.0 (+ 1.0 (exp (- x)))))