Sample trimmed logistic on [-pi, pi]

Percentage Accurate: 99.0% → 99.0%
Time: 11.9s
Alternatives: 4
Speedup: 1.0×

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} 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} \]
(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}
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}

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 4 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} 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} \]
(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}
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}

Alternative 1: 99.0% accurate, 1.0× speedup?

\[\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{-3.1415927410125732}{s}}} - t\_0\right) + t\_0} - 1\right) \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 (/ -3.1415927410125732 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((-3.1415927410125732f / 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(-3.1415927410125732) / 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(-3.1415927410125732) / s)))) - t_0)) + t_0)) - single(1.0)));
end
\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{-3.1415927410125732}{s}}} - t\_0\right) + t\_0} - 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. Evaluated real constant99.0%

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

Alternative 2: 37.7% accurate, 1.3× speedup?

\[\begin{array}{l} t_0 := \frac{1}{1 + e^{\frac{\pi}{s}}}\\ \left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(0.5 - t\_0\right) + t\_0} - 1\right) \end{array} \]
(FPCore (u s)
  :precision binary32
  (let* ((t_0 (/ 1.0 (+ 1.0 (exp (/ PI s))))))
  (* (- s) (log (- (/ 1.0 (+ (* u (- 0.5 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 * (0.5f - 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(0.5) - 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(0.5) - t_0)) + t_0)) - single(1.0)));
end
\begin{array}{l}
t_0 := \frac{1}{1 + e^{\frac{\pi}{s}}}\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(0.5 - t\_0\right) + t\_0} - 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. Taylor expanded in s around inf

    \[\leadsto \left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\color{blue}{\frac{1}{2}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  3. Step-by-step derivation
    1. Applied rewrites37.7%

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

    Alternative 3: 11.6% accurate, 10.0× speedup?

    \[\mathsf{fma}\left(-1, \pi, 6.2831854820251465 \cdot u\right) \]
    (FPCore (u s)
      :precision binary32
      (fma -1.0 PI (* 6.2831854820251465 u)))
    float code(float u, float s) {
    	return fmaf(-1.0f, ((float) M_PI), (6.2831854820251465f * u));
    }
    
    function code(u, s)
    	return fma(Float32(-1.0), Float32(pi), Float32(Float32(6.2831854820251465) * u))
    end
    
    \mathsf{fma}\left(-1, \pi, 6.2831854820251465 \cdot u\right)
    
    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. Taylor expanded in s around inf

      \[\leadsto \color{blue}{4 \cdot \left(u \cdot \left(\frac{1}{4} \cdot \pi - \frac{-1}{4} \cdot \pi\right) - \frac{1}{4} \cdot \pi\right)} \]
    3. Step-by-step derivation
      1. lower-*.f32N/A

        \[\leadsto 4 \cdot \color{blue}{\left(u \cdot \left(\frac{1}{4} \cdot \mathsf{PI}\left(\right) - \frac{-1}{4} \cdot \mathsf{PI}\left(\right)\right) - \frac{1}{4} \cdot \mathsf{PI}\left(\right)\right)} \]
      2. lower--.f32N/A

        \[\leadsto 4 \cdot \left(u \cdot \left(\frac{1}{4} \cdot \mathsf{PI}\left(\right) - \frac{-1}{4} \cdot \mathsf{PI}\left(\right)\right) - \color{blue}{\frac{1}{4} \cdot \mathsf{PI}\left(\right)}\right) \]
      3. lower-*.f32N/A

        \[\leadsto 4 \cdot \left(u \cdot \left(\frac{1}{4} \cdot \mathsf{PI}\left(\right) - \frac{-1}{4} \cdot \mathsf{PI}\left(\right)\right) - \color{blue}{\frac{1}{4}} \cdot \mathsf{PI}\left(\right)\right) \]
      4. lower--.f32N/A

        \[\leadsto 4 \cdot \left(u \cdot \left(\frac{1}{4} \cdot \mathsf{PI}\left(\right) - \frac{-1}{4} \cdot \mathsf{PI}\left(\right)\right) - \frac{1}{4} \cdot \mathsf{PI}\left(\right)\right) \]
      5. lower-*.f32N/A

        \[\leadsto 4 \cdot \left(u \cdot \left(\frac{1}{4} \cdot \mathsf{PI}\left(\right) - \frac{-1}{4} \cdot \mathsf{PI}\left(\right)\right) - \frac{1}{4} \cdot \mathsf{PI}\left(\right)\right) \]
      6. lower-PI.f32N/A

        \[\leadsto 4 \cdot \left(u \cdot \left(\frac{1}{4} \cdot \pi - \frac{-1}{4} \cdot \mathsf{PI}\left(\right)\right) - \frac{1}{4} \cdot \mathsf{PI}\left(\right)\right) \]
      7. lower-*.f32N/A

        \[\leadsto 4 \cdot \left(u \cdot \left(\frac{1}{4} \cdot \pi - \frac{-1}{4} \cdot \mathsf{PI}\left(\right)\right) - \frac{1}{4} \cdot \mathsf{PI}\left(\right)\right) \]
      8. lower-PI.f32N/A

        \[\leadsto 4 \cdot \left(u \cdot \left(\frac{1}{4} \cdot \pi - \frac{-1}{4} \cdot \pi\right) - \frac{1}{4} \cdot \mathsf{PI}\left(\right)\right) \]
      9. lower-*.f32N/A

        \[\leadsto 4 \cdot \left(u \cdot \left(\frac{1}{4} \cdot \pi - \frac{-1}{4} \cdot \pi\right) - \frac{1}{4} \cdot \color{blue}{\mathsf{PI}\left(\right)}\right) \]
      10. lower-PI.f3211.6%

        \[\leadsto 4 \cdot \left(u \cdot \left(0.25 \cdot \pi - -0.25 \cdot \pi\right) - 0.25 \cdot \pi\right) \]
    4. Applied rewrites11.6%

      \[\leadsto \color{blue}{4 \cdot \left(u \cdot \left(0.25 \cdot \pi - -0.25 \cdot \pi\right) - 0.25 \cdot \pi\right)} \]
    5. Evaluated real constant11.6%

      \[\leadsto 4 \cdot \left(u \cdot 1.5707963705062866 - 0.25 \cdot \pi\right) \]
    6. Taylor expanded in u around 0

      \[\leadsto -1 \cdot \pi + \color{blue}{\frac{13176795}{2097152} \cdot u} \]
    7. Step-by-step derivation
      1. lower-fma.f32N/A

        \[\leadsto \mathsf{fma}\left(-1, \mathsf{PI}\left(\right), \frac{13176795}{2097152} \cdot u\right) \]
      2. lower-PI.f32N/A

        \[\leadsto \mathsf{fma}\left(-1, \pi, \frac{13176795}{2097152} \cdot u\right) \]
      3. lower-*.f3211.6%

        \[\leadsto \mathsf{fma}\left(-1, \pi, 6.2831854820251465 \cdot u\right) \]
    8. Applied rewrites11.6%

      \[\leadsto \mathsf{fma}\left(-1, \color{blue}{\pi}, 6.2831854820251465 \cdot u\right) \]
    9. Add Preprocessing

    Alternative 4: 11.4% accurate, 90.2× speedup?

    \[-3.1415927410125732 \]
    (FPCore (u s)
      :precision binary32
      -3.1415927410125732)
    float code(float u, float s) {
    	return -3.1415927410125732f;
    }
    
    real(4) function code(u, s)
    use fmin_fmax_functions
        real(4), intent (in) :: u
        real(4), intent (in) :: s
        code = -3.1415927410125732e0
    end function
    
    function code(u, s)
    	return Float32(-3.1415927410125732)
    end
    
    function tmp = code(u, s)
    	tmp = single(-3.1415927410125732);
    end
    
    -3.1415927410125732
    
    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. Taylor expanded in u around 0

      \[\leadsto \color{blue}{-1 \cdot \pi} \]
    3. Step-by-step derivation
      1. lower-*.f32N/A

        \[\leadsto -1 \cdot \color{blue}{\mathsf{PI}\left(\right)} \]
      2. lower-PI.f3211.4%

        \[\leadsto -1 \cdot \pi \]
    4. Applied rewrites11.4%

      \[\leadsto \color{blue}{-1 \cdot \pi} \]
    5. Evaluated real constant11.4%

      \[\leadsto -3.1415927410125732 \]
    6. Add Preprocessing

    Reproduce

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