Sample trimmed logistic on [-pi, pi]

Percentage Accurate: 98.9% → 98.9%
Time: 11.4s
Alternatives: 8
Speedup: N/A×

Specification

?
\[\left(2.328306437 \cdot 10^{-10} \leq u \land u \leq 1\right) \land \left(0 \leq s \land s \leq 1.0651631\right)\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{1 + e^{\frac{\pi}{s}}}\\ \left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - t_0\right) + t_0} - 1\right) \end{array} \end{array} \]
(FPCore (u s)
 :precision binary32
 (let* ((t_0 (/ 1.0 (+ 1.0 (exp (/ PI s))))))
   (*
    (- s)
    (log
     (-
      (/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- PI) s)))) t_0)) t_0))
      1.0)))))
float code(float u, float s) {
	float t_0 = 1.0f / (1.0f + expf((((float) M_PI) / s)));
	return -s * logf(((1.0f / ((u * ((1.0f / (1.0f + expf((-((float) M_PI) / s)))) - t_0)) + t_0)) - 1.0f));
}
function code(u, s)
	t_0 = Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(pi) / s))))
	return Float32(Float32(-s) * log(Float32(Float32(Float32(1.0) / Float32(Float32(u * Float32(Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-Float32(pi)) / s)))) - t_0)) + t_0)) - Float32(1.0))))
end
function tmp = code(u, s)
	t_0 = single(1.0) / (single(1.0) + exp((single(pi) / s)));
	tmp = -s * log(((single(1.0) / ((u * ((single(1.0) / (single(1.0) + exp((-single(pi) / s)))) - t_0)) + t_0)) - single(1.0)));
end
\begin{array}{l}

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

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{1 + e^{\frac{\pi}{s}}}\\ \left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - t_0\right) + t_0} - 1\right) \end{array} \end{array} \]
(FPCore (u s)
 :precision binary32
 (let* ((t_0 (/ 1.0 (+ 1.0 (exp (/ PI s))))))
   (*
    (- s)
    (log
     (-
      (/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- PI) s)))) t_0)) t_0))
      1.0)))))
float code(float u, float s) {
	float t_0 = 1.0f / (1.0f + expf((((float) M_PI) / s)));
	return -s * logf(((1.0f / ((u * ((1.0f / (1.0f + expf((-((float) M_PI) / s)))) - t_0)) + t_0)) - 1.0f));
}
function code(u, s)
	t_0 = Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(pi) / s))))
	return Float32(Float32(-s) * log(Float32(Float32(Float32(1.0) / Float32(Float32(u * Float32(Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-Float32(pi)) / s)))) - t_0)) + t_0)) - Float32(1.0))))
end
function tmp = code(u, s)
	t_0 = single(1.0) / (single(1.0) + exp((single(pi) / s)));
	tmp = -s * log(((single(1.0) / ((u * ((single(1.0) / (single(1.0) + exp((-single(pi) / s)))) - t_0)) + t_0)) - single(1.0)));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{1 + e^{\frac{\pi}{s}}}\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - t_0\right) + t_0} - 1\right)
\end{array}
\end{array}

Alternative 1: 98.9% accurate, 1.4× speedup?

\[\begin{array}{l} \\ s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right) \end{array} \]
(FPCore (u s)
 :precision binary32
 (*
  s
  (-
   (log
    (+
     (/
      1.0
      (+
       (/ u (+ 1.0 (exp (/ PI (- s)))))
       (/ (- 1.0 u) (+ 1.0 (exp (/ PI s))))))
     -1.0)))))
float code(float u, float s) {
	return s * -logf(((1.0f / ((u / (1.0f + expf((((float) M_PI) / -s)))) + ((1.0f - u) / (1.0f + expf((((float) M_PI) / s)))))) + -1.0f));
}
function code(u, s)
	return Float32(s * Float32(-log(Float32(Float32(Float32(1.0) / Float32(Float32(u / Float32(Float32(1.0) + exp(Float32(Float32(pi) / Float32(-s))))) + Float32(Float32(Float32(1.0) - u) / Float32(Float32(1.0) + exp(Float32(Float32(pi) / s)))))) + Float32(-1.0)))))
end
function tmp = code(u, s)
	tmp = s * -log(((single(1.0) / ((u / (single(1.0) + exp((single(pi) / -s)))) + ((single(1.0) - u) / (single(1.0) + exp((single(pi) / s)))))) + single(-1.0)));
end
\begin{array}{l}

\\
s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)} \]
  3. Final simplification99.0%

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

Alternative 2: 25.4% accurate, 1.8× speedup?

\[\begin{array}{l} \\ s \cdot \left(\log s - \log \pi\right) - s \cdot \left(s \cdot \pi\right) \end{array} \]
(FPCore (u s)
 :precision binary32
 (- (* s (- (log s) (log PI))) (* s (* s PI))))
float code(float u, float s) {
	return (s * (logf(s) - logf(((float) M_PI)))) - (s * (s * ((float) M_PI)));
}
function code(u, s)
	return Float32(Float32(s * Float32(log(s) - log(Float32(pi)))) - Float32(s * Float32(s * Float32(pi))))
end
function tmp = code(u, s)
	tmp = (s * (log(s) - log(single(pi)))) - (s * (s * single(pi)));
end
\begin{array}{l}

\\
s \cdot \left(\log s - \log \pi\right) - s \cdot \left(s \cdot \pi\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)} \]
  3. Taylor expanded in s around -inf 24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)}\right) \]
  4. Step-by-step derivation
    1. +-commutative24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s} + 1\right)}\right) \]
    2. fma-def24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}, 1\right)\right)}\right) \]
  5. Simplified24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{\pi \cdot \left(u \cdot -0.25 + 0.25\right) + u \cdot \left(\pi \cdot -0.25\right)}{s}, 1\right)\right)}\right) \]
  6. Taylor expanded in u around 0 24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\log \left(1 + \frac{\pi}{s}\right)}\right) \]
  7. Step-by-step derivation
    1. log1p-def24.8%

      \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  8. Simplified24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  9. Taylor expanded in s around 0 25.1%

    \[\leadsto \color{blue}{-1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \frac{{s}^{2}}{\pi}} \]
  10. Step-by-step derivation
    1. div-inv25.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \color{blue}{\left({s}^{2} \cdot \frac{1}{\pi}\right)} \]
    2. unpow225.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(\color{blue}{\left(s \cdot s\right)} \cdot \frac{1}{\pi}\right) \]
    3. associate-*l*25.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \color{blue}{\left(s \cdot \left(s \cdot \frac{1}{\pi}\right)\right)} \]
    4. div-inv25.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot \color{blue}{\frac{s}{\pi}}\right) \]
    5. add-exp-log25.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot \frac{\color{blue}{e^{\log s}}}{\pi}\right) \]
    6. add-exp-log25.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot \frac{e^{\log s}}{\color{blue}{e^{\log \pi}}}\right) \]
    7. div-exp25.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot \color{blue}{e^{\log s - \log \pi}}\right) \]
    8. remove-double-neg25.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot e^{\color{blue}{\left(-\left(-\log s\right)\right)} - \log \pi}\right) \]
    9. neg-sub025.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot e^{\color{blue}{\left(0 - \left(-\log s\right)\right)} - \log \pi}\right) \]
    10. associate--r+25.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot e^{\color{blue}{0 - \left(\left(-\log s\right) + \log \pi\right)}}\right) \]
    11. +-commutative25.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot e^{0 - \color{blue}{\left(\log \pi + \left(-\log s\right)\right)}}\right) \]
    12. neg-sub025.1%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot e^{\color{blue}{-\left(\log \pi + \left(-\log s\right)\right)}}\right) \]
    13. add-sqr-sqrt-0.0%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot e^{\color{blue}{\sqrt{-\left(\log \pi + \left(-\log s\right)\right)} \cdot \sqrt{-\left(\log \pi + \left(-\log s\right)\right)}}}\right) \]
    14. sqrt-unprod10.5%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot e^{\color{blue}{\sqrt{\left(-\left(\log \pi + \left(-\log s\right)\right)\right) \cdot \left(-\left(\log \pi + \left(-\log s\right)\right)\right)}}}\right) \]
    15. sqr-neg10.5%

      \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \left(s \cdot e^{\sqrt{\color{blue}{\left(\log \pi + \left(-\log s\right)\right) \cdot \left(\log \pi + \left(-\log s\right)\right)}}}\right) \]
  11. Applied egg-rr25.1%

    \[\leadsto -1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) + -1 \cdot \color{blue}{\left(s \cdot \left(\pi \cdot s\right)\right)} \]
  12. Final simplification25.1%

    \[\leadsto s \cdot \left(\log s - \log \pi\right) - s \cdot \left(s \cdot \pi\right) \]

Alternative 3: 25.3% accurate, 1.8× speedup?

\[\begin{array}{l} \\ s \cdot \left(\left(\log s - \frac{s}{\pi}\right) - \log \pi\right) \end{array} \]
(FPCore (u s) :precision binary32 (* s (- (- (log s) (/ s PI)) (log PI))))
float code(float u, float s) {
	return s * ((logf(s) - (s / ((float) M_PI))) - logf(((float) M_PI)));
}
function code(u, s)
	return Float32(s * Float32(Float32(log(s) - Float32(s / Float32(pi))) - log(Float32(pi))))
end
function tmp = code(u, s)
	tmp = s * ((log(s) - (s / single(pi))) - log(single(pi)));
end
\begin{array}{l}

\\
s \cdot \left(\left(\log s - \frac{s}{\pi}\right) - \log \pi\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)} \]
  3. Taylor expanded in s around -inf 24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)}\right) \]
  4. Step-by-step derivation
    1. +-commutative24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s} + 1\right)}\right) \]
    2. fma-def24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}, 1\right)\right)}\right) \]
  5. Simplified24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{\pi \cdot \left(u \cdot -0.25 + 0.25\right) + u \cdot \left(\pi \cdot -0.25\right)}{s}, 1\right)\right)}\right) \]
  6. Taylor expanded in u around 0 24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\log \left(1 + \frac{\pi}{s}\right)}\right) \]
  7. Step-by-step derivation
    1. log1p-def24.8%

      \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  8. Simplified24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  9. Taylor expanded in s around 0 25.1%

    \[\leadsto s \cdot \left(-\color{blue}{\left(\log \pi + \left(-1 \cdot \log s + \frac{s}{\pi}\right)\right)}\right) \]
  10. Final simplification25.1%

    \[\leadsto s \cdot \left(\left(\log s - \frac{s}{\pi}\right) - \log \pi\right) \]

Alternative 4: 25.3% accurate, 2.4× speedup?

\[\begin{array}{l} \\ s \cdot \left(\log s - \log \pi\right) \end{array} \]
(FPCore (u s) :precision binary32 (* s (- (log s) (log PI))))
float code(float u, float s) {
	return s * (logf(s) - logf(((float) M_PI)));
}
function code(u, s)
	return Float32(s * Float32(log(s) - log(Float32(pi))))
end
function tmp = code(u, s)
	tmp = s * (log(s) - log(single(pi)));
end
\begin{array}{l}

\\
s \cdot \left(\log s - \log \pi\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)} \]
  3. Taylor expanded in s around -inf 24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)}\right) \]
  4. Step-by-step derivation
    1. +-commutative24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s} + 1\right)}\right) \]
    2. fma-def24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}, 1\right)\right)}\right) \]
  5. Simplified24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{\pi \cdot \left(u \cdot -0.25 + 0.25\right) + u \cdot \left(\pi \cdot -0.25\right)}{s}, 1\right)\right)}\right) \]
  6. Taylor expanded in u around 0 24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\log \left(1 + \frac{\pi}{s}\right)}\right) \]
  7. Step-by-step derivation
    1. log1p-def24.8%

      \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  8. Simplified24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  9. Taylor expanded in s around 0 25.0%

    \[\leadsto s \cdot \left(-\color{blue}{\left(\log \pi + -1 \cdot \log s\right)}\right) \]
  10. Step-by-step derivation
    1. mul-1-neg25.0%

      \[\leadsto s \cdot \left(-\left(\log \pi + \color{blue}{\left(-\log s\right)}\right)\right) \]
  11. Simplified25.0%

    \[\leadsto s \cdot \left(-\color{blue}{\left(\log \pi + \left(-\log s\right)\right)}\right) \]
  12. Taylor expanded in s around 0 25.0%

    \[\leadsto \color{blue}{s \cdot \left(\log s - \log \pi\right)} \]
  13. Final simplification25.0%

    \[\leadsto s \cdot \left(\log s - \log \pi\right) \]

Alternative 5: 25.2% accurate, 3.6× speedup?

\[\begin{array}{l} \\ s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right) \end{array} \]
(FPCore (u s) :precision binary32 (* s (- (log1p (/ PI s)))))
float code(float u, float s) {
	return s * -log1pf((((float) M_PI) / s));
}
function code(u, s)
	return Float32(s * Float32(-log1p(Float32(Float32(pi) / s))))
end
\begin{array}{l}

\\
s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)} \]
  3. Taylor expanded in s around -inf 24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)}\right) \]
  4. Step-by-step derivation
    1. +-commutative24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s} + 1\right)}\right) \]
    2. fma-def24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}, 1\right)\right)}\right) \]
  5. Simplified24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{\pi \cdot \left(u \cdot -0.25 + 0.25\right) + u \cdot \left(\pi \cdot -0.25\right)}{s}, 1\right)\right)}\right) \]
  6. Taylor expanded in u around 0 24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\log \left(1 + \frac{\pi}{s}\right)}\right) \]
  7. Step-by-step derivation
    1. log1p-def24.8%

      \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  8. Simplified24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  9. Final simplification24.8%

    \[\leadsto s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right) \]

Alternative 6: 11.7% accurate, 6.8× speedup?

\[\begin{array}{l} \\ 4 \cdot \left(\pi \cdot \left(u \cdot 0.5 + -0.25\right)\right) \end{array} \]
(FPCore (u s) :precision binary32 (* 4.0 (* PI (+ (* u 0.5) -0.25))))
float code(float u, float s) {
	return 4.0f * (((float) M_PI) * ((u * 0.5f) + -0.25f));
}
function code(u, s)
	return Float32(Float32(4.0) * Float32(Float32(pi) * Float32(Float32(u * Float32(0.5)) + Float32(-0.25))))
end
function tmp = code(u, s)
	tmp = single(4.0) * (single(pi) * ((u * single(0.5)) + single(-0.25)));
end
\begin{array}{l}

\\
4 \cdot \left(\pi \cdot \left(u \cdot 0.5 + -0.25\right)\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)} \]
  3. Taylor expanded in s around inf 11.0%

    \[\leadsto \color{blue}{4 \cdot \left(0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \left(u \cdot \pi\right) + 0.25 \cdot \pi\right)\right)} \]
  4. Step-by-step derivation
    1. associate--r+11.0%

      \[\leadsto 4 \cdot \color{blue}{\left(\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) - 0.25 \cdot \pi\right)} \]
    2. cancel-sign-sub-inv11.0%

      \[\leadsto 4 \cdot \color{blue}{\left(\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) + \left(-0.25\right) \cdot \pi\right)} \]
    3. distribute-rgt-out--11.0%

      \[\leadsto 4 \cdot \left(\color{blue}{\left(u \cdot \pi\right) \cdot \left(0.25 - -0.25\right)} + \left(-0.25\right) \cdot \pi\right) \]
    4. *-commutative11.0%

      \[\leadsto 4 \cdot \left(\color{blue}{\left(\pi \cdot u\right)} \cdot \left(0.25 - -0.25\right) + \left(-0.25\right) \cdot \pi\right) \]
    5. metadata-eval11.0%

      \[\leadsto 4 \cdot \left(\left(\pi \cdot u\right) \cdot \color{blue}{0.5} + \left(-0.25\right) \cdot \pi\right) \]
    6. metadata-eval11.0%

      \[\leadsto 4 \cdot \left(\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{-0.25} \cdot \pi\right) \]
    7. *-commutative11.0%

      \[\leadsto 4 \cdot \left(\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{\pi \cdot -0.25}\right) \]
  5. Simplified11.0%

    \[\leadsto \color{blue}{4 \cdot \left(\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25\right)} \]
  6. Step-by-step derivation
    1. associate-*l*11.0%

      \[\leadsto 4 \cdot \left(\color{blue}{\pi \cdot \left(u \cdot 0.5\right)} + \pi \cdot -0.25\right) \]
    2. distribute-lft-out11.0%

      \[\leadsto 4 \cdot \color{blue}{\left(\pi \cdot \left(u \cdot 0.5 + -0.25\right)\right)} \]
  7. Applied egg-rr11.0%

    \[\leadsto 4 \cdot \color{blue}{\left(\pi \cdot \left(u \cdot 0.5 + -0.25\right)\right)} \]
  8. Final simplification11.0%

    \[\leadsto 4 \cdot \left(\pi \cdot \left(u \cdot 0.5 + -0.25\right)\right) \]

Alternative 7: 11.5% accurate, 7.2× speedup?

\[\begin{array}{l} \\ -\pi \end{array} \]
(FPCore (u s) :precision binary32 (- PI))
float code(float u, float s) {
	return -((float) M_PI);
}
function code(u, s)
	return Float32(-Float32(pi))
end
function tmp = code(u, s)
	tmp = -single(pi);
end
\begin{array}{l}

\\
-\pi
\end{array}
Derivation
  1. Initial program 99.0%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)} \]
  3. Taylor expanded in u around 0 10.8%

    \[\leadsto \color{blue}{-1 \cdot \pi} \]
  4. Step-by-step derivation
    1. neg-mul-110.8%

      \[\leadsto \color{blue}{-\pi} \]
  5. Simplified10.8%

    \[\leadsto \color{blue}{-\pi} \]
  6. Final simplification10.8%

    \[\leadsto -\pi \]

Alternative 8: 4.6% accurate, 7.3× speedup?

\[\begin{array}{l} \\ \pi \end{array} \]
(FPCore (u s) :precision binary32 PI)
float code(float u, float s) {
	return (float) M_PI;
}
function code(u, s)
	return Float32(pi)
end
function tmp = code(u, s)
	tmp = single(pi);
end
\begin{array}{l}

\\
\pi
\end{array}
Derivation
  1. Initial program 99.0%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)} \]
  3. Taylor expanded in s around -inf 24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)}\right) \]
  4. Step-by-step derivation
    1. +-commutative24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s} + 1\right)}\right) \]
    2. fma-def24.5%

      \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}, 1\right)\right)}\right) \]
  5. Simplified24.5%

    \[\leadsto s \cdot \left(-\log \color{blue}{\left(\mathsf{fma}\left(4, \frac{\pi \cdot \left(u \cdot -0.25 + 0.25\right) + u \cdot \left(\pi \cdot -0.25\right)}{s}, 1\right)\right)}\right) \]
  6. Taylor expanded in u around 0 24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\log \left(1 + \frac{\pi}{s}\right)}\right) \]
  7. Step-by-step derivation
    1. log1p-def24.8%

      \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  8. Simplified24.8%

    \[\leadsto s \cdot \left(-\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right) \]
  9. Step-by-step derivation
    1. add-cube-cbrt24.8%

      \[\leadsto \color{blue}{\left(\sqrt[3]{s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right)} \cdot \sqrt[3]{s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right)}\right) \cdot \sqrt[3]{s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right)}} \]
    2. pow324.8%

      \[\leadsto \color{blue}{{\left(\sqrt[3]{s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right)}\right)}^{3}} \]
    3. add-sqr-sqrt-0.0%

      \[\leadsto {\left(\sqrt[3]{s \cdot \color{blue}{\left(\sqrt{-\mathsf{log1p}\left(\frac{\pi}{s}\right)} \cdot \sqrt{-\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right)}}\right)}^{3} \]
    4. sqrt-unprod7.5%

      \[\leadsto {\left(\sqrt[3]{s \cdot \color{blue}{\sqrt{\left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right) \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right)}}}\right)}^{3} \]
    5. sqr-neg7.5%

      \[\leadsto {\left(\sqrt[3]{s \cdot \sqrt{\color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right) \cdot \mathsf{log1p}\left(\frac{\pi}{s}\right)}}}\right)}^{3} \]
    6. sqrt-unprod7.5%

      \[\leadsto {\left(\sqrt[3]{s \cdot \color{blue}{\left(\sqrt{\mathsf{log1p}\left(\frac{\pi}{s}\right)} \cdot \sqrt{\mathsf{log1p}\left(\frac{\pi}{s}\right)}\right)}}\right)}^{3} \]
    7. add-sqr-sqrt7.5%

      \[\leadsto {\left(\sqrt[3]{s \cdot \color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)}}\right)}^{3} \]
  10. Applied egg-rr7.5%

    \[\leadsto \color{blue}{{\left(\sqrt[3]{s \cdot \mathsf{log1p}\left(\frac{\pi}{s}\right)}\right)}^{3}} \]
  11. Taylor expanded in s around inf 4.7%

    \[\leadsto \color{blue}{{1}^{0.3333333333333333} \cdot \pi} \]
  12. Step-by-step derivation
    1. pow-base-14.7%

      \[\leadsto \color{blue}{1} \cdot \pi \]
    2. *-lft-identity4.7%

      \[\leadsto \color{blue}{\pi} \]
  13. Simplified4.7%

    \[\leadsto \color{blue}{\pi} \]
  14. Final simplification4.7%

    \[\leadsto \pi \]

Reproduce

?
herbie shell --seed 2023334 
(FPCore (u s)
  :name "Sample trimmed logistic on [-pi, pi]"
  :precision binary32
  :pre (and (and (<= 2.328306437e-10 u) (<= u 1.0)) (and (<= 0.0 s) (<= s 1.0651631)))
  (* (- s) (log (- (/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- PI) s)))) (/ 1.0 (+ 1.0 (exp (/ PI s)))))) (/ 1.0 (+ 1.0 (exp (/ PI s)))))) 1.0))))