Data.Random.Distribution.Triangular:triangularCDF from random-fu-0.2.6.2, A

Percentage Accurate: 99.2% → 99.2%
Time: 8.5s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ 1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)} \end{array} \]
(FPCore (x y z t) :precision binary64 (- 1.0 (/ x (* (- y z) (- y t)))))
double code(double x, double y, double z, double t) {
	return 1.0 - (x / ((y - z) * (y - t)));
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = 1.0d0 - (x / ((y - z) * (y - t)))
end function
public static double code(double x, double y, double z, double t) {
	return 1.0 - (x / ((y - z) * (y - t)));
}
def code(x, y, z, t):
	return 1.0 - (x / ((y - z) * (y - t)))
function code(x, y, z, t)
	return Float64(1.0 - Float64(x / Float64(Float64(y - z) * Float64(y - t))))
end
function tmp = code(x, y, z, t)
	tmp = 1.0 - (x / ((y - z) * (y - t)));
end
code[x_, y_, z_, t_] := N[(1.0 - N[(x / N[(N[(y - z), $MachinePrecision] * N[(y - t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 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.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)} \end{array} \]
(FPCore (x y z t) :precision binary64 (- 1.0 (/ x (* (- y z) (- y t)))))
double code(double x, double y, double z, double t) {
	return 1.0 - (x / ((y - z) * (y - t)));
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = 1.0d0 - (x / ((y - z) * (y - t)))
end function
public static double code(double x, double y, double z, double t) {
	return 1.0 - (x / ((y - z) * (y - t)));
}
def code(x, y, z, t):
	return 1.0 - (x / ((y - z) * (y - t)))
function code(x, y, z, t)
	return Float64(1.0 - Float64(x / Float64(Float64(y - z) * Float64(y - t))))
end
function tmp = code(x, y, z, t)
	tmp = 1.0 - (x / ((y - z) * (y - t)));
end
code[x_, y_, z_, t_] := N[(1.0 - N[(x / N[(N[(y - z), $MachinePrecision] * N[(y - t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)}
\end{array}

Alternative 1: 99.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)} \end{array} \]
(FPCore (x y z t) :precision binary64 (- 1.0 (/ x (* (- t y) (- z y)))))
double code(double x, double y, double z, double t) {
	return 1.0 - (x / ((t - y) * (z - y)));
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = 1.0d0 - (x / ((t - y) * (z - y)))
end function
public static double code(double x, double y, double z, double t) {
	return 1.0 - (x / ((t - y) * (z - y)));
}
def code(x, y, z, t):
	return 1.0 - (x / ((t - y) * (z - y)))
function code(x, y, z, t)
	return Float64(1.0 - Float64(x / Float64(Float64(t - y) * Float64(z - y))))
end
function tmp = code(x, y, z, t)
	tmp = 1.0 - (x / ((t - y) * (z - y)));
end
code[x_, y_, z_, t_] := N[(1.0 - N[(x / N[(N[(t - y), $MachinePrecision] * N[(z - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)}
\end{array}
Derivation
  1. Initial program 98.7%

    \[1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)} \]
  2. Add Preprocessing
  3. Final simplification98.7%

    \[\leadsto 1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)} \]
  4. Add Preprocessing

Alternative 2: 97.9% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{x}{\left(t - y\right) \cdot \left(y - z\right)}\\ t_2 := 1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)}\\ \mathbf{if}\;t\_2 \leq -400:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_2 \leq 2:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (let* ((t_1 (/ x (* (- t y) (- y z))))
        (t_2 (- 1.0 (/ x (* (- t y) (- z y))))))
   (if (<= t_2 -400.0) t_1 (if (<= t_2 2.0) 1.0 t_1))))
double code(double x, double y, double z, double t) {
	double t_1 = x / ((t - y) * (y - z));
	double t_2 = 1.0 - (x / ((t - y) * (z - y)));
	double tmp;
	if (t_2 <= -400.0) {
		tmp = t_1;
	} else if (t_2 <= 2.0) {
		tmp = 1.0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_1 = x / ((t - y) * (y - z))
    t_2 = 1.0d0 - (x / ((t - y) * (z - y)))
    if (t_2 <= (-400.0d0)) then
        tmp = t_1
    else if (t_2 <= 2.0d0) then
        tmp = 1.0d0
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double x, double y, double z, double t) {
	double t_1 = x / ((t - y) * (y - z));
	double t_2 = 1.0 - (x / ((t - y) * (z - y)));
	double tmp;
	if (t_2 <= -400.0) {
		tmp = t_1;
	} else if (t_2 <= 2.0) {
		tmp = 1.0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(x, y, z, t):
	t_1 = x / ((t - y) * (y - z))
	t_2 = 1.0 - (x / ((t - y) * (z - y)))
	tmp = 0
	if t_2 <= -400.0:
		tmp = t_1
	elif t_2 <= 2.0:
		tmp = 1.0
	else:
		tmp = t_1
	return tmp
function code(x, y, z, t)
	t_1 = Float64(x / Float64(Float64(t - y) * Float64(y - z)))
	t_2 = Float64(1.0 - Float64(x / Float64(Float64(t - y) * Float64(z - y))))
	tmp = 0.0
	if (t_2 <= -400.0)
		tmp = t_1;
	elseif (t_2 <= 2.0)
		tmp = 1.0;
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(x, y, z, t)
	t_1 = x / ((t - y) * (y - z));
	t_2 = 1.0 - (x / ((t - y) * (z - y)));
	tmp = 0.0;
	if (t_2 <= -400.0)
		tmp = t_1;
	elseif (t_2 <= 2.0)
		tmp = 1.0;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(x / N[(N[(t - y), $MachinePrecision] * N[(y - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(1.0 - N[(x / N[(N[(t - y), $MachinePrecision] * N[(z - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$2, -400.0], t$95$1, If[LessEqual[t$95$2, 2.0], 1.0, t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{x}{\left(t - y\right) \cdot \left(y - z\right)}\\
t_2 := 1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)}\\
\mathbf{if}\;t\_2 \leq -400:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_2 \leq 2:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 #s(literal 1 binary64) (/.f64 x (*.f64 (-.f64 y z) (-.f64 y t)))) < -400 or 2 < (-.f64 #s(literal 1 binary64) (/.f64 x (*.f64 (-.f64 y z) (-.f64 y t))))

    1. Initial program 94.5%

      \[1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{-1 \cdot \frac{x}{\left(y - t\right) \cdot \left(y - z\right)}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(\frac{x}{\left(y - t\right) \cdot \left(y - z\right)}\right)} \]
      2. distribute-neg-frac2N/A

        \[\leadsto \color{blue}{\frac{x}{\mathsf{neg}\left(\left(y - t\right) \cdot \left(y - z\right)\right)}} \]
      3. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{x}{\mathsf{neg}\left(\left(y - t\right) \cdot \left(y - z\right)\right)}} \]
      4. distribute-lft-neg-inN/A

        \[\leadsto \frac{x}{\color{blue}{\left(\mathsf{neg}\left(\left(y - t\right)\right)\right) \cdot \left(y - z\right)}} \]
      5. mul-1-negN/A

        \[\leadsto \frac{x}{\color{blue}{\left(-1 \cdot \left(y - t\right)\right)} \cdot \left(y - z\right)} \]
      6. lower-*.f64N/A

        \[\leadsto \frac{x}{\color{blue}{\left(-1 \cdot \left(y - t\right)\right) \cdot \left(y - z\right)}} \]
      7. mul-1-negN/A

        \[\leadsto \frac{x}{\color{blue}{\left(\mathsf{neg}\left(\left(y - t\right)\right)\right)} \cdot \left(y - z\right)} \]
      8. sub-negN/A

        \[\leadsto \frac{x}{\left(\mathsf{neg}\left(\color{blue}{\left(y + \left(\mathsf{neg}\left(t\right)\right)\right)}\right)\right) \cdot \left(y - z\right)} \]
      9. mul-1-negN/A

        \[\leadsto \frac{x}{\left(\mathsf{neg}\left(\left(y + \color{blue}{-1 \cdot t}\right)\right)\right) \cdot \left(y - z\right)} \]
      10. +-commutativeN/A

        \[\leadsto \frac{x}{\left(\mathsf{neg}\left(\color{blue}{\left(-1 \cdot t + y\right)}\right)\right) \cdot \left(y - z\right)} \]
      11. distribute-neg-inN/A

        \[\leadsto \frac{x}{\color{blue}{\left(\left(\mathsf{neg}\left(-1 \cdot t\right)\right) + \left(\mathsf{neg}\left(y\right)\right)\right)} \cdot \left(y - z\right)} \]
      12. unsub-negN/A

        \[\leadsto \frac{x}{\color{blue}{\left(\left(\mathsf{neg}\left(-1 \cdot t\right)\right) - y\right)} \cdot \left(y - z\right)} \]
      13. mul-1-negN/A

        \[\leadsto \frac{x}{\left(\left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(t\right)\right)}\right)\right) - y\right) \cdot \left(y - z\right)} \]
      14. remove-double-negN/A

        \[\leadsto \frac{x}{\left(\color{blue}{t} - y\right) \cdot \left(y - z\right)} \]
      15. lower--.f64N/A

        \[\leadsto \frac{x}{\color{blue}{\left(t - y\right)} \cdot \left(y - z\right)} \]
      16. lower--.f6492.3

        \[\leadsto \frac{x}{\left(t - y\right) \cdot \color{blue}{\left(y - z\right)}} \]
    5. Applied rewrites92.3%

      \[\leadsto \color{blue}{\frac{x}{\left(t - y\right) \cdot \left(y - z\right)}} \]

    if -400 < (-.f64 #s(literal 1 binary64) (/.f64 x (*.f64 (-.f64 y z) (-.f64 y t)))) < 2

    1. Initial program 100.0%

      \[1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in t around inf

      \[\leadsto \color{blue}{1} \]
    4. Step-by-step derivation
      1. Applied rewrites99.4%

        \[\leadsto \color{blue}{1} \]
    5. Recombined 2 regimes into one program.
    6. Final simplification97.7%

      \[\leadsto \begin{array}{l} \mathbf{if}\;1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)} \leq -400:\\ \;\;\;\;\frac{x}{\left(t - y\right) \cdot \left(y - z\right)}\\ \mathbf{elif}\;1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)} \leq 2:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\left(t - y\right) \cdot \left(y - z\right)}\\ \end{array} \]
    7. Add Preprocessing

    Alternative 3: 85.8% accurate, 0.3× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_1 := 1 - \frac{x}{t \cdot z}\\ t_2 := 1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)}\\ \mathbf{if}\;t\_2 \leq -400:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_2 \leq 2:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
    (FPCore (x y z t)
     :precision binary64
     (let* ((t_1 (- 1.0 (/ x (* t z)))) (t_2 (- 1.0 (/ x (* (- t y) (- z y))))))
       (if (<= t_2 -400.0) t_1 (if (<= t_2 2.0) 1.0 t_1))))
    double code(double x, double y, double z, double t) {
    	double t_1 = 1.0 - (x / (t * z));
    	double t_2 = 1.0 - (x / ((t - y) * (z - y)));
    	double tmp;
    	if (t_2 <= -400.0) {
    		tmp = t_1;
    	} else if (t_2 <= 2.0) {
    		tmp = 1.0;
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    real(8) function code(x, y, z, t)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8), intent (in) :: z
        real(8), intent (in) :: t
        real(8) :: t_1
        real(8) :: t_2
        real(8) :: tmp
        t_1 = 1.0d0 - (x / (t * z))
        t_2 = 1.0d0 - (x / ((t - y) * (z - y)))
        if (t_2 <= (-400.0d0)) then
            tmp = t_1
        else if (t_2 <= 2.0d0) then
            tmp = 1.0d0
        else
            tmp = t_1
        end if
        code = tmp
    end function
    
    public static double code(double x, double y, double z, double t) {
    	double t_1 = 1.0 - (x / (t * z));
    	double t_2 = 1.0 - (x / ((t - y) * (z - y)));
    	double tmp;
    	if (t_2 <= -400.0) {
    		tmp = t_1;
    	} else if (t_2 <= 2.0) {
    		tmp = 1.0;
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    def code(x, y, z, t):
    	t_1 = 1.0 - (x / (t * z))
    	t_2 = 1.0 - (x / ((t - y) * (z - y)))
    	tmp = 0
    	if t_2 <= -400.0:
    		tmp = t_1
    	elif t_2 <= 2.0:
    		tmp = 1.0
    	else:
    		tmp = t_1
    	return tmp
    
    function code(x, y, z, t)
    	t_1 = Float64(1.0 - Float64(x / Float64(t * z)))
    	t_2 = Float64(1.0 - Float64(x / Float64(Float64(t - y) * Float64(z - y))))
    	tmp = 0.0
    	if (t_2 <= -400.0)
    		tmp = t_1;
    	elseif (t_2 <= 2.0)
    		tmp = 1.0;
    	else
    		tmp = t_1;
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y, z, t)
    	t_1 = 1.0 - (x / (t * z));
    	t_2 = 1.0 - (x / ((t - y) * (z - y)));
    	tmp = 0.0;
    	if (t_2 <= -400.0)
    		tmp = t_1;
    	elseif (t_2 <= 2.0)
    		tmp = 1.0;
    	else
    		tmp = t_1;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_, z_, t_] := Block[{t$95$1 = N[(1.0 - N[(x / N[(t * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(1.0 - N[(x / N[(N[(t - y), $MachinePrecision] * N[(z - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$2, -400.0], t$95$1, If[LessEqual[t$95$2, 2.0], 1.0, t$95$1]]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_1 := 1 - \frac{x}{t \cdot z}\\
    t_2 := 1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)}\\
    \mathbf{if}\;t\_2 \leq -400:\\
    \;\;\;\;t\_1\\
    
    \mathbf{elif}\;t\_2 \leq 2:\\
    \;\;\;\;1\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_1\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (-.f64 #s(literal 1 binary64) (/.f64 x (*.f64 (-.f64 y z) (-.f64 y t)))) < -400 or 2 < (-.f64 #s(literal 1 binary64) (/.f64 x (*.f64 (-.f64 y z) (-.f64 y t))))

      1. Initial program 94.5%

        \[1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)} \]
      2. Add Preprocessing
      3. Taylor expanded in y around 0

        \[\leadsto 1 - \frac{x}{\color{blue}{t \cdot z}} \]
      4. Step-by-step derivation
        1. lower-*.f6446.9

          \[\leadsto 1 - \frac{x}{\color{blue}{t \cdot z}} \]
      5. Applied rewrites46.9%

        \[\leadsto 1 - \frac{x}{\color{blue}{t \cdot z}} \]

      if -400 < (-.f64 #s(literal 1 binary64) (/.f64 x (*.f64 (-.f64 y z) (-.f64 y t)))) < 2

      1. Initial program 100.0%

        \[1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)} \]
      2. Add Preprocessing
      3. Taylor expanded in t around inf

        \[\leadsto \color{blue}{1} \]
      4. Step-by-step derivation
        1. Applied rewrites99.4%

          \[\leadsto \color{blue}{1} \]
      5. Recombined 2 regimes into one program.
      6. Final simplification86.7%

        \[\leadsto \begin{array}{l} \mathbf{if}\;1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)} \leq -400:\\ \;\;\;\;1 - \frac{x}{t \cdot z}\\ \mathbf{elif}\;1 - \frac{x}{\left(t - y\right) \cdot \left(z - y\right)} \leq 2:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;1 - \frac{x}{t \cdot z}\\ \end{array} \]
      7. Add Preprocessing

      Alternative 4: 75.7% accurate, 26.0× speedup?

      \[\begin{array}{l} \\ 1 \end{array} \]
      (FPCore (x y z t) :precision binary64 1.0)
      double code(double x, double y, double z, double t) {
      	return 1.0;
      }
      
      real(8) function code(x, y, z, t)
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          real(8), intent (in) :: z
          real(8), intent (in) :: t
          code = 1.0d0
      end function
      
      public static double code(double x, double y, double z, double t) {
      	return 1.0;
      }
      
      def code(x, y, z, t):
      	return 1.0
      
      function code(x, y, z, t)
      	return 1.0
      end
      
      function tmp = code(x, y, z, t)
      	tmp = 1.0;
      end
      
      code[x_, y_, z_, t_] := 1.0
      
      \begin{array}{l}
      
      \\
      1
      \end{array}
      
      Derivation
      1. Initial program 98.7%

        \[1 - \frac{x}{\left(y - z\right) \cdot \left(y - t\right)} \]
      2. Add Preprocessing
      3. Taylor expanded in t around inf

        \[\leadsto \color{blue}{1} \]
      4. Step-by-step derivation
        1. Applied rewrites76.1%

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

        Reproduce

        ?
        herbie shell --seed 2024235 
        (FPCore (x y z t)
          :name "Data.Random.Distribution.Triangular:triangularCDF from random-fu-0.2.6.2, A"
          :precision binary64
          (- 1.0 (/ x (* (- y z) (- y t)))))