Octave 3.8, oct_fill_randg

Percentage Accurate: 99.7% → 99.7%
Time: 12.1s
Alternatives: 12
Speedup: 2.4×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := a - \frac{1}{3}\\ t\_0 \cdot \left(1 + \frac{1}{\sqrt{9 \cdot t\_0}} \cdot rand\right) \end{array} \end{array} \]
(FPCore (a rand)
 :precision binary64
 (let* ((t_0 (- a (/ 1.0 3.0))))
   (* t_0 (+ 1.0 (* (/ 1.0 (sqrt (* 9.0 t_0))) rand)))))
double code(double a, double rand) {
	double t_0 = a - (1.0 / 3.0);
	return t_0 * (1.0 + ((1.0 / sqrt((9.0 * t_0))) * rand));
}
real(8) function code(a, rand)
    real(8), intent (in) :: a
    real(8), intent (in) :: rand
    real(8) :: t_0
    t_0 = a - (1.0d0 / 3.0d0)
    code = t_0 * (1.0d0 + ((1.0d0 / sqrt((9.0d0 * t_0))) * rand))
end function
public static double code(double a, double rand) {
	double t_0 = a - (1.0 / 3.0);
	return t_0 * (1.0 + ((1.0 / Math.sqrt((9.0 * t_0))) * rand));
}
def code(a, rand):
	t_0 = a - (1.0 / 3.0)
	return t_0 * (1.0 + ((1.0 / math.sqrt((9.0 * t_0))) * rand))
function code(a, rand)
	t_0 = Float64(a - Float64(1.0 / 3.0))
	return Float64(t_0 * Float64(1.0 + Float64(Float64(1.0 / sqrt(Float64(9.0 * t_0))) * rand)))
end
function tmp = code(a, rand)
	t_0 = a - (1.0 / 3.0);
	tmp = t_0 * (1.0 + ((1.0 / sqrt((9.0 * t_0))) * rand));
end
code[a_, rand_] := Block[{t$95$0 = N[(a - N[(1.0 / 3.0), $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 * N[(1.0 + N[(N[(1.0 / N[Sqrt[N[(9.0 * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * rand), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := a - \frac{1}{3}\\
t\_0 \cdot \left(1 + \frac{1}{\sqrt{9 \cdot t\_0}} \cdot rand\right)
\end{array}
\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 12 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.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := a - \frac{1}{3}\\ t\_0 \cdot \left(1 + \frac{1}{\sqrt{9 \cdot t\_0}} \cdot rand\right) \end{array} \end{array} \]
(FPCore (a rand)
 :precision binary64
 (let* ((t_0 (- a (/ 1.0 3.0))))
   (* t_0 (+ 1.0 (* (/ 1.0 (sqrt (* 9.0 t_0))) rand)))))
double code(double a, double rand) {
	double t_0 = a - (1.0 / 3.0);
	return t_0 * (1.0 + ((1.0 / sqrt((9.0 * t_0))) * rand));
}
real(8) function code(a, rand)
    real(8), intent (in) :: a
    real(8), intent (in) :: rand
    real(8) :: t_0
    t_0 = a - (1.0d0 / 3.0d0)
    code = t_0 * (1.0d0 + ((1.0d0 / sqrt((9.0d0 * t_0))) * rand))
end function
public static double code(double a, double rand) {
	double t_0 = a - (1.0 / 3.0);
	return t_0 * (1.0 + ((1.0 / Math.sqrt((9.0 * t_0))) * rand));
}
def code(a, rand):
	t_0 = a - (1.0 / 3.0)
	return t_0 * (1.0 + ((1.0 / math.sqrt((9.0 * t_0))) * rand))
function code(a, rand)
	t_0 = Float64(a - Float64(1.0 / 3.0))
	return Float64(t_0 * Float64(1.0 + Float64(Float64(1.0 / sqrt(Float64(9.0 * t_0))) * rand)))
end
function tmp = code(a, rand)
	t_0 = a - (1.0 / 3.0);
	tmp = t_0 * (1.0 + ((1.0 / sqrt((9.0 * t_0))) * rand));
end
code[a_, rand_] := Block[{t$95$0 = N[(a - N[(1.0 / 3.0), $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 * N[(1.0 + N[(N[(1.0 / N[Sqrt[N[(9.0 * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * rand), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := a - \frac{1}{3}\\
t\_0 \cdot \left(1 + \frac{1}{\sqrt{9 \cdot t\_0}} \cdot rand\right)
\end{array}
\end{array}

Alternative 1: 99.7% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\frac{a + -0.3333333333333333}{\sqrt{\mathsf{fma}\left(9, a, -3\right)}}, rand, a + -0.3333333333333333\right) \end{array} \]
(FPCore (a rand)
 :precision binary64
 (fma
  (/ (+ a -0.3333333333333333) (sqrt (fma 9.0 a -3.0)))
  rand
  (+ a -0.3333333333333333)))
double code(double a, double rand) {
	return fma(((a + -0.3333333333333333) / sqrt(fma(9.0, a, -3.0))), rand, (a + -0.3333333333333333));
}
function code(a, rand)
	return fma(Float64(Float64(a + -0.3333333333333333) / sqrt(fma(9.0, a, -3.0))), rand, Float64(a + -0.3333333333333333))
end
code[a_, rand_] := N[(N[(N[(a + -0.3333333333333333), $MachinePrecision] / N[Sqrt[N[(9.0 * a + -3.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * rand + N[(a + -0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\frac{a + -0.3333333333333333}{\sqrt{\mathsf{fma}\left(9, a, -3\right)}}, rand, a + -0.3333333333333333\right)
\end{array}
Derivation
  1. Initial program 99.8%

    \[\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right)} \]
    2. lift-+.f64N/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \color{blue}{\left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right)} \]
    3. +-commutativeN/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \color{blue}{\left(\frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand + 1\right)} \]
    4. distribute-lft-inN/A

      \[\leadsto \color{blue}{\left(a - \frac{1}{3}\right) \cdot \left(\frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) + \left(a - \frac{1}{3}\right) \cdot 1} \]
    5. *-lft-identityN/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(\frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot \color{blue}{\left(1 \cdot rand\right)}\right) + \left(a - \frac{1}{3}\right) \cdot 1 \]
    6. lower-*.f64N/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \color{blue}{\left(\frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot \left(1 \cdot rand\right)\right)} + \left(a - \frac{1}{3}\right) \cdot 1 \]
    7. associate-*r*N/A

      \[\leadsto \color{blue}{\left(\left(a - \frac{1}{3}\right) \cdot \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}}\right) \cdot \left(1 \cdot rand\right)} + \left(a - \frac{1}{3}\right) \cdot 1 \]
    8. *-rgt-identityN/A

      \[\leadsto \left(\left(a - \frac{1}{3}\right) \cdot \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}}\right) \cdot \left(1 \cdot rand\right) + \color{blue}{\left(a - \frac{1}{3}\right)} \]
    9. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\left(a - \frac{1}{3}\right) \cdot \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}}, 1 \cdot rand, a - \frac{1}{3}\right)} \]
  4. Applied rewrites99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{a + -0.3333333333333333}{\sqrt{\mathsf{fma}\left(9, a, -3\right)}}, rand, a + -0.3333333333333333\right)} \]
  5. Add Preprocessing

Alternative 2: 92.8% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{a + -0.3333333333333333}\\ \mathbf{if}\;rand \leq -1.08 \cdot 10^{+85}:\\ \;\;\;\;0.3333333333333333 \cdot \left(rand \cdot t\_0\right)\\ \mathbf{elif}\;rand \leq 6.5 \cdot 10^{+75}:\\ \;\;\;\;a + -0.3333333333333333\\ \mathbf{else}:\\ \;\;\;\;t\_0 \cdot \left(rand \cdot 0.3333333333333333\right)\\ \end{array} \end{array} \]
(FPCore (a rand)
 :precision binary64
 (let* ((t_0 (sqrt (+ a -0.3333333333333333))))
   (if (<= rand -1.08e+85)
     (* 0.3333333333333333 (* rand t_0))
     (if (<= rand 6.5e+75)
       (+ a -0.3333333333333333)
       (* t_0 (* rand 0.3333333333333333))))))
double code(double a, double rand) {
	double t_0 = sqrt((a + -0.3333333333333333));
	double tmp;
	if (rand <= -1.08e+85) {
		tmp = 0.3333333333333333 * (rand * t_0);
	} else if (rand <= 6.5e+75) {
		tmp = a + -0.3333333333333333;
	} else {
		tmp = t_0 * (rand * 0.3333333333333333);
	}
	return tmp;
}
real(8) function code(a, rand)
    real(8), intent (in) :: a
    real(8), intent (in) :: rand
    real(8) :: t_0
    real(8) :: tmp
    t_0 = sqrt((a + (-0.3333333333333333d0)))
    if (rand <= (-1.08d+85)) then
        tmp = 0.3333333333333333d0 * (rand * t_0)
    else if (rand <= 6.5d+75) then
        tmp = a + (-0.3333333333333333d0)
    else
        tmp = t_0 * (rand * 0.3333333333333333d0)
    end if
    code = tmp
end function
public static double code(double a, double rand) {
	double t_0 = Math.sqrt((a + -0.3333333333333333));
	double tmp;
	if (rand <= -1.08e+85) {
		tmp = 0.3333333333333333 * (rand * t_0);
	} else if (rand <= 6.5e+75) {
		tmp = a + -0.3333333333333333;
	} else {
		tmp = t_0 * (rand * 0.3333333333333333);
	}
	return tmp;
}
def code(a, rand):
	t_0 = math.sqrt((a + -0.3333333333333333))
	tmp = 0
	if rand <= -1.08e+85:
		tmp = 0.3333333333333333 * (rand * t_0)
	elif rand <= 6.5e+75:
		tmp = a + -0.3333333333333333
	else:
		tmp = t_0 * (rand * 0.3333333333333333)
	return tmp
function code(a, rand)
	t_0 = sqrt(Float64(a + -0.3333333333333333))
	tmp = 0.0
	if (rand <= -1.08e+85)
		tmp = Float64(0.3333333333333333 * Float64(rand * t_0));
	elseif (rand <= 6.5e+75)
		tmp = Float64(a + -0.3333333333333333);
	else
		tmp = Float64(t_0 * Float64(rand * 0.3333333333333333));
	end
	return tmp
end
function tmp_2 = code(a, rand)
	t_0 = sqrt((a + -0.3333333333333333));
	tmp = 0.0;
	if (rand <= -1.08e+85)
		tmp = 0.3333333333333333 * (rand * t_0);
	elseif (rand <= 6.5e+75)
		tmp = a + -0.3333333333333333;
	else
		tmp = t_0 * (rand * 0.3333333333333333);
	end
	tmp_2 = tmp;
end
code[a_, rand_] := Block[{t$95$0 = N[Sqrt[N[(a + -0.3333333333333333), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[rand, -1.08e+85], N[(0.3333333333333333 * N[(rand * t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[rand, 6.5e+75], N[(a + -0.3333333333333333), $MachinePrecision], N[(t$95$0 * N[(rand * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{a + -0.3333333333333333}\\
\mathbf{if}\;rand \leq -1.08 \cdot 10^{+85}:\\
\;\;\;\;0.3333333333333333 \cdot \left(rand \cdot t\_0\right)\\

\mathbf{elif}\;rand \leq 6.5 \cdot 10^{+75}:\\
\;\;\;\;a + -0.3333333333333333\\

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \left(rand \cdot 0.3333333333333333\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if rand < -1.08e85

    1. Initial program 99.3%

      \[\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right)} \]
      2. lift-+.f64N/A

        \[\leadsto \left(a - \frac{1}{3}\right) \cdot \color{blue}{\left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right)} \]
      3. +-commutativeN/A

        \[\leadsto \left(a - \frac{1}{3}\right) \cdot \color{blue}{\left(\frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand + 1\right)} \]
      4. distribute-lft-inN/A

        \[\leadsto \color{blue}{\left(a - \frac{1}{3}\right) \cdot \left(\frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) + \left(a - \frac{1}{3}\right) \cdot 1} \]
    4. Applied rewrites71.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\left(\left(a + -0.3333333333333333\right) \cdot rand\right) \cdot 0.3333333333333333, \frac{1}{\sqrt{a + -0.3333333333333333}}, a + -0.3333333333333333\right)} \]
    5. Taylor expanded in rand around inf

      \[\leadsto \color{blue}{\frac{1}{3} \cdot \left(rand \cdot \sqrt{a - \frac{1}{3}}\right)} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(rand \cdot \sqrt{a - \frac{1}{3}}\right) \cdot \frac{1}{3}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\sqrt{a - \frac{1}{3}} \cdot rand\right)} \cdot \frac{1}{3} \]
      3. associate-*l*N/A

        \[\leadsto \color{blue}{\sqrt{a - \frac{1}{3}} \cdot \left(rand \cdot \frac{1}{3}\right)} \]
      4. *-commutativeN/A

        \[\leadsto \sqrt{a - \frac{1}{3}} \cdot \color{blue}{\left(\frac{1}{3} \cdot rand\right)} \]
      5. lower-*.f64N/A

        \[\leadsto \color{blue}{\sqrt{a - \frac{1}{3}} \cdot \left(\frac{1}{3} \cdot rand\right)} \]
      6. lower-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{a - \frac{1}{3}}} \cdot \left(\frac{1}{3} \cdot rand\right) \]
      7. sub-negN/A

        \[\leadsto \sqrt{\color{blue}{a + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)}} \cdot \left(\frac{1}{3} \cdot rand\right) \]
      8. metadata-evalN/A

        \[\leadsto \sqrt{a + \color{blue}{\frac{-1}{3}}} \cdot \left(\frac{1}{3} \cdot rand\right) \]
      9. +-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\frac{-1}{3} + a}} \cdot \left(\frac{1}{3} \cdot rand\right) \]
      10. lower-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{\frac{-1}{3} + a}} \cdot \left(\frac{1}{3} \cdot rand\right) \]
      11. *-commutativeN/A

        \[\leadsto \sqrt{\frac{-1}{3} + a} \cdot \color{blue}{\left(rand \cdot \frac{1}{3}\right)} \]
      12. lower-*.f6491.2

        \[\leadsto \sqrt{-0.3333333333333333 + a} \cdot \color{blue}{\left(rand \cdot 0.3333333333333333\right)} \]
    7. Applied rewrites91.2%

      \[\leadsto \color{blue}{\sqrt{-0.3333333333333333 + a} \cdot \left(rand \cdot 0.3333333333333333\right)} \]
    8. Step-by-step derivation
      1. Applied rewrites91.0%

        \[\leadsto \left(rand \cdot \sqrt{a + -0.3333333333333333}\right) \cdot \color{blue}{0.3333333333333333} \]

      if -1.08e85 < rand < 6.4999999999999998e75

      1. Initial program 99.9%

        \[\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]
      2. Add Preprocessing
      3. Taylor expanded in rand around 0

        \[\leadsto \color{blue}{a - \frac{1}{3}} \]
      4. Step-by-step derivation
        1. sub-negN/A

          \[\leadsto \color{blue}{a + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)} \]
        2. metadata-evalN/A

          \[\leadsto a + \color{blue}{\frac{-1}{3}} \]
        3. lower-+.f6494.0

          \[\leadsto \color{blue}{a + -0.3333333333333333} \]
      5. Applied rewrites94.0%

        \[\leadsto \color{blue}{a + -0.3333333333333333} \]

      if 6.4999999999999998e75 < rand

      1. Initial program 99.3%

        \[\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]
      2. Add Preprocessing
      3. Taylor expanded in rand around inf

        \[\leadsto \color{blue}{\frac{1}{3} \cdot \left(rand \cdot \sqrt{a - \frac{1}{3}}\right)} \]
      4. Step-by-step derivation
        1. associate-*r*N/A

          \[\leadsto \color{blue}{\left(\frac{1}{3} \cdot rand\right) \cdot \sqrt{a - \frac{1}{3}}} \]
        2. *-commutativeN/A

          \[\leadsto \color{blue}{\sqrt{a - \frac{1}{3}} \cdot \left(\frac{1}{3} \cdot rand\right)} \]
        3. lower-*.f64N/A

          \[\leadsto \color{blue}{\sqrt{a - \frac{1}{3}} \cdot \left(\frac{1}{3} \cdot rand\right)} \]
        4. lower-sqrt.f64N/A

          \[\leadsto \color{blue}{\sqrt{a - \frac{1}{3}}} \cdot \left(\frac{1}{3} \cdot rand\right) \]
        5. sub-negN/A

          \[\leadsto \sqrt{\color{blue}{a + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)}} \cdot \left(\frac{1}{3} \cdot rand\right) \]
        6. metadata-evalN/A

          \[\leadsto \sqrt{a + \color{blue}{\frac{-1}{3}}} \cdot \left(\frac{1}{3} \cdot rand\right) \]
        7. lower-+.f64N/A

          \[\leadsto \sqrt{\color{blue}{a + \frac{-1}{3}}} \cdot \left(\frac{1}{3} \cdot rand\right) \]
        8. lower-*.f6490.5

          \[\leadsto \sqrt{a + -0.3333333333333333} \cdot \color{blue}{\left(0.3333333333333333 \cdot rand\right)} \]
      5. Applied rewrites90.5%

        \[\leadsto \color{blue}{\sqrt{a + -0.3333333333333333} \cdot \left(0.3333333333333333 \cdot rand\right)} \]
    9. Recombined 3 regimes into one program.
    10. Final simplification92.8%

      \[\leadsto \begin{array}{l} \mathbf{if}\;rand \leq -1.08 \cdot 10^{+85}:\\ \;\;\;\;0.3333333333333333 \cdot \left(rand \cdot \sqrt{a + -0.3333333333333333}\right)\\ \mathbf{elif}\;rand \leq 6.5 \cdot 10^{+75}:\\ \;\;\;\;a + -0.3333333333333333\\ \mathbf{else}:\\ \;\;\;\;\sqrt{a + -0.3333333333333333} \cdot \left(rand \cdot 0.3333333333333333\right)\\ \end{array} \]
    11. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2024229 
    (FPCore (a rand)
      :name "Octave 3.8, oct_fill_randg"
      :precision binary64
      (* (- a (/ 1.0 3.0)) (+ 1.0 (* (/ 1.0 (sqrt (* 9.0 (- a (/ 1.0 3.0))))) rand))))