Sample trimmed logistic on [-pi, pi]

Percentage Accurate: 98.9% → 98.9%
Time: 15.5s
Alternatives: 6
Speedup: 1.3×

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 6 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.3× 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(-Float32(pi)) / 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 98.8%

    \[\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. Simplified98.8%

    \[\leadsto \color{blue}{\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)} \]
  3. Add Preprocessing
  4. Final simplification98.8%

    \[\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) \]
  5. Add Preprocessing

Alternative 2: 25.2% accurate, 2.0× speedup?

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

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

    \[\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. Simplified98.8%

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

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

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

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

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) - 0.25 \cdot \pi}}{s}, 1\right)\right) \]
    4. cancel-sign-sub-inv25.0%

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

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

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

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

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

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

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

    \[\leadsto \left(-s\right) \cdot \color{blue}{\left(\log \left(-4 \cdot \left(-0.25 \cdot \pi + 0.5 \cdot \left(u \cdot \pi\right)\right)\right) + \left(-1 \cdot \log s + -0.25 \cdot \frac{s}{-0.25 \cdot \pi + 0.5 \cdot \left(u \cdot \pi\right)}\right)\right)} \]
  8. Taylor expanded in u around 0 25.4%

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

    \[\leadsto s \cdot \left(u \cdot \left(2 - 2 \cdot \frac{s}{\pi}\right)\right) + s \cdot \left(\left(\log s - \frac{s}{\pi}\right) - \log \pi\right) \]
  10. Add Preprocessing

Alternative 3: 25.1% accurate, 4.1× speedup?

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

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

    \[\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. Simplified98.8%

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

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

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

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

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) - 0.25 \cdot \pi}}{s}, 1\right)\right) \]
    4. cancel-sign-sub-inv25.0%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{-1 \cdot \left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right)} \]
  11. Step-by-step derivation
    1. *-commutative25.3%

      \[\leadsto \color{blue}{\left(s \cdot \left(\log \pi + -1 \cdot \log s\right)\right) \cdot -1} \]
    2. associate-*r*25.3%

      \[\leadsto \color{blue}{s \cdot \left(\left(\log \pi + -1 \cdot \log s\right) \cdot -1\right)} \]
    3. mul-1-neg25.3%

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

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

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

      \[\leadsto s \cdot \color{blue}{\left(-\left(\log \pi + \log \left(\frac{1}{s}\right)\right)\right)} \]
    7. log-rec25.3%

      \[\leadsto s \cdot \left(-\left(\log \pi + \color{blue}{\left(-\log s\right)}\right)\right) \]
    8. mul-1-neg25.3%

      \[\leadsto s \cdot \left(-\left(\log \pi + \color{blue}{-1 \cdot \log s}\right)\right) \]
    9. +-commutative25.3%

      \[\leadsto s \cdot \left(-\color{blue}{\left(-1 \cdot \log s + \log \pi\right)}\right) \]
    10. distribute-neg-in25.3%

      \[\leadsto s \cdot \color{blue}{\left(\left(--1 \cdot \log s\right) + \left(-\log \pi\right)\right)} \]
    11. mul-1-neg25.3%

      \[\leadsto s \cdot \left(\left(-\color{blue}{\left(-\log s\right)}\right) + \left(-\log \pi\right)\right) \]
    12. remove-double-neg25.3%

      \[\leadsto s \cdot \left(\color{blue}{\log s} + \left(-\log \pi\right)\right) \]
    13. sub-neg25.3%

      \[\leadsto s \cdot \color{blue}{\left(\log s - \log \pi\right)} \]
    14. log-div25.3%

      \[\leadsto s \cdot \color{blue}{\log \left(\frac{s}{\pi}\right)} \]
  12. Simplified25.3%

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

    \[\leadsto s \cdot \log \left(\frac{s}{\pi}\right) \]
  14. Add Preprocessing

Alternative 4: 13.2% accurate, 72.2× speedup?

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

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

    \[\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. Simplified98.8%

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

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

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

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

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) - 0.25 \cdot \pi}}{s}, 1\right)\right) \]
    4. cancel-sign-sub-inv25.0%

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

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

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

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

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

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

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

    \[\leadsto \left(-s\right) \cdot \color{blue}{\log \left(1 + \frac{\pi}{s}\right)} \]
  8. Taylor expanded in s around inf 11.5%

    \[\leadsto \left(-s\right) \cdot \color{blue}{\frac{\pi}{s}} \]
  9. Step-by-step derivation
    1. distribute-lft-neg-out11.5%

      \[\leadsto \color{blue}{-s \cdot \frac{\pi}{s}} \]
    2. neg-sub011.5%

      \[\leadsto \color{blue}{0 - s \cdot \frac{\pi}{s}} \]
    3. div-inv11.5%

      \[\leadsto 0 - s \cdot \color{blue}{\left(\pi \cdot \frac{1}{s}\right)} \]
    4. add-exp-log11.5%

      \[\leadsto 0 - s \cdot \color{blue}{e^{\log \left(\pi \cdot \frac{1}{s}\right)}} \]
    5. log-prod11.5%

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

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

      \[\leadsto 0 - s \cdot e^{\log \pi + \color{blue}{-1 \cdot \log s}} \]
    8. add-sqr-sqrt11.5%

      \[\leadsto 0 - s \cdot e^{\log \pi + \color{blue}{\sqrt{-1 \cdot \log s} \cdot \sqrt{-1 \cdot \log s}}} \]
    9. sqrt-unprod11.5%

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

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

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

      \[\leadsto 0 - s \cdot e^{\log \pi + \sqrt{\color{blue}{\log s \cdot \log s}}} \]
    13. sqrt-unprod-0.0%

      \[\leadsto 0 - s \cdot e^{\log \pi + \color{blue}{\sqrt{\log s} \cdot \sqrt{\log s}}} \]
    14. add-sqr-sqrt13.4%

      \[\leadsto 0 - s \cdot e^{\log \pi + \color{blue}{\log s}} \]
    15. prod-exp13.4%

      \[\leadsto 0 - s \cdot \color{blue}{\left(e^{\log \pi} \cdot e^{\log s}\right)} \]
    16. add-exp-log13.4%

      \[\leadsto 0 - s \cdot \left(\color{blue}{\pi} \cdot e^{\log s}\right) \]
    17. add-exp-log13.4%

      \[\leadsto 0 - s \cdot \left(\pi \cdot \color{blue}{s}\right) \]
  10. Applied egg-rr13.4%

    \[\leadsto \color{blue}{0 - s \cdot \left(\pi \cdot s\right)} \]
  11. Step-by-step derivation
    1. neg-sub013.4%

      \[\leadsto \color{blue}{-s \cdot \left(\pi \cdot s\right)} \]
    2. distribute-rgt-neg-in13.4%

      \[\leadsto \color{blue}{s \cdot \left(-\pi \cdot s\right)} \]
    3. distribute-lft-neg-in13.4%

      \[\leadsto s \cdot \color{blue}{\left(\left(-\pi\right) \cdot s\right)} \]
  12. Simplified13.4%

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

    \[\leadsto s \cdot \left(s \cdot \left(-\pi\right)\right) \]
  14. Add Preprocessing

Alternative 5: 11.4% accurate, 216.5× 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 98.8%

    \[\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. Simplified98.8%

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

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

      \[\leadsto \color{blue}{-\pi} \]
  6. Simplified11.5%

    \[\leadsto \color{blue}{-\pi} \]
  7. Final simplification11.5%

    \[\leadsto -\pi \]
  8. Add Preprocessing

Alternative 6: 4.6% accurate, 433.0× 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 98.8%

    \[\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. Simplified98.8%

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

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

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

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

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) - 0.25 \cdot \pi}}{s}, 1\right)\right) \]
    4. cancel-sign-sub-inv25.0%

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

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

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

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

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

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

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

    \[\leadsto \left(-s\right) \cdot \color{blue}{\log \left(1 + \frac{\pi}{s}\right)} \]
  8. Taylor expanded in s around inf 11.5%

    \[\leadsto \left(-s\right) \cdot \color{blue}{\frac{\pi}{s}} \]
  9. Step-by-step derivation
    1. clear-num11.5%

      \[\leadsto \left(-s\right) \cdot \color{blue}{\frac{1}{\frac{s}{\pi}}} \]
    2. un-div-inv11.5%

      \[\leadsto \color{blue}{\frac{-s}{\frac{s}{\pi}}} \]
    3. add-sqr-sqrt-0.0%

      \[\leadsto \frac{\color{blue}{\sqrt{-s} \cdot \sqrt{-s}}}{\frac{s}{\pi}} \]
    4. sqrt-unprod7.9%

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

      \[\leadsto \frac{\sqrt{\color{blue}{s \cdot s}}}{\frac{s}{\pi}} \]
    6. sqrt-unprod4.6%

      \[\leadsto \frac{\color{blue}{\sqrt{s} \cdot \sqrt{s}}}{\frac{s}{\pi}} \]
    7. add-sqr-sqrt4.6%

      \[\leadsto \frac{\color{blue}{s}}{\frac{s}{\pi}} \]
  10. Applied egg-rr4.6%

    \[\leadsto \color{blue}{\frac{s}{\frac{s}{\pi}}} \]
  11. Step-by-step derivation
    1. associate-/r/4.6%

      \[\leadsto \color{blue}{\frac{s}{s} \cdot \pi} \]
    2. *-inverses4.6%

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

      \[\leadsto \color{blue}{\pi} \]
  12. Simplified4.6%

    \[\leadsto \color{blue}{\pi} \]
  13. Final simplification4.6%

    \[\leadsto \pi \]
  14. Add Preprocessing

Reproduce

?
herbie shell --seed 2024050 
(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))))