Logistic distribution

Percentage Accurate: 99.5% → 99.5%
Time: 12.3s
Alternatives: 7
Speedup: 2.9×

Specification

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

\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t_0\\
\frac{t_0}{\left(s \cdot t_1\right) \cdot t_1}
\end{array}
\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 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: 99.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{\frac{-\left|x\right|}{s}}\\ t_1 := 1 + t_0\\ \frac{t_0}{\left(s \cdot t_1\right) \cdot t_1} \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0)))
   (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
	float t_0 = expf((-fabsf(x) / s));
	float t_1 = 1.0f + t_0;
	return t_0 / ((s * t_1) * t_1);
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: t_0
    real(4) :: t_1
    t_0 = exp((-abs(x) / s))
    t_1 = 1.0e0 + t_0
    code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s)
	t_0 = exp(Float32(Float32(-abs(x)) / s))
	t_1 = Float32(Float32(1.0) + t_0)
	return Float32(t_0 / Float32(Float32(s * t_1) * t_1))
end
function tmp = code(x, s)
	t_0 = exp((-abs(x) / s));
	t_1 = single(1.0) + t_0;
	tmp = t_0 / ((s * t_1) * t_1);
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t_0\\
\frac{t_0}{\left(s \cdot t_1\right) \cdot t_1}
\end{array}
\end{array}

Alternative 1: 99.5% accurate, 2.9× speedup?

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

\\
\frac{1}{\left(s \cdot \left(1 + e^{-\frac{x}{s}}\right)\right) \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.9%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Step-by-step derivation
    1. distribute-frac-neg99.9%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{-\frac{\left|x\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    2. rec-exp99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{\left|x\right|}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    3. pow199.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{\color{blue}{{\left(e^{\frac{\left|x\right|}{s}}\right)}^{1}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    4. pow199.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{\color{blue}{e^{\frac{\left|x\right|}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    5. frac-2neg99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\color{blue}{\frac{-\left|x\right|}{-s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    6. add-sqr-sqrt-0.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{-\left|x\right|} \cdot \sqrt{-\left|x\right|}}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    7. sqrt-unprod96.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{\left(-\left|x\right|\right) \cdot \left(-\left|x\right|\right)}}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    8. sqr-neg96.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\sqrt{\color{blue}{\left|x\right| \cdot \left|x\right|}}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    9. sqrt-unprod96.3%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{\left|x\right|} \cdot \sqrt{\left|x\right|}}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    10. add-sqr-sqrt96.3%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\left|x\right|}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    11. remove-double-neg96.3%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{-\left(-\left|x\right|\right)}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    12. frac-2neg96.3%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\color{blue}{\frac{-\left|x\right|}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    13. add-sqr-sqrt-0.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{-\left|x\right|} \cdot \sqrt{-\left|x\right|}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    14. sqrt-unprod97.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{\left(-\left|x\right|\right) \cdot \left(-\left|x\right|\right)}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    15. sqr-neg97.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\sqrt{\color{blue}{\left|x\right| \cdot \left|x\right|}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    16. sqrt-unprod99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{\left|x\right|} \cdot \sqrt{\left|x\right|}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    17. add-sqr-sqrt99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\left|x\right|}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    18. add-sqr-sqrt51.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    19. fabs-sqr51.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    20. add-sqr-sqrt99.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{x}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Applied egg-rr99.0%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{x}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  5. Step-by-step derivation
    1. rec-exp99.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{-\frac{x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  6. Simplified99.0%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{-\frac{x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  7. Step-by-step derivation
    1. add-sqr-sqrt99.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\color{blue}{\sqrt{\frac{\left|x\right|}{s}} \cdot \sqrt{\frac{\left|x\right|}{s}}}}\right)} \]
    2. add-sqr-sqrt99.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\color{blue}{\frac{\left|x\right|}{s}}}\right)} \]
    3. add-sqr-sqrt51.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}\right)} \]
    4. fabs-sqr51.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}\right)} \]
    5. add-sqr-sqrt99.9%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\frac{\color{blue}{x}}{s}}\right)} \]
    6. expm1-log1p-u99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)\right)}} \]
    7. +-commutative99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{e^{\frac{x}{s}} + 1}\right)\right)} \]
    8. expm1-udef99.7%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(e^{\frac{x}{s}} + 1\right)} - 1\right)}} \]
    9. +-commutative99.7%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{1 + e^{\frac{x}{s}}}\right)} - 1\right)} \]
  8. Applied egg-rr99.7%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)} - 1\right)}} \]
  9. Step-by-step derivation
    1. expm1-def99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)\right)}} \]
    2. expm1-log1p99.9%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\left(1 + e^{\frac{x}{s}}\right)}} \]
  10. Simplified99.9%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\left(1 + e^{\frac{x}{s}}\right)}} \]
  11. Taylor expanded in s around 0 99.8%

    \[\leadsto \frac{1}{\color{blue}{s \cdot \left(\left(1 + e^{-\frac{x}{s}}\right) \cdot \left(1 + e^{\frac{x}{s}}\right)\right)}} \]
  12. Step-by-step derivation
    1. associate-*r*99.9%

      \[\leadsto \frac{1}{\color{blue}{\left(s \cdot \left(1 + e^{-\frac{x}{s}}\right)\right) \cdot \left(1 + e^{\frac{x}{s}}\right)}} \]
  13. Simplified99.9%

    \[\leadsto \frac{1}{\color{blue}{\left(s \cdot \left(1 + e^{-\frac{x}{s}}\right)\right) \cdot \left(1 + e^{\frac{x}{s}}\right)}} \]
  14. Final simplification99.9%

    \[\leadsto \frac{1}{\left(s \cdot \left(1 + e^{-\frac{x}{s}}\right)\right) \cdot \left(1 + e^{\frac{x}{s}}\right)} \]

Alternative 2: 58.7% accurate, 5.5× speedup?

\[\begin{array}{l} \\ \frac{1}{\frac{1 + e^{\frac{x}{s}}}{\frac{1}{s + s}}} \end{array} \]
(FPCore (x s)
 :precision binary32
 (/ 1.0 (/ (+ 1.0 (exp (/ x s))) (/ 1.0 (+ s s)))))
float code(float x, float s) {
	return 1.0f / ((1.0f + expf((x / s))) / (1.0f / (s + 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))) / (1.0e0 / (s + s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(x / s))) / Float32(Float32(1.0) / Float32(s + s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / ((single(1.0) + exp((x / s))) / (single(1.0) / (s + s)));
end
\begin{array}{l}

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

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.9%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around inf 96.6%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{1}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Step-by-step derivation
    1. /-rgt-identity96.6%

      \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{fma}\left(s, 1, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}{1}}} \]
    2. *-commutative96.6%

      \[\leadsto \frac{1}{\frac{\color{blue}{\left(1 + e^{\frac{\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, 1, s\right)}}{1}} \]
    3. associate-/l*96.6%

      \[\leadsto \frac{1}{\color{blue}{\frac{1 + e^{\frac{\left|x\right|}{s}}}{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}}} \]
    4. add-sqr-sqrt96.6%

      \[\leadsto \frac{1}{\frac{1 + e^{\color{blue}{\sqrt{\frac{\left|x\right|}{s}} \cdot \sqrt{\frac{\left|x\right|}{s}}}}}{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}} \]
    5. add-sqr-sqrt96.6%

      \[\leadsto \frac{1}{\frac{1 + e^{\color{blue}{\frac{\left|x\right|}{s}}}}{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}} \]
    6. add-sqr-sqrt49.0%

      \[\leadsto \frac{1}{\frac{1 + e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}} \]
    7. fabs-sqr49.0%

      \[\leadsto \frac{1}{\frac{1 + e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}} \]
    8. add-sqr-sqrt62.5%

      \[\leadsto \frac{1}{\frac{1 + e^{\frac{\color{blue}{x}}{s}}}{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}} \]
    9. fma-udef62.5%

      \[\leadsto \frac{1}{\frac{1 + e^{\frac{x}{s}}}{\frac{1}{\color{blue}{s \cdot 1 + s}}}} \]
    10. *-rgt-identity62.5%

      \[\leadsto \frac{1}{\frac{1 + e^{\frac{x}{s}}}{\frac{1}{\color{blue}{s} + s}}} \]
  5. Applied egg-rr62.5%

    \[\leadsto \frac{1}{\color{blue}{\frac{1 + e^{\frac{x}{s}}}{\frac{1}{s + s}}}} \]
  6. Final simplification62.5%

    \[\leadsto \frac{1}{\frac{1 + e^{\frac{x}{s}}}{\frac{1}{s + s}}} \]

Alternative 3: 58.7% accurate, 5.6× speedup?

\[\begin{array}{l} \\ \frac{\frac{1}{s \cdot 2}}{1 + e^{\frac{x}{s}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ (/ 1.0 (* s 2.0)) (+ 1.0 (exp (/ x s)))))
float code(float x, float s) {
	return (1.0f / (s * 2.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 / (s * 2.0e0)) / (1.0e0 + exp((x / s)))
end function
function code(x, s)
	return Float32(Float32(Float32(1.0) / Float32(s * Float32(2.0))) / Float32(Float32(1.0) + exp(Float32(x / s))))
end
function tmp = code(x, s)
	tmp = (single(1.0) / (s * single(2.0))) / (single(1.0) + exp((x / s)));
end
\begin{array}{l}

\\
\frac{\frac{1}{s \cdot 2}}{1 + e^{\frac{x}{s}}}
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.9%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around inf 96.6%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{1}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Step-by-step derivation
    1. expm1-log1p-u95.5%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{\mathsf{fma}\left(s, 1, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}\right)\right)} \]
    2. expm1-udef95.5%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{1}{\mathsf{fma}\left(s, 1, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}\right)} - 1} \]
    3. associate-/r*95.5%

      \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\frac{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}{1 + e^{\frac{\left|x\right|}{s}}}}\right)} - 1 \]
    4. fma-udef95.5%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{\color{blue}{s \cdot 1 + s}}}{1 + e^{\frac{\left|x\right|}{s}}}\right)} - 1 \]
    5. *-rgt-identity95.5%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{\color{blue}{s} + s}}{1 + e^{\frac{\left|x\right|}{s}}}\right)} - 1 \]
    6. add-sqr-sqrt95.5%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s + s}}{1 + e^{\color{blue}{\sqrt{\frac{\left|x\right|}{s}} \cdot \sqrt{\frac{\left|x\right|}{s}}}}}\right)} - 1 \]
    7. add-sqr-sqrt95.5%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s + s}}{1 + e^{\color{blue}{\frac{\left|x\right|}{s}}}}\right)} - 1 \]
    8. add-sqr-sqrt48.6%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s + s}}{1 + e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}\right)} - 1 \]
    9. fabs-sqr48.6%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s + s}}{1 + e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}\right)} - 1 \]
    10. add-sqr-sqrt61.4%

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

    \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{\frac{1}{s + s}}{1 + e^{\frac{x}{s}}}\right)} - 1} \]
  6. Step-by-step derivation
    1. expm1-def61.4%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1}{s + s}}{1 + e^{\frac{x}{s}}}\right)\right)} \]
    2. expm1-log1p62.5%

      \[\leadsto \color{blue}{\frac{\frac{1}{s + s}}{1 + e^{\frac{x}{s}}}} \]
    3. count-262.5%

      \[\leadsto \frac{\frac{1}{\color{blue}{2 \cdot s}}}{1 + e^{\frac{x}{s}}} \]
  7. Simplified62.5%

    \[\leadsto \color{blue}{\frac{\frac{1}{2 \cdot s}}{1 + e^{\frac{x}{s}}}} \]
  8. Final simplification62.5%

    \[\leadsto \frac{\frac{1}{s \cdot 2}}{1 + e^{\frac{x}{s}}} \]

Alternative 4: 65.1% accurate, 56.4× speedup?

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

\\
\frac{1}{s \cdot 4 + x \cdot \frac{x}{s}}
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.9%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Step-by-step derivation
    1. distribute-frac-neg99.9%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{-\frac{\left|x\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    2. rec-exp99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{\left|x\right|}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    3. pow199.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{\color{blue}{{\left(e^{\frac{\left|x\right|}{s}}\right)}^{1}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    4. pow199.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{\color{blue}{e^{\frac{\left|x\right|}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    5. frac-2neg99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\color{blue}{\frac{-\left|x\right|}{-s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    6. add-sqr-sqrt-0.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{-\left|x\right|} \cdot \sqrt{-\left|x\right|}}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    7. sqrt-unprod96.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{\left(-\left|x\right|\right) \cdot \left(-\left|x\right|\right)}}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    8. sqr-neg96.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\sqrt{\color{blue}{\left|x\right| \cdot \left|x\right|}}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    9. sqrt-unprod96.3%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{\left|x\right|} \cdot \sqrt{\left|x\right|}}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    10. add-sqr-sqrt96.3%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\left|x\right|}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    11. remove-double-neg96.3%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{-\left(-\left|x\right|\right)}}{-s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    12. frac-2neg96.3%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\color{blue}{\frac{-\left|x\right|}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    13. add-sqr-sqrt-0.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{-\left|x\right|} \cdot \sqrt{-\left|x\right|}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    14. sqrt-unprod97.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{\left(-\left|x\right|\right) \cdot \left(-\left|x\right|\right)}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    15. sqr-neg97.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\sqrt{\color{blue}{\left|x\right| \cdot \left|x\right|}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    16. sqrt-unprod99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{\left|x\right|} \cdot \sqrt{\left|x\right|}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    17. add-sqr-sqrt99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\left|x\right|}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    18. add-sqr-sqrt51.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    19. fabs-sqr51.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    20. add-sqr-sqrt99.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{x}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Applied egg-rr99.0%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{x}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  5. Step-by-step derivation
    1. rec-exp99.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{-\frac{x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  6. Simplified99.0%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{-\frac{x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  7. Step-by-step derivation
    1. add-sqr-sqrt99.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\color{blue}{\sqrt{\frac{\left|x\right|}{s}} \cdot \sqrt{\frac{\left|x\right|}{s}}}}\right)} \]
    2. add-sqr-sqrt99.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\color{blue}{\frac{\left|x\right|}{s}}}\right)} \]
    3. add-sqr-sqrt51.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}\right)} \]
    4. fabs-sqr51.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}\right)} \]
    5. add-sqr-sqrt99.9%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(1 + e^{\frac{\color{blue}{x}}{s}}\right)} \]
    6. expm1-log1p-u99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)\right)}} \]
    7. +-commutative99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{e^{\frac{x}{s}} + 1}\right)\right)} \]
    8. expm1-udef99.7%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(e^{\frac{x}{s}} + 1\right)} - 1\right)}} \]
    9. +-commutative99.7%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{1 + e^{\frac{x}{s}}}\right)} - 1\right)} \]
  8. Applied egg-rr99.7%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)} - 1\right)}} \]
  9. Step-by-step derivation
    1. expm1-def99.8%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)\right)}} \]
    2. expm1-log1p99.9%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\left(1 + e^{\frac{x}{s}}\right)}} \]
  10. Simplified99.9%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{-\frac{x}{s}}, s\right) \cdot \color{blue}{\left(1 + e^{\frac{x}{s}}\right)}} \]
  11. Taylor expanded in x around 0 65.8%

    \[\leadsto \frac{1}{\color{blue}{4 \cdot s + \frac{{x}^{2}}{s}}} \]
  12. Step-by-step derivation
    1. unpow265.8%

      \[\leadsto \frac{1}{4 \cdot s + \frac{\color{blue}{x \cdot x}}{s}} \]
    2. *-un-lft-identity65.8%

      \[\leadsto \frac{1}{4 \cdot s + \frac{x \cdot x}{\color{blue}{1 \cdot s}}} \]
    3. times-frac66.5%

      \[\leadsto \frac{1}{4 \cdot s + \color{blue}{\frac{x}{1} \cdot \frac{x}{s}}} \]
  13. Applied egg-rr66.5%

    \[\leadsto \frac{1}{4 \cdot s + \color{blue}{\frac{x}{1} \cdot \frac{x}{s}}} \]
  14. Final simplification66.5%

    \[\leadsto \frac{1}{s \cdot 4 + x \cdot \frac{x}{s}} \]

Alternative 5: 28.8% accurate, 68.9× speedup?

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

\\
\frac{1}{s \cdot 4 + x \cdot 2}
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.9%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around inf 96.6%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{1}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Step-by-step derivation
    1. distribute-lft-in96.6%

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 1, s\right) \cdot 1 + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}}} \]
    2. *-rgt-identity96.6%

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 1, s\right)} + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    3. fma-udef96.6%

      \[\leadsto \frac{1}{\color{blue}{\left(s \cdot 1 + s\right)} + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    4. *-rgt-identity96.6%

      \[\leadsto \frac{1}{\left(\color{blue}{s} + s\right) + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    5. fma-udef96.6%

      \[\leadsto \frac{1}{\left(s + s\right) + \color{blue}{\left(s \cdot 1 + s\right)} \cdot e^{\frac{\left|x\right|}{s}}} \]
    6. *-rgt-identity96.6%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(\color{blue}{s} + s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    7. add-sqr-sqrt96.6%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\color{blue}{\sqrt{\frac{\left|x\right|}{s}} \cdot \sqrt{\frac{\left|x\right|}{s}}}}} \]
    8. add-sqr-sqrt96.6%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\color{blue}{\frac{\left|x\right|}{s}}}} \]
    9. add-sqr-sqrt49.0%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}} \]
    10. fabs-sqr49.0%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}} \]
    11. add-sqr-sqrt62.5%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\color{blue}{x}}{s}}} \]
  5. Applied egg-rr62.5%

    \[\leadsto \frac{1}{\color{blue}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{x}{s}}}} \]
  6. Taylor expanded in s around inf 28.9%

    \[\leadsto \frac{1}{\color{blue}{2 \cdot x + 4 \cdot s}} \]
  7. Final simplification28.9%

    \[\leadsto \frac{1}{s \cdot 4 + x \cdot 2} \]

Alternative 6: 28.2% accurate, 121.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.004999999888241291:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{x}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 0.004999999888241291) (/ 0.25 s) (/ 0.5 x)))
float code(float x, float s) {
	float tmp;
	if (x <= 0.004999999888241291f) {
		tmp = 0.25f / s;
	} else {
		tmp = 0.5f / 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.004999999888241291e0) then
        tmp = 0.25e0 / s
    else
        tmp = 0.5e0 / x
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(0.004999999888241291))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(Float32(0.5) / x);
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(0.004999999888241291))
		tmp = single(0.25) / s;
	else
		tmp = single(0.5) / x;
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.004999999888241291:\\
\;\;\;\;\frac{0.25}{s}\\

\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\


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

    1. Initial program 99.7%

      \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
    2. Simplified99.8%

      \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
    3. Taylor expanded in s around inf 35.6%

      \[\leadsto \color{blue}{\frac{0.25}{s}} \]

    if 0.00499999989 < x

    1. Initial program 100.0%

      \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
    3. Taylor expanded in s around inf 100.0%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{1}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    4. Step-by-step derivation
      1. distribute-lft-in100.0%

        \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 1, s\right) \cdot 1 + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}}} \]
      2. *-rgt-identity100.0%

        \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 1, s\right)} + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
      3. fma-udef100.0%

        \[\leadsto \frac{1}{\color{blue}{\left(s \cdot 1 + s\right)} + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
      4. *-rgt-identity100.0%

        \[\leadsto \frac{1}{\left(\color{blue}{s} + s\right) + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
      5. fma-udef100.0%

        \[\leadsto \frac{1}{\left(s + s\right) + \color{blue}{\left(s \cdot 1 + s\right)} \cdot e^{\frac{\left|x\right|}{s}}} \]
      6. *-rgt-identity100.0%

        \[\leadsto \frac{1}{\left(s + s\right) + \left(\color{blue}{s} + s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
      7. add-sqr-sqrt100.0%

        \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\color{blue}{\sqrt{\frac{\left|x\right|}{s}} \cdot \sqrt{\frac{\left|x\right|}{s}}}}} \]
      8. add-sqr-sqrt100.0%

        \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\color{blue}{\frac{\left|x\right|}{s}}}} \]
      9. add-sqr-sqrt100.0%

        \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}} \]
      10. fabs-sqr100.0%

        \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}} \]
      11. add-sqr-sqrt100.0%

        \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\color{blue}{x}}{s}}} \]
    5. Applied egg-rr100.0%

      \[\leadsto \frac{1}{\color{blue}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{x}{s}}}} \]
    6. Taylor expanded in s around inf 10.3%

      \[\leadsto \frac{1}{\color{blue}{2 \cdot x + 4 \cdot s}} \]
    7. Taylor expanded in x around inf 10.3%

      \[\leadsto \color{blue}{\frac{0.5}{x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification28.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 0.004999999888241291:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{x}\\ \end{array} \]

Alternative 7: 26.6% accurate, 206.7× speedup?

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

\\
\frac{0.25}{s}
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.9%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around inf 26.7%

    \[\leadsto \color{blue}{\frac{0.25}{s}} \]
  4. Final simplification26.7%

    \[\leadsto \frac{0.25}{s} \]

Reproduce

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