Logistic function

Percentage Accurate: 99.8% → 99.8%
Time: 2.3s
Alternatives: 7
Speedup: 0.8×

Specification

?
\[0 \leq s \land s \leq 1.0651631\]
\[\frac{1}{1 + e^{\frac{-x}{s}}} \]
(FPCore (x s)
  :precision binary32
  :pre (and (<= 0.0 s) (<= s 1.0651631))
  (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\frac{1}{1 + e^{\frac{-x}{s}}}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 7 alternatives:

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

Initial Program: 99.8% accurate, 1.0× speedup?

\[0 \leq s \land s \leq 1.0651631\]
\[\frac{1}{1 + e^{\frac{-x}{s}}} \]
(FPCore (x s)
  :precision binary32
  :pre (and (<= 0.0 s) (<= s 1.0651631))
  (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\frac{1}{1 + e^{\frac{-x}{s}}}

Alternative 1: 99.8% accurate, 0.8× speedup?

\[0 \leq s \land s \leq 1.0651631\]
\[\frac{1}{1 + {e}^{\left(\frac{-x}{s}\right)}} \]
(FPCore (x s)
  :precision binary32
  :pre (and (<= 0.0 s) (<= s 1.0651631))
  (/ 1.0 (+ 1.0 (pow E (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + powf(((float) M_E), (-x / s)));
}
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + (Float32(exp(1)) ^ Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + (single(2.71828182845904523536) ^ (-x / s)));
end
\frac{1}{1 + {e}^{\left(\frac{-x}{s}\right)}}
Derivation
  1. Initial program 99.8%

    \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
  2. Step-by-step derivation
    1. Applied rewrites99.7%

      \[\leadsto \frac{1}{1 + \sqrt{e^{\frac{x + x}{-s}}}} \]
    2. Step-by-step derivation
      1. Applied rewrites99.8%

        \[\leadsto \frac{1}{1 + {e}^{\left(\frac{-x}{s}\right)}} \]
      2. Add Preprocessing

      Alternative 2: 99.8% accurate, 0.9× speedup?

      \[0 \leq s \land s \leq 1.0651631\]
      \[\frac{1}{1 + \sqrt{e^{-2 \cdot \frac{x}{s}}}} \]
      (FPCore (x s)
        :precision binary32
        :pre (and (<= 0.0 s) (<= s 1.0651631))
        (/ 1.0 (+ 1.0 (sqrt (exp (* -2.0 (/ x s)))))))
      float code(float x, float s) {
      	return 1.0f / (1.0f + sqrtf(expf((-2.0f * (x / s)))));
      }
      
      real(4) function code(x, s)
      use fmin_fmax_functions
          real(4), intent (in) :: x
          real(4), intent (in) :: s
          code = 1.0e0 / (1.0e0 + sqrt(exp(((-2.0e0) * (x / s)))))
      end function
      
      function code(x, s)
      	return Float32(Float32(1.0) / Float32(Float32(1.0) + sqrt(exp(Float32(Float32(-2.0) * Float32(x / s))))))
      end
      
      function tmp = code(x, s)
      	tmp = single(1.0) / (single(1.0) + sqrt(exp((single(-2.0) * (x / s)))));
      end
      
      \frac{1}{1 + \sqrt{e^{-2 \cdot \frac{x}{s}}}}
      
      Derivation
      1. Initial program 99.8%

        \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
      2. Step-by-step derivation
        1. Applied rewrites99.7%

          \[\leadsto \frac{1}{1 + \sqrt{e^{\frac{x + x}{-s}}}} \]
        2. Step-by-step derivation
          1. Applied rewrites99.7%

            \[\leadsto \frac{1}{1 + \sqrt{e^{-2 \cdot \frac{x}{s}}}} \]
          2. Add Preprocessing

          Alternative 3: 95.7% accurate, 0.9× speedup?

          \[0 \leq s \land s \leq 1.0651631\]
          \[\begin{array}{l} \mathbf{if}\;\frac{-x}{s} \leq 10:\\ \;\;\;\;\frac{1}{1 + \frac{-1}{-\left(1 + \frac{x}{s}\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{0}{2}\\ \end{array} \]
          (FPCore (x s)
            :precision binary32
            :pre (and (<= 0.0 s) (<= s 1.0651631))
            (if (<= (/ (- x) s) 10.0)
            (/ 1.0 (+ 1.0 (/ -1.0 (- (+ 1.0 (/ x s))))))
            (/ 0.0 2.0)))
          float code(float x, float s) {
          	float tmp;
          	if ((-x / s) <= 10.0f) {
          		tmp = 1.0f / (1.0f + (-1.0f / -(1.0f + (x / s))));
          	} else {
          		tmp = 0.0f / 2.0f;
          	}
          	return tmp;
          }
          
          real(4) function code(x, s)
          use fmin_fmax_functions
              real(4), intent (in) :: x
              real(4), intent (in) :: s
              real(4) :: tmp
              if ((-x / s) <= 10.0e0) then
                  tmp = 1.0e0 / (1.0e0 + ((-1.0e0) / -(1.0e0 + (x / s))))
              else
                  tmp = 0.0e0 / 2.0e0
              end if
              code = tmp
          end function
          
          function code(x, s)
          	tmp = Float32(0.0)
          	if (Float32(Float32(-x) / s) <= Float32(10.0))
          		tmp = Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(-1.0) / Float32(-Float32(Float32(1.0) + Float32(x / s))))));
          	else
          		tmp = Float32(Float32(0.0) / Float32(2.0));
          	end
          	return tmp
          end
          
          function tmp_2 = code(x, s)
          	tmp = single(0.0);
          	if ((-x / s) <= single(10.0))
          		tmp = single(1.0) / (single(1.0) + (single(-1.0) / -(single(1.0) + (x / s))));
          	else
          		tmp = single(0.0) / single(2.0);
          	end
          	tmp_2 = tmp;
          end
          
          \begin{array}{l}
          \mathbf{if}\;\frac{-x}{s} \leq 10:\\
          \;\;\;\;\frac{1}{1 + \frac{-1}{-\left(1 + \frac{x}{s}\right)}}\\
          
          \mathbf{else}:\\
          \;\;\;\;\frac{0}{2}\\
          
          
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if (/.f32 (neg.f32 x) s) < 10

            1. Initial program 99.8%

              \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
            2. Step-by-step derivation
              1. Applied rewrites99.7%

                \[\leadsto \frac{1}{1 + \sqrt{e^{\frac{x + x}{-s}}}} \]
              2. Step-by-step derivation
                1. Applied rewrites99.7%

                  \[\leadsto \frac{1}{1 + \sqrt{e^{-2 \cdot \frac{x}{s}}}} \]
                2. Step-by-step derivation
                  1. Applied rewrites99.8%

                    \[\leadsto \frac{1}{1 + \frac{-1}{-e^{\frac{x}{s}}}} \]
                  2. Taylor expanded in x around 0

                    \[\leadsto \frac{1}{1 + \frac{-1}{-\left(1 + \frac{x}{s}\right)}} \]
                  3. Step-by-step derivation
                    1. Applied rewrites61.4%

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

                    if 10 < (/.f32 (neg.f32 x) s)

                    1. Initial program 99.8%

                      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
                    2. Taylor expanded in x around 0

                      \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{s}} \]
                    3. Step-by-step derivation
                      1. Applied rewrites40.7%

                        \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{s}} \]
                      2. Taylor expanded in undef-var around zero

                        \[\leadsto \frac{0}{2 + -1 \cdot \frac{x}{s}} \]
                      3. Step-by-step derivation
                        1. Applied rewrites40.6%

                          \[\leadsto \frac{0}{2 + -1 \cdot \frac{x}{s}} \]
                        2. Taylor expanded in x around 0

                          \[\leadsto \frac{0}{2} \]
                        3. Step-by-step derivation
                          1. Applied rewrites40.6%

                            \[\leadsto \frac{0}{2} \]
                        4. Recombined 2 regimes into one program.
                        5. Add Preprocessing

                        Alternative 4: 69.3% accurate, 1.2× speedup?

                        \[0 \leq s \land s \leq 1.0651631\]
                        \[\begin{array}{l} \mathbf{if}\;\frac{-x}{s} \leq 10:\\ \;\;\;\;\frac{0.5 \cdot s}{s} \cdot 1\\ \mathbf{else}:\\ \;\;\;\;\frac{0}{2}\\ \end{array} \]
                        (FPCore (x s)
                          :precision binary32
                          :pre (and (<= 0.0 s) (<= s 1.0651631))
                          (if (<= (/ (- x) s) 10.0) (* (/ (* 0.5 s) s) 1.0) (/ 0.0 2.0)))
                        float code(float x, float s) {
                        	float tmp;
                        	if ((-x / s) <= 10.0f) {
                        		tmp = ((0.5f * s) / s) * 1.0f;
                        	} else {
                        		tmp = 0.0f / 2.0f;
                        	}
                        	return tmp;
                        }
                        
                        real(4) function code(x, s)
                        use fmin_fmax_functions
                            real(4), intent (in) :: x
                            real(4), intent (in) :: s
                            real(4) :: tmp
                            if ((-x / s) <= 10.0e0) then
                                tmp = ((0.5e0 * s) / s) * 1.0e0
                            else
                                tmp = 0.0e0 / 2.0e0
                            end if
                            code = tmp
                        end function
                        
                        function code(x, s)
                        	tmp = Float32(0.0)
                        	if (Float32(Float32(-x) / s) <= Float32(10.0))
                        		tmp = Float32(Float32(Float32(Float32(0.5) * s) / s) * Float32(1.0));
                        	else
                        		tmp = Float32(Float32(0.0) / Float32(2.0));
                        	end
                        	return tmp
                        end
                        
                        function tmp_2 = code(x, s)
                        	tmp = single(0.0);
                        	if ((-x / s) <= single(10.0))
                        		tmp = ((single(0.5) * s) / s) * single(1.0);
                        	else
                        		tmp = single(0.0) / single(2.0);
                        	end
                        	tmp_2 = tmp;
                        end
                        
                        \begin{array}{l}
                        \mathbf{if}\;\frac{-x}{s} \leq 10:\\
                        \;\;\;\;\frac{0.5 \cdot s}{s} \cdot 1\\
                        
                        \mathbf{else}:\\
                        \;\;\;\;\frac{0}{2}\\
                        
                        
                        \end{array}
                        
                        Derivation
                        1. Split input into 2 regimes
                        2. if (/.f32 (neg.f32 x) s) < 10

                          1. Initial program 99.8%

                            \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
                          2. Taylor expanded in x around 0

                            \[\leadsto \frac{1}{2} + \frac{1}{4} \cdot \frac{x}{s} \]
                          3. Step-by-step derivation
                            1. Applied rewrites29.4%

                              \[\leadsto 0.5 + 0.25 \cdot \frac{x}{s} \]
                            2. Step-by-step derivation
                              1. Applied rewrites29.3%

                                \[\leadsto \frac{1}{\frac{s}{\mathsf{fma}\left(0.5, s, 0.25 \cdot x\right)}} \]
                              2. Taylor expanded in x around 0

                                \[\leadsto \frac{1}{\frac{s}{\frac{1}{2} \cdot s}} \]
                              3. Step-by-step derivation
                                1. Applied rewrites35.0%

                                  \[\leadsto \frac{1}{\frac{s}{0.5 \cdot s}} \]
                                2. Step-by-step derivation
                                  1. Applied rewrites35.0%

                                    \[\leadsto \frac{0.5 \cdot s}{s} \cdot 1 \]

                                  if 10 < (/.f32 (neg.f32 x) s)

                                  1. Initial program 99.8%

                                    \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
                                  2. Taylor expanded in x around 0

                                    \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{s}} \]
                                  3. Step-by-step derivation
                                    1. Applied rewrites40.7%

                                      \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{s}} \]
                                    2. Taylor expanded in undef-var around zero

                                      \[\leadsto \frac{0}{2 + -1 \cdot \frac{x}{s}} \]
                                    3. Step-by-step derivation
                                      1. Applied rewrites40.6%

                                        \[\leadsto \frac{0}{2 + -1 \cdot \frac{x}{s}} \]
                                      2. Taylor expanded in x around 0

                                        \[\leadsto \frac{0}{2} \]
                                      3. Step-by-step derivation
                                        1. Applied rewrites40.6%

                                          \[\leadsto \frac{0}{2} \]
                                      4. Recombined 2 regimes into one program.
                                      5. Add Preprocessing

                                      Alternative 5: 69.3% accurate, 1.7× speedup?

                                      \[0 \leq s \land s \leq 1.0651631\]
                                      \[\begin{array}{l} \mathbf{if}\;\frac{-x}{s} \leq 10:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{0}{2}\\ \end{array} \]
                                      (FPCore (x s)
                                        :precision binary32
                                        :pre (and (<= 0.0 s) (<= s 1.0651631))
                                        (if (<= (/ (- x) s) 10.0) 0.5 (/ 0.0 2.0)))
                                      float code(float x, float s) {
                                      	float tmp;
                                      	if ((-x / s) <= 10.0f) {
                                      		tmp = 0.5f;
                                      	} else {
                                      		tmp = 0.0f / 2.0f;
                                      	}
                                      	return tmp;
                                      }
                                      
                                      real(4) function code(x, s)
                                      use fmin_fmax_functions
                                          real(4), intent (in) :: x
                                          real(4), intent (in) :: s
                                          real(4) :: tmp
                                          if ((-x / s) <= 10.0e0) then
                                              tmp = 0.5e0
                                          else
                                              tmp = 0.0e0 / 2.0e0
                                          end if
                                          code = tmp
                                      end function
                                      
                                      function code(x, s)
                                      	tmp = Float32(0.0)
                                      	if (Float32(Float32(-x) / s) <= Float32(10.0))
                                      		tmp = Float32(0.5);
                                      	else
                                      		tmp = Float32(Float32(0.0) / Float32(2.0));
                                      	end
                                      	return tmp
                                      end
                                      
                                      function tmp_2 = code(x, s)
                                      	tmp = single(0.0);
                                      	if ((-x / s) <= single(10.0))
                                      		tmp = single(0.5);
                                      	else
                                      		tmp = single(0.0) / single(2.0);
                                      	end
                                      	tmp_2 = tmp;
                                      end
                                      
                                      \begin{array}{l}
                                      \mathbf{if}\;\frac{-x}{s} \leq 10:\\
                                      \;\;\;\;0.5\\
                                      
                                      \mathbf{else}:\\
                                      \;\;\;\;\frac{0}{2}\\
                                      
                                      
                                      \end{array}
                                      
                                      Derivation
                                      1. Split input into 2 regimes
                                      2. if (/.f32 (neg.f32 x) s) < 10

                                        1. Initial program 99.8%

                                          \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
                                        2. Taylor expanded in x around 0

                                          \[\leadsto \frac{1}{2} \]
                                        3. Step-by-step derivation
                                          1. Applied rewrites35.0%

                                            \[\leadsto 0.5 \]

                                          if 10 < (/.f32 (neg.f32 x) s)

                                          1. Initial program 99.8%

                                            \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
                                          2. Taylor expanded in x around 0

                                            \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{s}} \]
                                          3. Step-by-step derivation
                                            1. Applied rewrites40.7%

                                              \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{s}} \]
                                            2. Taylor expanded in undef-var around zero

                                              \[\leadsto \frac{0}{2 + -1 \cdot \frac{x}{s}} \]
                                            3. Step-by-step derivation
                                              1. Applied rewrites40.6%

                                                \[\leadsto \frac{0}{2 + -1 \cdot \frac{x}{s}} \]
                                              2. Taylor expanded in x around 0

                                                \[\leadsto \frac{0}{2} \]
                                              3. Step-by-step derivation
                                                1. Applied rewrites40.6%

                                                  \[\leadsto \frac{0}{2} \]
                                              4. Recombined 2 regimes into one program.
                                              5. Add Preprocessing

                                              Alternative 6: 35.0% accurate, 23.2× speedup?

                                              \[0 \leq s \land s \leq 1.0651631\]
                                              \[0.5 \]
                                              (FPCore (x s)
                                                :precision binary32
                                                :pre (and (<= 0.0 s) (<= s 1.0651631))
                                                0.5)
                                              float code(float x, float s) {
                                              	return 0.5f;
                                              }
                                              
                                              real(4) function code(x, s)
                                              use fmin_fmax_functions
                                                  real(4), intent (in) :: x
                                                  real(4), intent (in) :: s
                                                  code = 0.5e0
                                              end function
                                              
                                              function code(x, s)
                                              	return Float32(0.5)
                                              end
                                              
                                              function tmp = code(x, s)
                                              	tmp = single(0.5);
                                              end
                                              
                                              0.5
                                              
                                              Derivation
                                              1. Initial program 99.8%

                                                \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
                                              2. Taylor expanded in x around 0

                                                \[\leadsto \frac{1}{2} \]
                                              3. Step-by-step derivation
                                                1. Applied rewrites35.0%

                                                  \[\leadsto 0.5 \]
                                                2. Add Preprocessing

                                                Reproduce

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