Logistic function

Percentage Accurate: 99.8% → 99.8%
Time: 3.1s
Alternatives: 7
Speedup: 0.5×

Specification

?
\[0 \leq s \land s \leq \frac{10651631}{10000000}\]
\[\frac{1}{1 + e^{\frac{-x}{s}}} \]
(FPCore (x s)
  :precision binary32
  (/ 1 (+ 1 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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?

\[\frac{1}{1 + e^{\frac{-x}{s}}} \]
(FPCore (x s)
  :precision binary32
  (/ 1 (+ 1 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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.5× speedup?

\[\frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\left(e \cdot e\right)}^{\left(\frac{\frac{-1}{3} \cdot \frac{x}{s}}{2}\right)}} \]
(FPCore (x s)
  :precision binary32
  (/
 1
 (+
  1
  (*
   (pow (* E E) (* (/ x s) -1/3))
   (pow (* E E) (/ (* -1/3 (/ x s)) 2))))))
float code(float x, float s) {
	return 1.0f / (1.0f + (powf((((float) M_E) * ((float) M_E)), ((x / s) * -0.3333333333333333f)) * powf((((float) M_E) * ((float) M_E)), ((-0.3333333333333333f * (x / s)) / 2.0f))));
}
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32((Float32(Float32(exp(1)) * Float32(exp(1))) ^ Float32(Float32(x / s) * Float32(-0.3333333333333333))) * (Float32(Float32(exp(1)) * Float32(exp(1))) ^ Float32(Float32(Float32(-0.3333333333333333) * Float32(x / s)) / Float32(2.0))))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + (((single(2.71828182845904523536) * single(2.71828182845904523536)) ^ ((x / s) * single(-0.3333333333333333))) * ((single(2.71828182845904523536) * single(2.71828182845904523536)) ^ ((single(-0.3333333333333333) * (x / s)) / single(2.0)))));
end
\frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\left(e \cdot e\right)}^{\left(\frac{\frac{-1}{3} \cdot \frac{x}{s}}{2}\right)}}
Derivation
  1. Initial program 99.8%

    \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
  2. Step-by-step derivation
    1. lift-exp.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{e^{\frac{-x}{s}}}} \]
    2. lift-/.f32N/A

      \[\leadsto \frac{1}{1 + e^{\color{blue}{\frac{-x}{s}}}} \]
    3. mult-flipN/A

      \[\leadsto \frac{1}{1 + e^{\color{blue}{\left(-x\right) \cdot \frac{1}{s}}}} \]
    4. *-commutativeN/A

      \[\leadsto \frac{1}{1 + e^{\color{blue}{\frac{1}{s} \cdot \left(-x\right)}}} \]
    5. mult-flipN/A

      \[\leadsto \frac{1}{1 + e^{\color{blue}{\left(1 \cdot \frac{1}{s}\right)} \cdot \left(-x\right)}} \]
    6. associate-*l*N/A

      \[\leadsto \frac{1}{1 + e^{\color{blue}{1 \cdot \left(\frac{1}{s} \cdot \left(-x\right)\right)}}} \]
    7. *-commutativeN/A

      \[\leadsto \frac{1}{1 + e^{1 \cdot \color{blue}{\left(\left(-x\right) \cdot \frac{1}{s}\right)}}} \]
    8. mult-flipN/A

      \[\leadsto \frac{1}{1 + e^{1 \cdot \color{blue}{\frac{-x}{s}}}} \]
    9. lift-/.f32N/A

      \[\leadsto \frac{1}{1 + e^{1 \cdot \color{blue}{\frac{-x}{s}}}} \]
    10. exp-prodN/A

      \[\leadsto \frac{1}{1 + \color{blue}{{\left(e^{1}\right)}^{\left(\frac{-x}{s}\right)}}} \]
    11. lower-pow.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{{\left(e^{1}\right)}^{\left(\frac{-x}{s}\right)}}} \]
    12. lower-exp.f3299.8%

      \[\leadsto \frac{1}{1 + {\color{blue}{\left(e^{1}\right)}}^{\left(\frac{-x}{s}\right)}} \]
  3. Applied rewrites99.8%

    \[\leadsto \frac{1}{1 + \color{blue}{{\left(e^{1}\right)}^{\left(\frac{-x}{s}\right)}}} \]
  4. Step-by-step derivation
    1. lift-pow.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{{\left(e^{1}\right)}^{\left(\frac{-x}{s}\right)}}} \]
    2. lift-exp.f32N/A

      \[\leadsto \frac{1}{1 + {\color{blue}{\left(e^{1}\right)}}^{\left(\frac{-x}{s}\right)}} \]
    3. exp-1-eN/A

      \[\leadsto \frac{1}{1 + {\color{blue}{\mathsf{E}\left(\right)}}^{\left(\frac{-x}{s}\right)}} \]
    4. add-cbrt-cubeN/A

      \[\leadsto \frac{1}{1 + {\color{blue}{\left(\sqrt[3]{\left(\mathsf{E}\left(\right) \cdot \mathsf{E}\left(\right)\right) \cdot \mathsf{E}\left(\right)}\right)}}^{\left(\frac{-x}{s}\right)}} \]
    5. pow-cbrtN/A

      \[\leadsto \frac{1}{1 + \color{blue}{{\left(\left(\mathsf{E}\left(\right) \cdot \mathsf{E}\left(\right)\right) \cdot \mathsf{E}\left(\right)\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}}} \]
    6. lower-pow.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{{\left(\left(\mathsf{E}\left(\right) \cdot \mathsf{E}\left(\right)\right) \cdot \mathsf{E}\left(\right)\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}}} \]
    7. exp-1-eN/A

      \[\leadsto \frac{1}{1 + {\left(\left(\mathsf{E}\left(\right) \cdot \mathsf{E}\left(\right)\right) \cdot \color{blue}{e^{1}}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    8. lift-exp.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\left(\mathsf{E}\left(\right) \cdot \mathsf{E}\left(\right)\right) \cdot \color{blue}{e^{1}}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    9. lower-*.f32N/A

      \[\leadsto \frac{1}{1 + {\color{blue}{\left(\left(\mathsf{E}\left(\right) \cdot \mathsf{E}\left(\right)\right) \cdot e^{1}\right)}}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    10. exp-1-eN/A

      \[\leadsto \frac{1}{1 + {\left(\left(\color{blue}{e^{1}} \cdot \mathsf{E}\left(\right)\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    11. lift-exp.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\left(\color{blue}{e^{1}} \cdot \mathsf{E}\left(\right)\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    12. exp-1-eN/A

      \[\leadsto \frac{1}{1 + {\left(\left(e^{1} \cdot \color{blue}{e^{1}}\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    13. lift-exp.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\left(e^{1} \cdot \color{blue}{e^{1}}\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    14. lower-*.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\color{blue}{\left(e^{1} \cdot e^{1}\right)} \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    15. lift-exp.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\left(\color{blue}{e^{1}} \cdot e^{1}\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    16. exp-1-eN/A

      \[\leadsto \frac{1}{1 + {\left(\left(\color{blue}{\mathsf{E}\left(\right)} \cdot e^{1}\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    17. lower-E.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\left(\color{blue}{e} \cdot e^{1}\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    18. lift-exp.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\left(e \cdot \color{blue}{e^{1}}\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    19. exp-1-eN/A

      \[\leadsto \frac{1}{1 + {\left(\left(e \cdot \color{blue}{\mathsf{E}\left(\right)}\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    20. lower-E.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\left(e \cdot \color{blue}{e}\right) \cdot e^{1}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    21. lift-exp.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\left(e \cdot e\right) \cdot \color{blue}{e^{1}}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    22. exp-1-eN/A

      \[\leadsto \frac{1}{1 + {\left(\left(e \cdot e\right) \cdot \color{blue}{\mathsf{E}\left(\right)}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    23. lower-E.f32N/A

      \[\leadsto \frac{1}{1 + {\left(\left(e \cdot e\right) \cdot \color{blue}{e}\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    24. lower-/.f3299.8%

      \[\leadsto \frac{1}{1 + {\left(\left(e \cdot e\right) \cdot e\right)}^{\color{blue}{\left(\frac{\frac{-x}{s}}{3}\right)}}} \]
  5. Applied rewrites99.8%

    \[\leadsto \frac{1}{1 + \color{blue}{{\left(\left(e \cdot e\right) \cdot e\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}}} \]
  6. Step-by-step derivation
    1. lift-pow.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{{\left(\left(e \cdot e\right) \cdot e\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}}} \]
    2. lift-*.f32N/A

      \[\leadsto \frac{1}{1 + {\color{blue}{\left(\left(e \cdot e\right) \cdot e\right)}}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    3. unpow-prod-downN/A

      \[\leadsto \frac{1}{1 + \color{blue}{{\left(e \cdot e\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}}} \]
    4. lower-unsound-*.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{{\left(e \cdot e\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}}} \]
    5. lower-unsound-pow.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{{\left(e \cdot e\right)}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    6. lift-/.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\color{blue}{\left(\frac{\frac{-x}{s}}{3}\right)}} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    7. frac-2negN/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\color{blue}{\left(\frac{\mathsf{neg}\left(\frac{-x}{s}\right)}{\mathsf{neg}\left(3\right)}\right)}} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    8. mult-flipN/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\color{blue}{\left(\left(\mathsf{neg}\left(\frac{-x}{s}\right)\right) \cdot \frac{1}{\mathsf{neg}\left(3\right)}\right)}} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    9. lift-/.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\left(\mathsf{neg}\left(\color{blue}{\frac{-x}{s}}\right)\right) \cdot \frac{1}{\mathsf{neg}\left(3\right)}\right)} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    10. distribute-neg-frac2N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\color{blue}{\frac{-x}{\mathsf{neg}\left(s\right)}} \cdot \frac{1}{\mathsf{neg}\left(3\right)}\right)} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    11. lift-neg.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{\color{blue}{\mathsf{neg}\left(x\right)}}{\mathsf{neg}\left(s\right)} \cdot \frac{1}{\mathsf{neg}\left(3\right)}\right)} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    12. frac-2negN/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\color{blue}{\frac{x}{s}} \cdot \frac{1}{\mathsf{neg}\left(3\right)}\right)} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    13. lift-/.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\color{blue}{\frac{x}{s}} \cdot \frac{1}{\mathsf{neg}\left(3\right)}\right)} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    14. lower-*.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\color{blue}{\left(\frac{x}{s} \cdot \frac{1}{\mathsf{neg}\left(3\right)}\right)}} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    15. metadata-evalN/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{1}{\color{blue}{-3}}\right)} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    16. metadata-evalN/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \color{blue}{\frac{-1}{3}}\right)} \cdot {e}^{\left(\frac{\frac{-x}{s}}{3}\right)}} \]
    17. lower-unsound-pow.f3299.8%

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot \color{blue}{{e}^{\left(\frac{\frac{-x}{s}}{3}\right)}}} \]
  7. Applied rewrites99.8%

    \[\leadsto \frac{1}{1 + \color{blue}{{\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {e}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)}}} \]
  8. Step-by-step derivation
    1. lift-pow.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot \color{blue}{{e}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)}}} \]
    2. lift-E.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\color{blue}{\mathsf{E}\left(\right)}}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)}} \]
    3. add-sqr-sqrtN/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\color{blue}{\left(\sqrt{\mathsf{E}\left(\right)} \cdot \sqrt{\mathsf{E}\left(\right)}\right)}}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)}} \]
    4. lift-E.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\left(\sqrt{\color{blue}{e}} \cdot \sqrt{\mathsf{E}\left(\right)}\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)}} \]
    5. lift-E.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\left(\sqrt{e} \cdot \sqrt{\color{blue}{e}}\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)}} \]
    6. sqrt-prodN/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\color{blue}{\left(\sqrt{e \cdot e}\right)}}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)}} \]
    7. lift-*.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\left(\sqrt{\color{blue}{e \cdot e}}\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)}} \]
    8. sqrt-pow2N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot \color{blue}{{\left(e \cdot e\right)}^{\left(\frac{\frac{x}{s} \cdot \frac{-1}{3}}{2}\right)}}} \]
    9. lower-pow.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot \color{blue}{{\left(e \cdot e\right)}^{\left(\frac{\frac{x}{s} \cdot \frac{-1}{3}}{2}\right)}}} \]
    10. lower-/.f3299.8%

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\left(e \cdot e\right)}^{\color{blue}{\left(\frac{\frac{x}{s} \cdot \frac{-1}{3}}{2}\right)}}} \]
    11. lift-*.f32N/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\left(e \cdot e\right)}^{\left(\frac{\color{blue}{\frac{x}{s} \cdot \frac{-1}{3}}}{2}\right)}} \]
    12. *-commutativeN/A

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\left(e \cdot e\right)}^{\left(\frac{\color{blue}{\frac{-1}{3} \cdot \frac{x}{s}}}{2}\right)}} \]
    13. lower-*.f3299.8%

      \[\leadsto \frac{1}{1 + {\left(e \cdot e\right)}^{\left(\frac{x}{s} \cdot \frac{-1}{3}\right)} \cdot {\left(e \cdot e\right)}^{\left(\frac{\color{blue}{\frac{-1}{3} \cdot \frac{x}{s}}}{2}\right)}} \]
  9. Applied rewrites99.8%

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

Alternative 2: 49.5% accurate, 0.8× speedup?

\[\begin{array}{l} \mathbf{if}\;1 + e^{\frac{-x}{s}} \leq \frac{3}{2}:\\ \;\;\;\;\frac{1}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{\left(2 - \frac{x}{s}\right) \cdot 2}\\ \end{array} \]
(FPCore (x s)
  :precision binary32
  (if (<= (+ 1 (exp (/ (- x) s))) 3/2) 1/2 (/ 2 (* (- 2 (/ x s)) 2))))
float code(float x, float s) {
	float tmp;
	if ((1.0f + expf((-x / s))) <= 1.5f) {
		tmp = 0.5f;
	} else {
		tmp = 2.0f / ((2.0f - (x / s)) * 2.0f);
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if ((1.0e0 + exp((-x / s))) <= 1.5e0) then
        tmp = 0.5e0
    else
        tmp = 2.0e0 / ((2.0e0 - (x / s)) * 2.0e0)
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) <= Float32(1.5))
		tmp = Float32(0.5);
	else
		tmp = Float32(Float32(2.0) / Float32(Float32(Float32(2.0) - Float32(x / s)) * Float32(2.0)));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if ((single(1.0) + exp((-x / s))) <= single(1.5))
		tmp = single(0.5);
	else
		tmp = single(2.0) / ((single(2.0) - (x / s)) * single(2.0));
	end
	tmp_2 = tmp;
end
\begin{array}{l}
\mathbf{if}\;1 + e^{\frac{-x}{s}} \leq \frac{3}{2}:\\
\;\;\;\;\frac{1}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{2}{\left(2 - \frac{x}{s}\right) \cdot 2}\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s))) < 1.5

    1. Initial program 99.8%

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

      \[\leadsto \color{blue}{\frac{1}{2}} \]
    3. Step-by-step derivation
      1. Applied rewrites34.8%

        \[\leadsto \color{blue}{\frac{1}{2}} \]

      if 1.5 < (+.f32 #s(literal 1 binary32) (exp.f32 (/.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}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
      3. Step-by-step derivation
        1. lower-+.f32N/A

          \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \frac{x}{s}}} \]
        2. lower-*.f32N/A

          \[\leadsto \frac{1}{2 + -1 \cdot \color{blue}{\frac{x}{s}}} \]
        3. lower-/.f3240.9%

          \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
      4. Applied rewrites40.9%

        \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
      5. Step-by-step derivation
        1. lift-/.f32N/A

          \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
        2. mult-flipN/A

          \[\leadsto \frac{1}{2 + -1 \cdot \left(x \cdot \color{blue}{\frac{1}{s}}\right)} \]
        3. *-commutativeN/A

          \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
        4. lower-*.f32N/A

          \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
        5. lower-/.f3240.9%

          \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot x\right)} \]
      6. Applied rewrites40.9%

        \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
      7. Step-by-step derivation
        1. lift-/.f32N/A

          \[\leadsto \color{blue}{\frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot x\right)}} \]
        2. metadata-evalN/A

          \[\leadsto \frac{\color{blue}{1 \cdot 1}}{2 + -1 \cdot \left(\frac{1}{s} \cdot x\right)} \]
        3. lift-+.f32N/A

          \[\leadsto \frac{1 \cdot 1}{2 + \color{blue}{-1 \cdot \left(\frac{1}{s} \cdot x\right)}} \]
        4. +-commutativeN/A

          \[\leadsto \frac{1 \cdot 1}{-1 \cdot \left(\frac{1}{s} \cdot x\right) + \color{blue}{2}} \]
        5. lift-*.f32N/A

          \[\leadsto \frac{1 \cdot 1}{-1 \cdot \left(\frac{1}{s} \cdot x\right) + 2} \]
        6. mul-1-negN/A

          \[\leadsto \frac{1 \cdot 1}{\left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right) + 2} \]
        7. lift-*.f32N/A

          \[\leadsto \frac{1 \cdot 1}{\left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right) + 2} \]
        8. *-commutativeN/A

          \[\leadsto \frac{1 \cdot 1}{\left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right) + 2} \]
        9. lift-/.f32N/A

          \[\leadsto \frac{1 \cdot 1}{\left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right) + 2} \]
        10. mult-flipN/A

          \[\leadsto \frac{1 \cdot 1}{\left(\mathsf{neg}\left(\frac{x}{s}\right)\right) + 2} \]
        11. distribute-frac-negN/A

          \[\leadsto \frac{1 \cdot 1}{\frac{\mathsf{neg}\left(x\right)}{s} + 2} \]
        12. lift-neg.f32N/A

          \[\leadsto \frac{1 \cdot 1}{\frac{-x}{s} + 2} \]
        13. lift-/.f32N/A

          \[\leadsto \frac{1 \cdot 1}{\frac{-x}{s} + 2} \]
        14. sum-to-mult-revN/A

          \[\leadsto \frac{1 \cdot 1}{\left(1 + \frac{2}{\frac{-x}{s}}\right) \cdot \color{blue}{\frac{-x}{s}}} \]
        15. lift-/.f32N/A

          \[\leadsto \frac{1 \cdot 1}{\left(1 + \frac{2}{\frac{-x}{s}}\right) \cdot \frac{-x}{s}} \]
        16. lift-+.f32N/A

          \[\leadsto \frac{1 \cdot 1}{\left(1 + \frac{2}{\frac{-x}{s}}\right) \cdot \frac{\color{blue}{-x}}{s}} \]
        17. lift-*.f32N/A

          \[\leadsto \frac{1 \cdot 1}{\left(1 + \frac{2}{\frac{-x}{s}}\right) \cdot \color{blue}{\frac{-x}{s}}} \]
      8. Applied rewrites41.0%

        \[\leadsto \color{blue}{\frac{2}{\left(2 - \frac{x}{s}\right) \cdot 2}} \]
    4. Recombined 2 regimes into one program.
    5. Add Preprocessing

    Alternative 3: 49.4% accurate, 0.9× speedup?

    \[\begin{array}{l} \mathbf{if}\;1 + e^{\frac{-x}{s}} \leq \frac{3}{2}:\\ \;\;\;\;\frac{1}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2 - \frac{x}{s}}\\ \end{array} \]
    (FPCore (x s)
      :precision binary32
      (if (<= (+ 1 (exp (/ (- x) s))) 3/2) 1/2 (/ 1 (- 2 (/ x s)))))
    float code(float x, float s) {
    	float tmp;
    	if ((1.0f + expf((-x / s))) <= 1.5f) {
    		tmp = 0.5f;
    	} else {
    		tmp = 1.0f / (2.0f - (x / s));
    	}
    	return tmp;
    }
    
    module fmin_fmax_functions
        implicit none
        private
        public fmax
        public fmin
    
        interface fmax
            module procedure fmax88
            module procedure fmax44
            module procedure fmax84
            module procedure fmax48
        end interface
        interface fmin
            module procedure fmin88
            module procedure fmin44
            module procedure fmin84
            module procedure fmin48
        end interface
    contains
        real(8) function fmax88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(4) function fmax44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(8) function fmax84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmax48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
        end function
        real(8) function fmin88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(4) function fmin44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(8) function fmin84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmin48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
        end function
    end module
    
    real(4) function code(x, s)
    use fmin_fmax_functions
        real(4), intent (in) :: x
        real(4), intent (in) :: s
        real(4) :: tmp
        if ((1.0e0 + exp((-x / s))) <= 1.5e0) then
            tmp = 0.5e0
        else
            tmp = 1.0e0 / (2.0e0 - (x / s))
        end if
        code = tmp
    end function
    
    function code(x, s)
    	tmp = Float32(0.0)
    	if (Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) <= Float32(1.5))
    		tmp = Float32(0.5);
    	else
    		tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(x / s)));
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, s)
    	tmp = single(0.0);
    	if ((single(1.0) + exp((-x / s))) <= single(1.5))
    		tmp = single(0.5);
    	else
    		tmp = single(1.0) / (single(2.0) - (x / s));
    	end
    	tmp_2 = tmp;
    end
    
    \begin{array}{l}
    \mathbf{if}\;1 + e^{\frac{-x}{s}} \leq \frac{3}{2}:\\
    \;\;\;\;\frac{1}{2}\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s))) < 1.5

      1. Initial program 99.8%

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

        \[\leadsto \color{blue}{\frac{1}{2}} \]
      3. Step-by-step derivation
        1. Applied rewrites34.8%

          \[\leadsto \color{blue}{\frac{1}{2}} \]

        if 1.5 < (+.f32 #s(literal 1 binary32) (exp.f32 (/.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}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
        3. Step-by-step derivation
          1. lower-+.f32N/A

            \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \frac{x}{s}}} \]
          2. lower-*.f32N/A

            \[\leadsto \frac{1}{2 + -1 \cdot \color{blue}{\frac{x}{s}}} \]
          3. lower-/.f3240.9%

            \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
        4. Applied rewrites40.9%

          \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
        5. Step-by-step derivation
          1. lift-/.f32N/A

            \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
          2. mult-flipN/A

            \[\leadsto \frac{1}{2 + -1 \cdot \left(x \cdot \color{blue}{\frac{1}{s}}\right)} \]
          3. *-commutativeN/A

            \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
          4. lower-*.f32N/A

            \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
          5. lower-/.f3240.9%

            \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot x\right)} \]
        6. Applied rewrites40.9%

          \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
        7. Step-by-step derivation
          1. lift-+.f32N/A

            \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \left(\frac{1}{s} \cdot x\right)}} \]
          2. lift-*.f32N/A

            \[\leadsto \frac{1}{2 + -1 \cdot \color{blue}{\left(\frac{1}{s} \cdot x\right)}} \]
          3. mul-1-negN/A

            \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right)} \]
          4. lift-*.f32N/A

            \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right)} \]
          5. *-commutativeN/A

            \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right)} \]
          6. lift-/.f32N/A

            \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right)} \]
          7. mult-flipN/A

            \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{x}{s}\right)\right)} \]
          8. lift-/.f32N/A

            \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{x}{s}\right)\right)} \]
          9. sub-flip-reverseN/A

            \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
          10. lower--.f3240.9%

            \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
        8. Applied rewrites40.9%

          \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
      4. Recombined 2 regimes into one program.
      5. Add Preprocessing

      Alternative 4: 48.7% accurate, 1.2× speedup?

      \[\begin{array}{l} t_0 := \frac{-x}{s}\\ \mathbf{if}\;t\_0 \leq 200:\\ \;\;\;\;\frac{1}{2}\\ \mathbf{elif}\;t\_0 \leq \infty:\\ \;\;\;\;\frac{1}{\frac{\frac{\left(2 \cdot \left(-s\right) + x\right) \cdot x}{s \cdot s}}{t\_0}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2 - \frac{x}{s}}\\ \end{array} \]
      (FPCore (x s)
        :precision binary32
        (let* ((t_0 (/ (- x) s)))
        (if (<= t_0 200)
          1/2
          (if (<= t_0 INFINITY)
            (/ 1 (/ (/ (* (+ (* 2 (- s)) x) x) (* s s)) t_0))
            (/ 1 (- 2 (/ x s)))))))
      float code(float x, float s) {
      	float t_0 = -x / s;
      	float tmp;
      	if (t_0 <= 200.0f) {
      		tmp = 0.5f;
      	} else if (t_0 <= ((float) INFINITY)) {
      		tmp = 1.0f / (((((2.0f * -s) + x) * x) / (s * s)) / t_0);
      	} else {
      		tmp = 1.0f / (2.0f - (x / s));
      	}
      	return tmp;
      }
      
      function code(x, s)
      	t_0 = Float32(Float32(-x) / s)
      	tmp = Float32(0.0)
      	if (t_0 <= Float32(200.0))
      		tmp = Float32(0.5);
      	elseif (t_0 <= Float32(Inf))
      		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(Float32(Float32(Float32(2.0) * Float32(-s)) + x) * x) / Float32(s * s)) / t_0));
      	else
      		tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(x / s)));
      	end
      	return tmp
      end
      
      function tmp_2 = code(x, s)
      	t_0 = -x / s;
      	tmp = single(0.0);
      	if (t_0 <= single(200.0))
      		tmp = single(0.5);
      	elseif (t_0 <= single(Inf))
      		tmp = single(1.0) / (((((single(2.0) * -s) + x) * x) / (s * s)) / t_0);
      	else
      		tmp = single(1.0) / (single(2.0) - (x / s));
      	end
      	tmp_2 = tmp;
      end
      
      \begin{array}{l}
      t_0 := \frac{-x}{s}\\
      \mathbf{if}\;t\_0 \leq 200:\\
      \;\;\;\;\frac{1}{2}\\
      
      \mathbf{elif}\;t\_0 \leq \infty:\\
      \;\;\;\;\frac{1}{\frac{\frac{\left(2 \cdot \left(-s\right) + x\right) \cdot x}{s \cdot s}}{t\_0}}\\
      
      \mathbf{else}:\\
      \;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
      
      
      \end{array}
      
      Derivation
      1. Split input into 3 regimes
      2. if (/.f32 (neg.f32 x) s) < 200

        1. Initial program 99.8%

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

          \[\leadsto \color{blue}{\frac{1}{2}} \]
        3. Step-by-step derivation
          1. Applied rewrites34.8%

            \[\leadsto \color{blue}{\frac{1}{2}} \]

          if 200 < (/.f32 (neg.f32 x) s) < +inf.0

          1. Initial program 99.8%

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

            \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
          3. Step-by-step derivation
            1. lower-+.f32N/A

              \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \frac{x}{s}}} \]
            2. lower-*.f32N/A

              \[\leadsto \frac{1}{2 + -1 \cdot \color{blue}{\frac{x}{s}}} \]
            3. lower-/.f3240.9%

              \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
          4. Applied rewrites40.9%

            \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
          5. Step-by-step derivation
            1. lift-/.f32N/A

              \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
            2. mult-flipN/A

              \[\leadsto \frac{1}{2 + -1 \cdot \left(x \cdot \color{blue}{\frac{1}{s}}\right)} \]
            3. *-commutativeN/A

              \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
            4. lower-*.f32N/A

              \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
            5. lower-/.f3240.9%

              \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot x\right)} \]
          6. Applied rewrites40.9%

            \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
          7. Applied rewrites32.3%

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

          if +inf.0 < (/.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}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
          3. Step-by-step derivation
            1. lower-+.f32N/A

              \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \frac{x}{s}}} \]
            2. lower-*.f32N/A

              \[\leadsto \frac{1}{2 + -1 \cdot \color{blue}{\frac{x}{s}}} \]
            3. lower-/.f3240.9%

              \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
          4. Applied rewrites40.9%

            \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
          5. Step-by-step derivation
            1. lift-/.f32N/A

              \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
            2. mult-flipN/A

              \[\leadsto \frac{1}{2 + -1 \cdot \left(x \cdot \color{blue}{\frac{1}{s}}\right)} \]
            3. *-commutativeN/A

              \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
            4. lower-*.f32N/A

              \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
            5. lower-/.f3240.9%

              \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot x\right)} \]
          6. Applied rewrites40.9%

            \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
          7. Step-by-step derivation
            1. lift-+.f32N/A

              \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \left(\frac{1}{s} \cdot x\right)}} \]
            2. lift-*.f32N/A

              \[\leadsto \frac{1}{2 + -1 \cdot \color{blue}{\left(\frac{1}{s} \cdot x\right)}} \]
            3. mul-1-negN/A

              \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right)} \]
            4. lift-*.f32N/A

              \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right)} \]
            5. *-commutativeN/A

              \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right)} \]
            6. lift-/.f32N/A

              \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right)} \]
            7. mult-flipN/A

              \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{x}{s}\right)\right)} \]
            8. lift-/.f32N/A

              \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{x}{s}\right)\right)} \]
            9. sub-flip-reverseN/A

              \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
            10. lower--.f3240.9%

              \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
          8. Applied rewrites40.9%

            \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
        4. Recombined 3 regimes into one program.
        5. Add Preprocessing

        Alternative 5: 41.6% accurate, 1.3× speedup?

        \[\begin{array}{l} t_0 := \frac{-x}{s}\\ \mathbf{if}\;t\_0 \leq -2:\\ \;\;\;\;\frac{1}{2}\\ \mathbf{elif}\;t\_0 \leq \infty:\\ \;\;\;\;\frac{1}{\frac{\left(\frac{x}{s} + -2\right) \cdot \left(-x\right)}{\frac{x}{s} \cdot s}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2 - \frac{x}{s}}\\ \end{array} \]
        (FPCore (x s)
          :precision binary32
          (let* ((t_0 (/ (- x) s)))
          (if (<= t_0 -2)
            1/2
            (if (<= t_0 INFINITY)
              (/ 1 (/ (* (+ (/ x s) -2) (- x)) (* (/ x s) s)))
              (/ 1 (- 2 (/ x s)))))))
        float code(float x, float s) {
        	float t_0 = -x / s;
        	float tmp;
        	if (t_0 <= -2.0f) {
        		tmp = 0.5f;
        	} else if (t_0 <= ((float) INFINITY)) {
        		tmp = 1.0f / ((((x / s) + -2.0f) * -x) / ((x / s) * s));
        	} else {
        		tmp = 1.0f / (2.0f - (x / s));
        	}
        	return tmp;
        }
        
        function code(x, s)
        	t_0 = Float32(Float32(-x) / s)
        	tmp = Float32(0.0)
        	if (t_0 <= Float32(-2.0))
        		tmp = Float32(0.5);
        	elseif (t_0 <= Float32(Inf))
        		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(Float32(x / s) + Float32(-2.0)) * Float32(-x)) / Float32(Float32(x / s) * s)));
        	else
        		tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(x / s)));
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, s)
        	t_0 = -x / s;
        	tmp = single(0.0);
        	if (t_0 <= single(-2.0))
        		tmp = single(0.5);
        	elseif (t_0 <= single(Inf))
        		tmp = single(1.0) / ((((x / s) + single(-2.0)) * -x) / ((x / s) * s));
        	else
        		tmp = single(1.0) / (single(2.0) - (x / s));
        	end
        	tmp_2 = tmp;
        end
        
        \begin{array}{l}
        t_0 := \frac{-x}{s}\\
        \mathbf{if}\;t\_0 \leq -2:\\
        \;\;\;\;\frac{1}{2}\\
        
        \mathbf{elif}\;t\_0 \leq \infty:\\
        \;\;\;\;\frac{1}{\frac{\left(\frac{x}{s} + -2\right) \cdot \left(-x\right)}{\frac{x}{s} \cdot s}}\\
        
        \mathbf{else}:\\
        \;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
        
        
        \end{array}
        
        Derivation
        1. Split input into 3 regimes
        2. if (/.f32 (neg.f32 x) s) < -2

          1. Initial program 99.8%

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

            \[\leadsto \color{blue}{\frac{1}{2}} \]
          3. Step-by-step derivation
            1. Applied rewrites34.8%

              \[\leadsto \color{blue}{\frac{1}{2}} \]

            if -2 < (/.f32 (neg.f32 x) s) < +inf.0

            1. Initial program 99.8%

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

              \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
            3. Step-by-step derivation
              1. lower-+.f32N/A

                \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \frac{x}{s}}} \]
              2. lower-*.f32N/A

                \[\leadsto \frac{1}{2 + -1 \cdot \color{blue}{\frac{x}{s}}} \]
              3. lower-/.f3240.9%

                \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
            4. Applied rewrites40.9%

              \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
            5. Step-by-step derivation
              1. lift-/.f32N/A

                \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
              2. mult-flipN/A

                \[\leadsto \frac{1}{2 + -1 \cdot \left(x \cdot \color{blue}{\frac{1}{s}}\right)} \]
              3. *-commutativeN/A

                \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
              4. lower-*.f32N/A

                \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
              5. lower-/.f3240.9%

                \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot x\right)} \]
            6. Applied rewrites40.9%

              \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
            7. Step-by-step derivation
              1. lift-+.f32N/A

                \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \left(\frac{1}{s} \cdot x\right)}} \]
              2. +-commutativeN/A

                \[\leadsto \frac{1}{-1 \cdot \left(\frac{1}{s} \cdot x\right) + \color{blue}{2}} \]
              3. lift-*.f32N/A

                \[\leadsto \frac{1}{-1 \cdot \left(\frac{1}{s} \cdot x\right) + 2} \]
              4. mul-1-negN/A

                \[\leadsto \frac{1}{\left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right) + 2} \]
              5. lift-*.f32N/A

                \[\leadsto \frac{1}{\left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right) + 2} \]
              6. *-commutativeN/A

                \[\leadsto \frac{1}{\left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right) + 2} \]
              7. lift-/.f32N/A

                \[\leadsto \frac{1}{\left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right) + 2} \]
              8. mult-flipN/A

                \[\leadsto \frac{1}{\left(\mathsf{neg}\left(\frac{x}{s}\right)\right) + 2} \]
              9. distribute-frac-negN/A

                \[\leadsto \frac{1}{\frac{\mathsf{neg}\left(x\right)}{s} + 2} \]
              10. lift-neg.f32N/A

                \[\leadsto \frac{1}{\frac{-x}{s} + 2} \]
              11. lift-/.f32N/A

                \[\leadsto \frac{1}{\frac{-x}{s} + 2} \]
              12. sum-to-mult-revN/A

                \[\leadsto \frac{1}{\left(1 + \frac{2}{\frac{-x}{s}}\right) \cdot \color{blue}{\frac{-x}{s}}} \]
              13. lift-/.f32N/A

                \[\leadsto \frac{1}{\left(1 + \frac{2}{\frac{-x}{s}}\right) \cdot \frac{-x}{s}} \]
              14. lift-+.f32N/A

                \[\leadsto \frac{1}{\left(1 + \frac{2}{\frac{-x}{s}}\right) \cdot \frac{\color{blue}{-x}}{s}} \]
            8. Applied rewrites32.3%

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

            if +inf.0 < (/.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}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
            3. Step-by-step derivation
              1. lower-+.f32N/A

                \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \frac{x}{s}}} \]
              2. lower-*.f32N/A

                \[\leadsto \frac{1}{2 + -1 \cdot \color{blue}{\frac{x}{s}}} \]
              3. lower-/.f3240.9%

                \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
            4. Applied rewrites40.9%

              \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
            5. Step-by-step derivation
              1. lift-/.f32N/A

                \[\leadsto \frac{1}{2 + -1 \cdot \frac{x}{\color{blue}{s}}} \]
              2. mult-flipN/A

                \[\leadsto \frac{1}{2 + -1 \cdot \left(x \cdot \color{blue}{\frac{1}{s}}\right)} \]
              3. *-commutativeN/A

                \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
              4. lower-*.f32N/A

                \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
              5. lower-/.f3240.9%

                \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot x\right)} \]
            6. Applied rewrites40.9%

              \[\leadsto \frac{1}{2 + -1 \cdot \left(\frac{1}{s} \cdot \color{blue}{x}\right)} \]
            7. Step-by-step derivation
              1. lift-+.f32N/A

                \[\leadsto \frac{1}{2 + \color{blue}{-1 \cdot \left(\frac{1}{s} \cdot x\right)}} \]
              2. lift-*.f32N/A

                \[\leadsto \frac{1}{2 + -1 \cdot \color{blue}{\left(\frac{1}{s} \cdot x\right)}} \]
              3. mul-1-negN/A

                \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right)} \]
              4. lift-*.f32N/A

                \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{1}{s} \cdot x\right)\right)} \]
              5. *-commutativeN/A

                \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right)} \]
              6. lift-/.f32N/A

                \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(x \cdot \frac{1}{s}\right)\right)} \]
              7. mult-flipN/A

                \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{x}{s}\right)\right)} \]
              8. lift-/.f32N/A

                \[\leadsto \frac{1}{2 + \left(\mathsf{neg}\left(\frac{x}{s}\right)\right)} \]
              9. sub-flip-reverseN/A

                \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
              10. lower--.f3240.9%

                \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
            8. Applied rewrites40.9%

              \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
          4. Recombined 3 regimes into one program.
          5. Add Preprocessing

          Alternative 6: 34.8% accurate, 128.0× speedup?

          \[\frac{1}{2} \]
          (FPCore (x s)
            :precision binary32
            1/2)
          float code(float x, float s) {
          	return 0.5f;
          }
          
          module fmin_fmax_functions
              implicit none
              private
              public fmax
              public fmin
          
              interface fmax
                  module procedure fmax88
                  module procedure fmax44
                  module procedure fmax84
                  module procedure fmax48
              end interface
              interface fmin
                  module procedure fmin88
                  module procedure fmin44
                  module procedure fmin84
                  module procedure fmin48
              end interface
          contains
              real(8) function fmax88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(4) function fmax44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(8) function fmax84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmax48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
              end function
              real(8) function fmin88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(4) function fmin44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(8) function fmin84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmin48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
              end function
          end module
          
          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
          
          \frac{1}{2}
          
          Derivation
          1. Initial program 99.8%

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

            \[\leadsto \color{blue}{\frac{1}{2}} \]
          3. Step-by-step derivation
            1. Applied rewrites34.8%

              \[\leadsto \color{blue}{\frac{1}{2}} \]
            2. Add Preprocessing

            Reproduce

            ?
            herbie shell --seed 2025271 -o generate:evaluate
            (FPCore (x s)
              :name "Logistic function"
              :precision binary32
              :pre (and (<= 0 s) (<= s 10651631/10000000))
              (/ 1 (+ 1 (exp (/ (- x) s)))))