Logistic function

Percentage Accurate: 99.8% → 99.8%
Time: 1.5s
Alternatives: 8
Speedup: 1.0×

Specification

?
\[0 \leq s \land s \leq 1.0651631\]
\[\begin{array}{l} \\ \frac{1}{1 + e^{\frac{-x}{s}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\begin{array}{l}

\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}

Sampling outcomes in binary32 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 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: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{1 + e^{\frac{-x}{s}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\begin{array}{l}

\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}

Alternative 1: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{1 + e^{\frac{-x}{s}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\begin{array}{l}

\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
Derivation
  1. Initial program 99.9%

    \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 45.7% accurate, 1.1× speedup?

\[\begin{array}{l} \\ 1 + e^{\frac{-x}{s}} \end{array} \]
(FPCore (x s) :precision binary32 (+ 1.0 (exp (/ (- x) s))))
float code(float x, float s) {
	return 1.0f + expf((-x / s));
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 + exp((-x / s))
end function
function code(x, s)
	return Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))
end
function tmp = code(x, s)
	tmp = single(1.0) + exp((-x / s));
end
\begin{array}{l}

\\
1 + e^{\frac{-x}{s}}
\end{array}
Derivation
  1. Initial program 99.9%

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

    \[\leadsto \color{blue}{\frac{1}{2} + \frac{1}{4} \cdot \frac{x}{s}} \]
  4. Applied rewrites44.2%

    \[\leadsto \color{blue}{1 + e^{\frac{-x}{s}}} \]
  5. Add Preprocessing

Alternative 3: 44.9% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.4000000059604645:\\ \;\;\;\;1 + \frac{1}{\frac{1}{-x}}\\ \mathbf{else}:\\ \;\;\;\;1 + e^{-x}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 0.4000000059604645)
   (+ 1.0 (/ 1.0 (/ 1.0 (- x))))
   (+ 1.0 (exp (- x)))))
float code(float x, float s) {
	float tmp;
	if (x <= 0.4000000059604645f) {
		tmp = 1.0f + (1.0f / (1.0f / -x));
	} else {
		tmp = 1.0f + expf(-x);
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 0.4000000059604645e0) then
        tmp = 1.0e0 + (1.0e0 / (1.0e0 / -x))
    else
        tmp = 1.0e0 + exp(-x)
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(0.4000000059604645))
		tmp = Float32(Float32(1.0) + Float32(Float32(1.0) / Float32(Float32(1.0) / Float32(-x))));
	else
		tmp = Float32(Float32(1.0) + exp(Float32(-x)));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(0.4000000059604645))
		tmp = single(1.0) + (single(1.0) / (single(1.0) / -x));
	else
		tmp = single(1.0) + exp(-x);
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.4000000059604645:\\
\;\;\;\;1 + \frac{1}{\frac{1}{-x}}\\

\mathbf{else}:\\
\;\;\;\;1 + e^{-x}\\


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

    1. Initial program 99.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} + \frac{1}{4} \cdot \frac{x}{s}} \]
    4. Applied rewrites28.2%

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

      \[\leadsto 1 + \left(1 + \color{blue}{x \cdot \left(x \cdot \left(\frac{-1}{6} \cdot \frac{x}{{s}^{3}} + \frac{1}{2} \cdot \frac{1}{{s}^{2}}\right) - \frac{1}{s}\right)}\right) \]
    6. Applied rewrites5.2%

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

      \[\leadsto 1 + \frac{1}{-1 \cdot x} \]
    8. Applied rewrites28.0%

      \[\leadsto 1 + \frac{1}{\frac{1}{-x}} \]

    if 0.400000006 < x

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\frac{1}{2} + \frac{1}{4} \cdot \frac{x}{s}} \]
    4. Applied rewrites100.0%

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

      \[\leadsto 1 + e^{-1 \cdot \frac{x}{s}} \]
    6. Applied rewrites100.0%

      \[\leadsto 1 + e^{-x} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 43.5% accurate, 3.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{-x}\\ \mathbf{if}\;x \leq 0.4000000059604645:\\ \;\;\;\;1 + \frac{1}{t\_0}\\ \mathbf{else}:\\ \;\;\;\;1 + t\_0\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (let* ((t_0 (/ 1.0 (- x))))
   (if (<= x 0.4000000059604645) (+ 1.0 (/ 1.0 t_0)) (+ 1.0 t_0))))
float code(float x, float s) {
	float t_0 = 1.0f / -x;
	float tmp;
	if (x <= 0.4000000059604645f) {
		tmp = 1.0f + (1.0f / t_0);
	} else {
		tmp = 1.0f + t_0;
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: t_0
    real(4) :: tmp
    t_0 = 1.0e0 / -x
    if (x <= 0.4000000059604645e0) then
        tmp = 1.0e0 + (1.0e0 / t_0)
    else
        tmp = 1.0e0 + t_0
    end if
    code = tmp
end function
function code(x, s)
	t_0 = Float32(Float32(1.0) / Float32(-x))
	tmp = Float32(0.0)
	if (x <= Float32(0.4000000059604645))
		tmp = Float32(Float32(1.0) + Float32(Float32(1.0) / t_0));
	else
		tmp = Float32(Float32(1.0) + t_0);
	end
	return tmp
end
function tmp_2 = code(x, s)
	t_0 = single(1.0) / -x;
	tmp = single(0.0);
	if (x <= single(0.4000000059604645))
		tmp = single(1.0) + (single(1.0) / t_0);
	else
		tmp = single(1.0) + t_0;
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{-x}\\
\mathbf{if}\;x \leq 0.4000000059604645:\\
\;\;\;\;1 + \frac{1}{t\_0}\\

\mathbf{else}:\\
\;\;\;\;1 + t\_0\\


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

    1. Initial program 99.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} + \frac{1}{4} \cdot \frac{x}{s}} \]
    4. Applied rewrites28.2%

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

      \[\leadsto 1 + \left(1 + \color{blue}{x \cdot \left(x \cdot \left(\frac{-1}{6} \cdot \frac{x}{{s}^{3}} + \frac{1}{2} \cdot \frac{1}{{s}^{2}}\right) - \frac{1}{s}\right)}\right) \]
    6. Applied rewrites5.2%

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

      \[\leadsto 1 + \frac{1}{-1 \cdot x} \]
    8. Applied rewrites28.0%

      \[\leadsto 1 + \frac{1}{\frac{1}{-x}} \]

    if 0.400000006 < x

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\frac{1}{2} + \frac{1}{4} \cdot \frac{x}{s}} \]
    4. Applied rewrites100.0%

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

      \[\leadsto 1 + \left(1 + \color{blue}{x \cdot \left(x \cdot \left(\frac{-1}{6} \cdot \frac{x}{{s}^{3}} + \frac{1}{2} \cdot \frac{1}{{s}^{2}}\right) - \frac{1}{s}\right)}\right) \]
    6. Applied rewrites96.4%

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

Alternative 5: 32.4% accurate, 5.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 1.0000000116860974 \cdot 10^{-7}:\\ \;\;\;\;1 + \frac{-x}{s}\\ \mathbf{else}:\\ \;\;\;\;1 + \frac{1}{-x}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 1.0000000116860974e-7) (+ 1.0 (/ (- x) s)) (+ 1.0 (/ 1.0 (- x)))))
float code(float x, float s) {
	float tmp;
	if (x <= 1.0000000116860974e-7f) {
		tmp = 1.0f + (-x / s);
	} else {
		tmp = 1.0f + (1.0f / -x);
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 1.0000000116860974e-7) then
        tmp = 1.0e0 + (-x / s)
    else
        tmp = 1.0e0 + (1.0e0 / -x)
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(1.0000000116860974e-7))
		tmp = Float32(Float32(1.0) + Float32(Float32(-x) / s));
	else
		tmp = Float32(Float32(1.0) + Float32(Float32(1.0) / Float32(-x)));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(1.0000000116860974e-7))
		tmp = single(1.0) + (-x / s);
	else
		tmp = single(1.0) + (single(1.0) / -x);
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.0000000116860974 \cdot 10^{-7}:\\
\;\;\;\;1 + \frac{-x}{s}\\

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


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

    1. Initial program 99.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} + \frac{1}{4} \cdot \frac{x}{s}} \]
    4. Applied rewrites23.2%

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

      \[\leadsto 1 + \left(1 + \color{blue}{x \cdot \left(\frac{1}{2} \cdot \frac{x}{{s}^{2}} - \frac{1}{s}\right)}\right) \]
    6. Applied rewrites12.2%

      \[\leadsto 1 + \frac{-x}{\color{blue}{s}} \]

    if 1.00000001e-7 < x

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\frac{1}{2} + \frac{1}{4} \cdot \frac{x}{s}} \]
    4. Applied rewrites100.0%

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

      \[\leadsto 1 + \left(1 + \color{blue}{x \cdot \left(x \cdot \left(\frac{-1}{6} \cdot \frac{x}{{s}^{3}} + \frac{1}{2} \cdot \frac{1}{{s}^{2}}\right) - \frac{1}{s}\right)}\right) \]
    6. Applied rewrites79.0%

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

Alternative 6: 9.3% accurate, 7.5× speedup?

\[\begin{array}{l} \\ 1 + \frac{-x}{s} \end{array} \]
(FPCore (x s) :precision binary32 (+ 1.0 (/ (- x) s)))
float code(float x, float s) {
	return 1.0f + (-x / s);
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 + (-x / s)
end function
function code(x, s)
	return Float32(Float32(1.0) + Float32(Float32(-x) / s))
end
function tmp = code(x, s)
	tmp = single(1.0) + (-x / s);
end
\begin{array}{l}

\\
1 + \frac{-x}{s}
\end{array}
Derivation
  1. Initial program 99.9%

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

    \[\leadsto \color{blue}{\frac{1}{2} + \frac{1}{4} \cdot \frac{x}{s}} \]
  4. Applied rewrites44.2%

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

    \[\leadsto 1 + \left(1 + \color{blue}{x \cdot \left(\frac{1}{2} \cdot \frac{x}{{s}^{2}} - \frac{1}{s}\right)}\right) \]
  6. Applied rewrites9.3%

    \[\leadsto 1 + \frac{-x}{\color{blue}{s}} \]
  7. Add Preprocessing

Alternative 7: 6.1% accurate, 9.1× speedup?

\[\begin{array}{l} \\ \frac{1}{-x} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (- x)))
float code(float x, float s) {
	return 1.0f / -x;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / -x
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(-x))
end
function tmp = code(x, s)
	tmp = single(1.0) / -x;
end
\begin{array}{l}

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

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

    \[\leadsto \frac{1}{\color{blue}{2}} \]
  4. Applied rewrites6.1%

    \[\leadsto \frac{1}{\color{blue}{-x}} \]
  5. Add Preprocessing

Alternative 8: 4.9% accurate, 42.7× speedup?

\[\begin{array}{l} \\ -x \end{array} \]
(FPCore (x s) :precision binary32 (- x))
float code(float x, float s) {
	return -x;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = -x
end function
function code(x, s)
	return Float32(-x)
end
function tmp = code(x, s)
	tmp = -x;
end
\begin{array}{l}

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

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

    \[\leadsto \color{blue}{\frac{1}{2} + x \cdot \left(\frac{-1}{48} \cdot \frac{{x}^{2}}{{s}^{3}} + \frac{1}{4} \cdot \frac{1}{s}\right)} \]
  4. Applied rewrites10.6%

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

    \[\leadsto \color{blue}{\frac{1}{2} + x \cdot \left({x}^{2} \cdot \left(\frac{1}{480} \cdot \frac{{x}^{2}}{{s}^{5}} - \frac{1}{48} \cdot \frac{1}{{s}^{3}}\right) + \frac{1}{4} \cdot \frac{1}{s}\right)} \]
  6. Applied rewrites5.0%

    \[\leadsto \color{blue}{-x} \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2024321 
(FPCore (x s)
  :name "Logistic function"
  :precision binary32
  :pre (and (<= 0.0 s) (<= s 1.0651631))
  (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))