
(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:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (a rand)
:precision binary64
(-
(+
a
(*
(sqrt (* a (- 1.0 (/ 0.3333333333333333 a))))
(* 0.3333333333333333 rand)))
0.3333333333333333))
double code(double a, double rand) {
return (a + (sqrt((a * (1.0 - (0.3333333333333333 / a)))) * (0.3333333333333333 * rand))) - 0.3333333333333333;
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
code = (a + (sqrt((a * (1.0d0 - (0.3333333333333333d0 / a)))) * (0.3333333333333333d0 * rand))) - 0.3333333333333333d0
end function
public static double code(double a, double rand) {
return (a + (Math.sqrt((a * (1.0 - (0.3333333333333333 / a)))) * (0.3333333333333333 * rand))) - 0.3333333333333333;
}
def code(a, rand): return (a + (math.sqrt((a * (1.0 - (0.3333333333333333 / a)))) * (0.3333333333333333 * rand))) - 0.3333333333333333
function code(a, rand) return Float64(Float64(a + Float64(sqrt(Float64(a * Float64(1.0 - Float64(0.3333333333333333 / a)))) * Float64(0.3333333333333333 * rand))) - 0.3333333333333333) end
function tmp = code(a, rand) tmp = (a + (sqrt((a * (1.0 - (0.3333333333333333 / a)))) * (0.3333333333333333 * rand))) - 0.3333333333333333; end
code[a_, rand_] := N[(N[(a + N[(N[Sqrt[N[(a * N[(1.0 - N[(0.3333333333333333 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(0.3333333333333333 * rand), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $MachinePrecision]
\begin{array}{l}
\\
\left(a + \sqrt{a \cdot \left(1 - \frac{0.3333333333333333}{a}\right)} \cdot \left(0.3333333333333333 \cdot rand\right)\right) - 0.3333333333333333
\end{array}
Initial program 99.8%
*-lft-identity99.8%
*-lft-identity99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
*-commutative99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
Taylor expanded in rand around 0 99.8%
pow199.8%
*-commutative99.8%
*-commutative99.8%
sub-neg99.8%
metadata-eval99.8%
associate-*l*99.8%
Applied egg-rr99.8%
unpow199.8%
+-commutative99.8%
*-commutative99.8%
Simplified99.8%
Taylor expanded in a around inf 99.8%
associate-*r/99.8%
metadata-eval99.8%
Simplified99.8%
(FPCore (a rand) :precision binary64 (if (or (<= rand -3.1e+76) (not (<= rand 1.1e+68))) (* 0.3333333333333333 (* rand (sqrt (- a 0.3333333333333333)))) (- a 0.3333333333333333)))
double code(double a, double rand) {
double tmp;
if ((rand <= -3.1e+76) || !(rand <= 1.1e+68)) {
tmp = 0.3333333333333333 * (rand * sqrt((a - 0.3333333333333333)));
} else {
tmp = a - 0.3333333333333333;
}
return tmp;
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
real(8) :: tmp
if ((rand <= (-3.1d+76)) .or. (.not. (rand <= 1.1d+68))) then
tmp = 0.3333333333333333d0 * (rand * sqrt((a - 0.3333333333333333d0)))
else
tmp = a - 0.3333333333333333d0
end if
code = tmp
end function
public static double code(double a, double rand) {
double tmp;
if ((rand <= -3.1e+76) || !(rand <= 1.1e+68)) {
tmp = 0.3333333333333333 * (rand * Math.sqrt((a - 0.3333333333333333)));
} else {
tmp = a - 0.3333333333333333;
}
return tmp;
}
def code(a, rand): tmp = 0 if (rand <= -3.1e+76) or not (rand <= 1.1e+68): tmp = 0.3333333333333333 * (rand * math.sqrt((a - 0.3333333333333333))) else: tmp = a - 0.3333333333333333 return tmp
function code(a, rand) tmp = 0.0 if ((rand <= -3.1e+76) || !(rand <= 1.1e+68)) tmp = Float64(0.3333333333333333 * Float64(rand * sqrt(Float64(a - 0.3333333333333333)))); else tmp = Float64(a - 0.3333333333333333); end return tmp end
function tmp_2 = code(a, rand) tmp = 0.0; if ((rand <= -3.1e+76) || ~((rand <= 1.1e+68))) tmp = 0.3333333333333333 * (rand * sqrt((a - 0.3333333333333333))); else tmp = a - 0.3333333333333333; end tmp_2 = tmp; end
code[a_, rand_] := If[Or[LessEqual[rand, -3.1e+76], N[Not[LessEqual[rand, 1.1e+68]], $MachinePrecision]], N[(0.3333333333333333 * N[(rand * N[Sqrt[N[(a - 0.3333333333333333), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a - 0.3333333333333333), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;rand \leq -3.1 \cdot 10^{+76} \lor \neg \left(rand \leq 1.1 \cdot 10^{+68}\right):\\
\;\;\;\;0.3333333333333333 \cdot \left(rand \cdot \sqrt{a - 0.3333333333333333}\right)\\
\mathbf{else}:\\
\;\;\;\;a - 0.3333333333333333\\
\end{array}
\end{array}
if rand < -3.10000000000000011e76 or 1.09999999999999994e68 < rand Initial program 99.4%
*-lft-identity99.4%
*-lft-identity99.4%
sub-neg99.4%
metadata-eval99.4%
metadata-eval99.4%
*-commutative99.4%
sub-neg99.4%
metadata-eval99.4%
metadata-eval99.4%
Simplified99.4%
Taylor expanded in rand around inf 89.3%
if -3.10000000000000011e76 < rand < 1.09999999999999994e68Initial program 100.0%
*-lft-identity100.0%
*-lft-identity100.0%
sub-neg100.0%
metadata-eval100.0%
metadata-eval100.0%
*-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in rand around 0 96.6%
Final simplification94.3%
(FPCore (a rand) :precision binary64 (* (+ 1.0 (/ rand (sqrt (* a (- 9.0 (/ 3.0 a)))))) (+ a -0.3333333333333333)))
double code(double a, double rand) {
return (1.0 + (rand / sqrt((a * (9.0 - (3.0 / a)))))) * (a + -0.3333333333333333);
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
code = (1.0d0 + (rand / sqrt((a * (9.0d0 - (3.0d0 / a)))))) * (a + (-0.3333333333333333d0))
end function
public static double code(double a, double rand) {
return (1.0 + (rand / Math.sqrt((a * (9.0 - (3.0 / a)))))) * (a + -0.3333333333333333);
}
def code(a, rand): return (1.0 + (rand / math.sqrt((a * (9.0 - (3.0 / a)))))) * (a + -0.3333333333333333)
function code(a, rand) return Float64(Float64(1.0 + Float64(rand / sqrt(Float64(a * Float64(9.0 - Float64(3.0 / a)))))) * Float64(a + -0.3333333333333333)) end
function tmp = code(a, rand) tmp = (1.0 + (rand / sqrt((a * (9.0 - (3.0 / a)))))) * (a + -0.3333333333333333); end
code[a_, rand_] := N[(N[(1.0 + N[(rand / N[Sqrt[N[(a * N[(9.0 - N[(3.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(a + -0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(1 + \frac{rand}{\sqrt{a \cdot \left(9 - \frac{3}{a}\right)}}\right) \cdot \left(a + -0.3333333333333333\right)
\end{array}
Initial program 99.8%
*-lft-identity99.8%
*-lft-identity99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
associate-*l/99.8%
*-lft-identity99.8%
sub-neg99.8%
distribute-lft-in99.8%
metadata-eval99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
Taylor expanded in a around inf 99.8%
associate-*r/99.8%
metadata-eval99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (a rand) :precision binary64 (- (+ a (* (* 0.3333333333333333 rand) (sqrt (+ a -0.3333333333333333)))) 0.3333333333333333))
double code(double a, double rand) {
return (a + ((0.3333333333333333 * rand) * sqrt((a + -0.3333333333333333)))) - 0.3333333333333333;
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
code = (a + ((0.3333333333333333d0 * rand) * sqrt((a + (-0.3333333333333333d0))))) - 0.3333333333333333d0
end function
public static double code(double a, double rand) {
return (a + ((0.3333333333333333 * rand) * Math.sqrt((a + -0.3333333333333333)))) - 0.3333333333333333;
}
def code(a, rand): return (a + ((0.3333333333333333 * rand) * math.sqrt((a + -0.3333333333333333)))) - 0.3333333333333333
function code(a, rand) return Float64(Float64(a + Float64(Float64(0.3333333333333333 * rand) * sqrt(Float64(a + -0.3333333333333333)))) - 0.3333333333333333) end
function tmp = code(a, rand) tmp = (a + ((0.3333333333333333 * rand) * sqrt((a + -0.3333333333333333)))) - 0.3333333333333333; end
code[a_, rand_] := N[(N[(a + N[(N[(0.3333333333333333 * rand), $MachinePrecision] * N[Sqrt[N[(a + -0.3333333333333333), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $MachinePrecision]
\begin{array}{l}
\\
\left(a + \left(0.3333333333333333 \cdot rand\right) \cdot \sqrt{a + -0.3333333333333333}\right) - 0.3333333333333333
\end{array}
Initial program 99.8%
*-lft-identity99.8%
*-lft-identity99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
*-commutative99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
Taylor expanded in rand around 0 99.8%
pow199.8%
*-commutative99.8%
*-commutative99.8%
sub-neg99.8%
metadata-eval99.8%
associate-*l*99.8%
Applied egg-rr99.8%
unpow199.8%
+-commutative99.8%
*-commutative99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (a rand) :precision binary64 (- (+ a (* 0.3333333333333333 (* rand (sqrt (- a 0.3333333333333333))))) 0.3333333333333333))
double code(double a, double rand) {
return (a + (0.3333333333333333 * (rand * sqrt((a - 0.3333333333333333))))) - 0.3333333333333333;
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
code = (a + (0.3333333333333333d0 * (rand * sqrt((a - 0.3333333333333333d0))))) - 0.3333333333333333d0
end function
public static double code(double a, double rand) {
return (a + (0.3333333333333333 * (rand * Math.sqrt((a - 0.3333333333333333))))) - 0.3333333333333333;
}
def code(a, rand): return (a + (0.3333333333333333 * (rand * math.sqrt((a - 0.3333333333333333))))) - 0.3333333333333333
function code(a, rand) return Float64(Float64(a + Float64(0.3333333333333333 * Float64(rand * sqrt(Float64(a - 0.3333333333333333))))) - 0.3333333333333333) end
function tmp = code(a, rand) tmp = (a + (0.3333333333333333 * (rand * sqrt((a - 0.3333333333333333))))) - 0.3333333333333333; end
code[a_, rand_] := N[(N[(a + N[(0.3333333333333333 * N[(rand * N[Sqrt[N[(a - 0.3333333333333333), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $MachinePrecision]
\begin{array}{l}
\\
\left(a + 0.3333333333333333 \cdot \left(rand \cdot \sqrt{a - 0.3333333333333333}\right)\right) - 0.3333333333333333
\end{array}
Initial program 99.8%
*-lft-identity99.8%
*-lft-identity99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
*-commutative99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
Taylor expanded in rand around 0 99.8%
(FPCore (a rand) :precision binary64 (- (+ a (* rand (* 0.3333333333333333 (sqrt a)))) 0.3333333333333333))
double code(double a, double rand) {
return (a + (rand * (0.3333333333333333 * sqrt(a)))) - 0.3333333333333333;
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
code = (a + (rand * (0.3333333333333333d0 * sqrt(a)))) - 0.3333333333333333d0
end function
public static double code(double a, double rand) {
return (a + (rand * (0.3333333333333333 * Math.sqrt(a)))) - 0.3333333333333333;
}
def code(a, rand): return (a + (rand * (0.3333333333333333 * math.sqrt(a)))) - 0.3333333333333333
function code(a, rand) return Float64(Float64(a + Float64(rand * Float64(0.3333333333333333 * sqrt(a)))) - 0.3333333333333333) end
function tmp = code(a, rand) tmp = (a + (rand * (0.3333333333333333 * sqrt(a)))) - 0.3333333333333333; end
code[a_, rand_] := N[(N[(a + N[(rand * N[(0.3333333333333333 * N[Sqrt[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $MachinePrecision]
\begin{array}{l}
\\
\left(a + rand \cdot \left(0.3333333333333333 \cdot \sqrt{a}\right)\right) - 0.3333333333333333
\end{array}
Initial program 99.8%
*-lft-identity99.8%
*-lft-identity99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
*-commutative99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
Taylor expanded in rand around 0 99.8%
Taylor expanded in a around inf 97.9%
associate-*r*97.9%
Simplified97.9%
Final simplification97.9%
(FPCore (a rand) :precision binary64 (+ a (* 0.3333333333333333 (* rand (sqrt a)))))
double code(double a, double rand) {
return a + (0.3333333333333333 * (rand * sqrt(a)));
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
code = a + (0.3333333333333333d0 * (rand * sqrt(a)))
end function
public static double code(double a, double rand) {
return a + (0.3333333333333333 * (rand * Math.sqrt(a)));
}
def code(a, rand): return a + (0.3333333333333333 * (rand * math.sqrt(a)))
function code(a, rand) return Float64(a + Float64(0.3333333333333333 * Float64(rand * sqrt(a)))) end
function tmp = code(a, rand) tmp = a + (0.3333333333333333 * (rand * sqrt(a))); end
code[a_, rand_] := N[(a + N[(0.3333333333333333 * N[(rand * N[Sqrt[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
a + 0.3333333333333333 \cdot \left(rand \cdot \sqrt{a}\right)
\end{array}
Initial program 99.8%
*-lft-identity99.8%
*-lft-identity99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
associate-*l/99.8%
*-lft-identity99.8%
sub-neg99.8%
distribute-lft-in99.8%
metadata-eval99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
Taylor expanded in a around inf 97.9%
*-commutative97.9%
Simplified97.9%
Taylor expanded in a around inf 96.6%
Taylor expanded in a around 0 96.6%
Final simplification96.6%
(FPCore (a rand) :precision binary64 (- a 0.3333333333333333))
double code(double a, double rand) {
return a - 0.3333333333333333;
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
code = a - 0.3333333333333333d0
end function
public static double code(double a, double rand) {
return a - 0.3333333333333333;
}
def code(a, rand): return a - 0.3333333333333333
function code(a, rand) return Float64(a - 0.3333333333333333) end
function tmp = code(a, rand) tmp = a - 0.3333333333333333; end
code[a_, rand_] := N[(a - 0.3333333333333333), $MachinePrecision]
\begin{array}{l}
\\
a - 0.3333333333333333
\end{array}
Initial program 99.8%
*-lft-identity99.8%
*-lft-identity99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
*-commutative99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
Taylor expanded in rand around 0 69.1%
(FPCore (a rand) :precision binary64 a)
double code(double a, double rand) {
return a;
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
code = a
end function
public static double code(double a, double rand) {
return a;
}
def code(a, rand): return a
function code(a, rand) return a end
function tmp = code(a, rand) tmp = a; end
code[a_, rand_] := a
\begin{array}{l}
\\
a
\end{array}
Initial program 99.8%
*-lft-identity99.8%
*-lft-identity99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
*-commutative99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
Taylor expanded in rand around 0 69.1%
Taylor expanded in a around inf 67.8%
(FPCore (a rand) :precision binary64 -0.3333333333333333)
double code(double a, double rand) {
return -0.3333333333333333;
}
real(8) function code(a, rand)
real(8), intent (in) :: a
real(8), intent (in) :: rand
code = -0.3333333333333333d0
end function
public static double code(double a, double rand) {
return -0.3333333333333333;
}
def code(a, rand): return -0.3333333333333333
function code(a, rand) return -0.3333333333333333 end
function tmp = code(a, rand) tmp = -0.3333333333333333; end
code[a_, rand_] := -0.3333333333333333
\begin{array}{l}
\\
-0.3333333333333333
\end{array}
Initial program 99.8%
*-lft-identity99.8%
*-lft-identity99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
*-commutative99.8%
sub-neg99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
Taylor expanded in rand around 0 69.1%
Taylor expanded in a around 0 1.4%
herbie shell --seed 2024117
(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))))