Data.Random.Distribution.T:$ccdf from random-fu-0.2.6.2

Percentage Accurate: 99.9% → 100.0%
Time: 1.2s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\frac{x + y}{y + y} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (/ (+ x y) (+ y y)))
double code(double x, double y) {
	return (x + y) / (y + y);
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + y) / (y + y)
end function
public static double code(double x, double y) {
	return (x + y) / (y + y);
}
def code(x, y):
	return (x + y) / (y + y)
function code(x, y)
	return Float64(Float64(x + y) / Float64(y + y))
end
function tmp = code(x, y)
	tmp = (x + y) / (y + y);
end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(y + y), $MachinePrecision]), $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	(x + y) / (y + y)
END code
\frac{x + y}{y + y}

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 3 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.9% accurate, 1.0× speedup?

\[\frac{x + y}{y + y} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (/ (+ x y) (+ y y)))
double code(double x, double y) {
	return (x + y) / (y + y);
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + y) / (y + y)
end function
public static double code(double x, double y) {
	return (x + y) / (y + y);
}
def code(x, y):
	return (x + y) / (y + y)
function code(x, y)
	return Float64(Float64(x + y) / Float64(y + y))
end
function tmp = code(x, y)
	tmp = (x + y) / (y + y);
end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(y + y), $MachinePrecision]), $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	(x + y) / (y + y)
END code
\frac{x + y}{y + y}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\mathsf{fma}\left(\frac{x}{y}, 0.5, 0.5\right) \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (fma (/ x y) 0.5 0.5))
double code(double x, double y) {
	return fma((x / y), 0.5, 0.5);
}
function code(x, y)
	return fma(Float64(x / y), 0.5, 0.5)
end
code[x_, y_] := N[(N[(x / y), $MachinePrecision] * 0.5 + 0.5), $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	((x / y) * (5e-1)) + (5e-1)
END code
\mathsf{fma}\left(\frac{x}{y}, 0.5, 0.5\right)
Derivation
  1. Initial program 99.9%

    \[\frac{x + y}{y + y} \]
  2. Step-by-step derivation
    1. Applied rewrites100.0%

      \[\leadsto \mathsf{fma}\left(\frac{x}{y}, 0.5, 0.5\right) \]
    2. Add Preprocessing

    Alternative 2: 97.7% accurate, 0.3× speedup?

    \[\begin{array}{l} t_0 := \frac{x + y}{y + y}\\ t_1 := \frac{x}{y + y}\\ \mathbf{if}\;t\_0 \leq -5:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_0 \leq 1:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \]
    (FPCore (x y)
      :precision binary64
      :pre TRUE
      (let* ((t_0 (/ (+ x y) (+ y y))) (t_1 (/ x (+ y y))))
      (if (<= t_0 -5.0) t_1 (if (<= t_0 1.0) 0.5 t_1))))
    double code(double x, double y) {
    	double t_0 = (x + y) / (y + y);
    	double t_1 = x / (y + y);
    	double tmp;
    	if (t_0 <= -5.0) {
    		tmp = t_1;
    	} else if (t_0 <= 1.0) {
    		tmp = 0.5;
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    real(8) function code(x, y)
    use fmin_fmax_functions
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8) :: t_0
        real(8) :: t_1
        real(8) :: tmp
        t_0 = (x + y) / (y + y)
        t_1 = x / (y + y)
        if (t_0 <= (-5.0d0)) then
            tmp = t_1
        else if (t_0 <= 1.0d0) then
            tmp = 0.5d0
        else
            tmp = t_1
        end if
        code = tmp
    end function
    
    public static double code(double x, double y) {
    	double t_0 = (x + y) / (y + y);
    	double t_1 = x / (y + y);
    	double tmp;
    	if (t_0 <= -5.0) {
    		tmp = t_1;
    	} else if (t_0 <= 1.0) {
    		tmp = 0.5;
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    def code(x, y):
    	t_0 = (x + y) / (y + y)
    	t_1 = x / (y + y)
    	tmp = 0
    	if t_0 <= -5.0:
    		tmp = t_1
    	elif t_0 <= 1.0:
    		tmp = 0.5
    	else:
    		tmp = t_1
    	return tmp
    
    function code(x, y)
    	t_0 = Float64(Float64(x + y) / Float64(y + y))
    	t_1 = Float64(x / Float64(y + y))
    	tmp = 0.0
    	if (t_0 <= -5.0)
    		tmp = t_1;
    	elseif (t_0 <= 1.0)
    		tmp = 0.5;
    	else
    		tmp = t_1;
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y)
    	t_0 = (x + y) / (y + y);
    	t_1 = x / (y + y);
    	tmp = 0.0;
    	if (t_0 <= -5.0)
    		tmp = t_1;
    	elseif (t_0 <= 1.0)
    		tmp = 0.5;
    	else
    		tmp = t_1;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_] := Block[{t$95$0 = N[(N[(x + y), $MachinePrecision] / N[(y + y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(x / N[(y + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -5.0], t$95$1, If[LessEqual[t$95$0, 1.0], 0.5, t$95$1]]]]
    
    f(x, y):
    	x in [-inf, +inf],
    	y in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x, y: real): real =
    	LET t_0 = ((x + y) / (y + y)) IN
    		LET t_1 = (x / (y + y)) IN
    			LET tmp_1 = IF (t_0 <= (1)) THEN (5e-1) ELSE t_1 ENDIF IN
    			LET tmp = IF (t_0 <= (-5)) THEN t_1 ELSE tmp_1 ENDIF IN
    	tmp
    END code
    \begin{array}{l}
    t_0 := \frac{x + y}{y + y}\\
    t_1 := \frac{x}{y + y}\\
    \mathbf{if}\;t\_0 \leq -5:\\
    \;\;\;\;t\_1\\
    
    \mathbf{elif}\;t\_0 \leq 1:\\
    \;\;\;\;0.5\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_1\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (/.f64 (+.f64 x y) (+.f64 y y)) < -5 or 1 < (/.f64 (+.f64 x y) (+.f64 y y))

      1. Initial program 99.9%

        \[\frac{x + y}{y + y} \]
      2. Taylor expanded in x around inf

        \[\leadsto \frac{1}{2} \cdot \frac{x}{y} \]
      3. Step-by-step derivation
        1. Applied rewrites49.5%

          \[\leadsto 0.5 \cdot \frac{x}{y} \]
        2. Step-by-step derivation
          1. Applied rewrites49.5%

            \[\leadsto \frac{x}{y + y} \]

          if -5 < (/.f64 (+.f64 x y) (+.f64 y y)) < 1

          1. Initial program 99.9%

            \[\frac{x + y}{y + y} \]
          2. Taylor expanded in x around 0

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

              \[\leadsto 0.5 \]
          4. Recombined 2 regimes into one program.
          5. Add Preprocessing

          Alternative 3: 51.6% accurate, 10.0× speedup?

          \[0.5 \]
          (FPCore (x y)
            :precision binary64
            :pre TRUE
            0.5)
          double code(double x, double y) {
          	return 0.5;
          }
          
          real(8) function code(x, y)
          use fmin_fmax_functions
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              code = 0.5d0
          end function
          
          public static double code(double x, double y) {
          	return 0.5;
          }
          
          def code(x, y):
          	return 0.5
          
          function code(x, y)
          	return 0.5
          end
          
          function tmp = code(x, y)
          	tmp = 0.5;
          end
          
          code[x_, y_] := 0.5
          
          f(x, y):
          	x in [-inf, +inf],
          	y in [-inf, +inf]
          code: THEORY
          BEGIN
          f(x, y: real): real =
          	5e-1
          END code
          0.5
          
          Derivation
          1. Initial program 99.9%

            \[\frac{x + y}{y + y} \]
          2. Taylor expanded in x around 0

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

              \[\leadsto 0.5 \]
            2. Add Preprocessing

            Reproduce

            ?
            herbie shell --seed 2026092 
            (FPCore (x y)
              :name "Data.Random.Distribution.T:$ccdf from random-fu-0.2.6.2"
              :precision binary64
              (/ (+ x y) (+ y y)))