Sample trimmed logistic on [-pi, pi]

Percentage Accurate: 99.0% → 99.0%
Time: 21.0s
Alternatives: 8
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 8 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.0% 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: 99.0% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{{\left(\sqrt[3]{\pi}\right)}^{2} \cdot \frac{\sqrt[3]{\pi}}{s}}}} + -1\right) \end{array} \]
(FPCore (u s)
 :precision binary32
 (*
  (- s)
  (log
   (+
    (/
     1.0
     (+
      (/ u (+ 1.0 (exp (/ PI (- s)))))
      (/ (- 1.0 u) (+ 1.0 (exp (* (pow (cbrt PI) 2.0) (/ (cbrt 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((powf(cbrtf(((float) M_PI)), 2.0f) * (cbrtf(((float) M_PI)) / s))))))) + -1.0f));
}
function code(u, s)
	return Float32(Float32(-s) * 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((cbrt(Float32(pi)) ^ Float32(2.0)) * Float32(cbrt(Float32(pi)) / s))))))) + Float32(-1.0))))
end
\begin{array}{l}

\\
\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{{\left(\sqrt[3]{\pi}\right)}^{2} \cdot \frac{\sqrt[3]{\pi}}{s}}}} + -1\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}{\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. Step-by-step derivation
    1. add-cube-cbrt99.0%

      \[\leadsto \left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\color{blue}{\left(\sqrt[3]{\pi} \cdot \sqrt[3]{\pi}\right) \cdot \sqrt[3]{\pi}}}{s}}}} + -1\right) \]
    2. associate-/l*99.0%

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

      \[\leadsto \left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\color{blue}{{\left(\sqrt[3]{\pi}\right)}^{2}} \cdot \frac{\sqrt[3]{\pi}}{s}}}} + -1\right) \]
  5. Applied egg-rr99.0%

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

    \[\leadsto \left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{{\left(\sqrt[3]{\pi}\right)}^{2} \cdot \frac{\sqrt[3]{\pi}}{s}}}} + -1\right) \]
  7. Add Preprocessing

Alternative 2: 99.0% accurate, 1.3× speedup?

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

\\
\left(-s\right) \cdot \log \left(-1 + \frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}}\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}{\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 simplification99.0%

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

Alternative 3: 13.2% accurate, 1.4× speedup?

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

\\
\log \left({\left(e^{-s}\right)}^{\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}{\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.3%

    \[\leadsto \left(-s\right) \cdot \color{blue}{\frac{\pi}{s}} \]
  5. Step-by-step derivation
    1. add-log-exp11.3%

      \[\leadsto \color{blue}{\log \left(e^{\left(-s\right) \cdot \frac{\pi}{s}}\right)} \]
    2. exp-prod13.0%

      \[\leadsto \log \color{blue}{\left({\left(e^{-s}\right)}^{\left(\frac{\pi}{s}\right)}\right)} \]
  6. Applied egg-rr13.0%

    \[\leadsto \color{blue}{\log \left({\left(e^{-s}\right)}^{\left(\frac{\pi}{s}\right)}\right)} \]
  7. Final simplification13.0%

    \[\leadsto \log \left({\left(e^{-s}\right)}^{\left(\frac{\pi}{s}\right)}\right) \]
  8. Add Preprocessing

Alternative 4: 11.7% accurate, 2.1× speedup?

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

\\
4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\pi \cdot \left(-0.25 + u \cdot 0.5\right)\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}{\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 11.5%

    \[\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)} \]
  5. Step-by-step derivation
    1. associate--r+11.5%

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{4 \cdot \left(\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25\right)} \]
  7. Step-by-step derivation
    1. expm1-log1p-u11.5%

      \[\leadsto 4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25\right)\right)} \]
    2. expm1-undefine11.5%

      \[\leadsto 4 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25\right)} - 1\right)} \]
    3. associate-*l*11.5%

      \[\leadsto 4 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\pi \cdot \left(u \cdot 0.5\right)} + \pi \cdot -0.25\right)} - 1\right) \]
    4. fma-define11.5%

      \[\leadsto 4 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\mathsf{fma}\left(\pi, u \cdot 0.5, \pi \cdot -0.25\right)}\right)} - 1\right) \]
  8. Applied egg-rr11.5%

    \[\leadsto 4 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\mathsf{fma}\left(\pi, u \cdot 0.5, \pi \cdot -0.25\right)\right)} - 1\right)} \]
  9. Step-by-step derivation
    1. expm1-define11.5%

      \[\leadsto 4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(\pi, u \cdot 0.5, \pi \cdot -0.25\right)\right)\right)} \]
    2. fma-undefine11.5%

      \[\leadsto 4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\pi \cdot \left(u \cdot 0.5\right) + \pi \cdot -0.25}\right)\right) \]
    3. *-commutative11.5%

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

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

      \[\leadsto 4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\left(0.5 \cdot u\right)} \cdot \pi + -0.25 \cdot \pi\right)\right) \]
    6. associate-*r*11.5%

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

      \[\leadsto 4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{-0.25 \cdot \pi + 0.5 \cdot \left(u \cdot \pi\right)}\right)\right) \]
    8. associate-*r*11.5%

      \[\leadsto 4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(-0.25 \cdot \pi + \color{blue}{\left(0.5 \cdot u\right) \cdot \pi}\right)\right) \]
    9. *-commutative11.5%

      \[\leadsto 4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(-0.25 \cdot \pi + \color{blue}{\left(u \cdot 0.5\right)} \cdot \pi\right)\right) \]
    10. distribute-rgt-out11.5%

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

    \[\leadsto 4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\pi \cdot \left(-0.25 + u \cdot 0.5\right)\right)\right)} \]
  11. Final simplification11.5%

    \[\leadsto 4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\pi \cdot \left(-0.25 + u \cdot 0.5\right)\right)\right) \]
  12. Add Preprocessing

Alternative 5: 11.7% accurate, 33.3× speedup?

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

\\
\left(s \cdot 4\right) \cdot \frac{\pi \cdot \left(-0.25 + u \cdot 0.5\right)}{s}
\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}{\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 11.5%

    \[\leadsto \left(-s\right) \cdot \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}\right)} \]
  5. Step-by-step derivation
    1. associate--r+11.5%

      \[\leadsto \left(-s\right) \cdot \left(-4 \cdot \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}\right) \]
    2. cancel-sign-sub-inv11.5%

      \[\leadsto \left(-s\right) \cdot \left(-4 \cdot \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}\right) \]
    3. distribute-rgt-out--11.5%

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

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

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

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

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

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

      \[\leadsto \color{blue}{{\left(\left(-s\right) \cdot \left(-4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}\right)\right)}^{1}} \]
    2. associate-*r*11.5%

      \[\leadsto {\color{blue}{\left(\left(\left(-s\right) \cdot -4\right) \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}\right)}}^{1} \]
    3. associate-*l*11.5%

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

      \[\leadsto {\left(\left(\left(-s\right) \cdot -4\right) \cdot \frac{\color{blue}{\mathsf{fma}\left(\pi, u \cdot 0.5, \pi \cdot -0.25\right)}}{s}\right)}^{1} \]
  8. Applied egg-rr11.5%

    \[\leadsto \color{blue}{{\left(\left(\left(-s\right) \cdot -4\right) \cdot \frac{\mathsf{fma}\left(\pi, u \cdot 0.5, \pi \cdot -0.25\right)}{s}\right)}^{1}} \]
  9. Step-by-step derivation
    1. unpow111.5%

      \[\leadsto \color{blue}{\left(\left(-s\right) \cdot -4\right) \cdot \frac{\mathsf{fma}\left(\pi, u \cdot 0.5, \pi \cdot -0.25\right)}{s}} \]
    2. distribute-lft-neg-out11.5%

      \[\leadsto \color{blue}{\left(-s \cdot -4\right)} \cdot \frac{\mathsf{fma}\left(\pi, u \cdot 0.5, \pi \cdot -0.25\right)}{s} \]
    3. distribute-rgt-neg-in11.5%

      \[\leadsto \color{blue}{\left(s \cdot \left(--4\right)\right)} \cdot \frac{\mathsf{fma}\left(\pi, u \cdot 0.5, \pi \cdot -0.25\right)}{s} \]
    4. metadata-eval11.5%

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

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

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

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

      \[\leadsto \left(s \cdot 4\right) \cdot \frac{\color{blue}{\left(0.5 \cdot u\right)} \cdot \pi + -0.25 \cdot \pi}{s} \]
    9. associate-*r*11.5%

      \[\leadsto \left(s \cdot 4\right) \cdot \frac{\color{blue}{0.5 \cdot \left(u \cdot \pi\right)} + -0.25 \cdot \pi}{s} \]
    10. +-commutative11.5%

      \[\leadsto \left(s \cdot 4\right) \cdot \frac{\color{blue}{-0.25 \cdot \pi + 0.5 \cdot \left(u \cdot \pi\right)}}{s} \]
    11. associate-*r*11.5%

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

      \[\leadsto \left(s \cdot 4\right) \cdot \frac{-0.25 \cdot \pi + \color{blue}{\left(u \cdot 0.5\right)} \cdot \pi}{s} \]
    13. distribute-rgt-out11.5%

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

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

    \[\leadsto \left(s \cdot 4\right) \cdot \frac{\pi \cdot \left(-0.25 + u \cdot 0.5\right)}{s} \]
  12. Add Preprocessing

Alternative 6: 11.7% accurate, 61.9× speedup?

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

\\
\pi \cdot \left(-1 + u \cdot 2\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}{\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. Step-by-step derivation
    1. add-cube-cbrt99.0%

      \[\leadsto \left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\color{blue}{\left(\sqrt[3]{\pi} \cdot \sqrt[3]{\pi}\right) \cdot \sqrt[3]{\pi}}}{s}}}} + -1\right) \]
    2. associate-/l*99.0%

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

      \[\leadsto \left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\color{blue}{{\left(\sqrt[3]{\pi}\right)}^{2}} \cdot \frac{\sqrt[3]{\pi}}{s}}}} + -1\right) \]
  5. Applied egg-rr99.0%

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

    \[\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)} \]
  7. Step-by-step derivation
    1. associate--r+11.5%

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

      \[\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. *-commutative11.5%

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

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

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

      \[\leadsto 4 \cdot \left(\left(\left(\pi \cdot u\right) \cdot 0.25 - \color{blue}{\left(\pi \cdot u\right) \cdot -0.25}\right) + \left(-0.25\right) \cdot \pi\right) \]
    7. distribute-lft-out--11.5%

      \[\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) \]
    8. metadata-eval11.5%

      \[\leadsto 4 \cdot \left(\left(\pi \cdot u\right) \cdot \color{blue}{0.5} + \left(-0.25\right) \cdot \pi\right) \]
    9. associate-*r*11.5%

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

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

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

      \[\leadsto 4 \cdot \left(\color{blue}{\left(0.5 \cdot u\right)} \cdot \pi + -0.25 \cdot \pi\right) \]
    13. associate-*r*11.5%

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

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

    \[\leadsto \color{blue}{\pi \cdot \left(-1 + 2 \cdot u\right)} \]
  9. Final simplification11.5%

    \[\leadsto \pi \cdot \left(-1 + u \cdot 2\right) \]
  10. Add Preprocessing

Alternative 7: 11.5% accurate, 72.2× speedup?

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

\\
\frac{s \cdot \left(-\pi\right)}{s}
\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}{\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.3%

    \[\leadsto \left(-s\right) \cdot \color{blue}{\frac{\pi}{s}} \]
  5. Step-by-step derivation
    1. associate-*r/11.3%

      \[\leadsto \color{blue}{\frac{\left(-s\right) \cdot \pi}{s}} \]
  6. Applied egg-rr11.3%

    \[\leadsto \color{blue}{\frac{\left(-s\right) \cdot \pi}{s}} \]
  7. Final simplification11.3%

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

Alternative 8: 11.5% 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 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}{\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.3%

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

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

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

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

Reproduce

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