normal distribution

Percentage Accurate: 99.4% → 99.6%
Time: 11.8s
Alternatives: 11
Speedup: 0.8×

Specification

?
\[\left(0 \leq u1 \land u1 \leq 1\right) \land \left(0 \leq u2 \land u2 \leq 1\right)\]
\[\begin{array}{l} \\ \left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \end{array} \]
(FPCore (u1 u2)
 :precision binary64
 (+
  (* (* (/ 1.0 6.0) (pow (* -2.0 (log u1)) 0.5)) (cos (* (* 2.0 PI) u2)))
  0.5))
double code(double u1, double u2) {
	return (((1.0 / 6.0) * pow((-2.0 * log(u1)), 0.5)) * cos(((2.0 * ((double) M_PI)) * u2))) + 0.5;
}
public static double code(double u1, double u2) {
	return (((1.0 / 6.0) * Math.pow((-2.0 * Math.log(u1)), 0.5)) * Math.cos(((2.0 * Math.PI) * u2))) + 0.5;
}
def code(u1, u2):
	return (((1.0 / 6.0) * math.pow((-2.0 * math.log(u1)), 0.5)) * math.cos(((2.0 * math.pi) * u2))) + 0.5
function code(u1, u2)
	return Float64(Float64(Float64(Float64(1.0 / 6.0) * (Float64(-2.0 * log(u1)) ^ 0.5)) * cos(Float64(Float64(2.0 * pi) * u2))) + 0.5)
end
function tmp = code(u1, u2)
	tmp = (((1.0 / 6.0) * ((-2.0 * log(u1)) ^ 0.5)) * cos(((2.0 * pi) * u2))) + 0.5;
end
code[u1_, u2_] := N[(N[(N[(N[(1.0 / 6.0), $MachinePrecision] * N[Power[N[(-2.0 * N[Log[u1], $MachinePrecision]), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}

\\
\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5
\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 11 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.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \end{array} \]
(FPCore (u1 u2)
 :precision binary64
 (+
  (* (* (/ 1.0 6.0) (pow (* -2.0 (log u1)) 0.5)) (cos (* (* 2.0 PI) u2)))
  0.5))
double code(double u1, double u2) {
	return (((1.0 / 6.0) * pow((-2.0 * log(u1)), 0.5)) * cos(((2.0 * ((double) M_PI)) * u2))) + 0.5;
}
public static double code(double u1, double u2) {
	return (((1.0 / 6.0) * Math.pow((-2.0 * Math.log(u1)), 0.5)) * Math.cos(((2.0 * Math.PI) * u2))) + 0.5;
}
def code(u1, u2):
	return (((1.0 / 6.0) * math.pow((-2.0 * math.log(u1)), 0.5)) * math.cos(((2.0 * math.pi) * u2))) + 0.5
function code(u1, u2)
	return Float64(Float64(Float64(Float64(1.0 / 6.0) * (Float64(-2.0 * log(u1)) ^ 0.5)) * cos(Float64(Float64(2.0 * pi) * u2))) + 0.5)
end
function tmp = code(u1, u2)
	tmp = (((1.0 / 6.0) * ((-2.0 * log(u1)) ^ 0.5)) * cos(((2.0 * pi) * u2))) + 0.5;
end
code[u1_, u2_] := N[(N[(N[(N[(1.0 / 6.0), $MachinePrecision] * N[Power[N[(-2.0 * N[Log[u1], $MachinePrecision]), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}

\\
\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5
\end{array}

Alternative 1: 99.6% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \left(\sqrt{-\log u1} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \end{array} \]
(FPCore (u1 u2)
 :precision binary64
 (+
  (*
   (* (sqrt (- (log u1))) (* (sqrt 2.0) 0.16666666666666666))
   (cos (* (* 2.0 PI) u2)))
  0.5))
double code(double u1, double u2) {
	return ((sqrt(-log(u1)) * (sqrt(2.0) * 0.16666666666666666)) * cos(((2.0 * ((double) M_PI)) * u2))) + 0.5;
}
public static double code(double u1, double u2) {
	return ((Math.sqrt(-Math.log(u1)) * (Math.sqrt(2.0) * 0.16666666666666666)) * Math.cos(((2.0 * Math.PI) * u2))) + 0.5;
}
def code(u1, u2):
	return ((math.sqrt(-math.log(u1)) * (math.sqrt(2.0) * 0.16666666666666666)) * math.cos(((2.0 * math.pi) * u2))) + 0.5
function code(u1, u2)
	return Float64(Float64(Float64(sqrt(Float64(-log(u1))) * Float64(sqrt(2.0) * 0.16666666666666666)) * cos(Float64(Float64(2.0 * pi) * u2))) + 0.5)
end
function tmp = code(u1, u2)
	tmp = ((sqrt(-log(u1)) * (sqrt(2.0) * 0.16666666666666666)) * cos(((2.0 * pi) * u2))) + 0.5;
end
code[u1_, u2_] := N[(N[(N[(N[Sqrt[(-N[Log[u1], $MachinePrecision])], $MachinePrecision] * N[(N[Sqrt[2.0], $MachinePrecision] * 0.16666666666666666), $MachinePrecision]), $MachinePrecision] * N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}

\\
\left(\sqrt{-\log u1} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Taylor expanded in u1 around inf 99.6%

    \[\leadsto \color{blue}{\left(0.16666666666666666 \cdot \left(\sqrt{\log \left(\frac{1}{u1}\right)} \cdot \sqrt{2}\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  3. Step-by-step derivation
    1. *-commutative99.6%

      \[\leadsto \color{blue}{\left(\left(\sqrt{\log \left(\frac{1}{u1}\right)} \cdot \sqrt{2}\right) \cdot 0.16666666666666666\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    2. associate-*l*99.7%

      \[\leadsto \color{blue}{\left(\sqrt{\log \left(\frac{1}{u1}\right)} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    3. log-rec99.7%

      \[\leadsto \left(\sqrt{\color{blue}{-\log u1}} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  4. Simplified99.7%

    \[\leadsto \color{blue}{\left(\sqrt{-\log u1} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  5. Final simplification99.7%

    \[\leadsto \left(\sqrt{-\log u1} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]

Alternative 2: 99.4% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(0.16666666666666666, \sqrt{\log u1 \cdot -2} \cdot \cos \left(2 \cdot \left(\pi \cdot u2\right)\right), 0.5\right) \end{array} \]
(FPCore (u1 u2)
 :precision binary64
 (fma
  0.16666666666666666
  (* (sqrt (* (log u1) -2.0)) (cos (* 2.0 (* PI u2))))
  0.5))
double code(double u1, double u2) {
	return fma(0.16666666666666666, (sqrt((log(u1) * -2.0)) * cos((2.0 * (((double) M_PI) * u2)))), 0.5);
}
function code(u1, u2)
	return fma(0.16666666666666666, Float64(sqrt(Float64(log(u1) * -2.0)) * cos(Float64(2.0 * Float64(pi * u2)))), 0.5)
end
code[u1_, u2_] := N[(0.16666666666666666 * N[(N[Sqrt[N[(N[Log[u1], $MachinePrecision] * -2.0), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(2.0 * N[(Pi * u2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(0.16666666666666666, \sqrt{\log u1 \cdot -2} \cdot \cos \left(2 \cdot \left(\pi \cdot u2\right)\right), 0.5\right)
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Step-by-step derivation
    1. associate-*l*99.4%

      \[\leadsto \color{blue}{\frac{1}{6} \cdot \left({\left(-2 \cdot \log u1\right)}^{0.5} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\right)} + 0.5 \]
    2. fma-def99.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{6}, {\left(-2 \cdot \log u1\right)}^{0.5} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right), 0.5\right)} \]
    3. cos-neg99.4%

      \[\leadsto \mathsf{fma}\left(\frac{1}{6}, {\left(-2 \cdot \log u1\right)}^{0.5} \cdot \color{blue}{\cos \left(-\left(2 \cdot \pi\right) \cdot u2\right)}, 0.5\right) \]
    4. distribute-rgt-neg-out99.4%

      \[\leadsto \mathsf{fma}\left(\frac{1}{6}, {\left(-2 \cdot \log u1\right)}^{0.5} \cdot \cos \color{blue}{\left(\left(2 \cdot \pi\right) \cdot \left(-u2\right)\right)}, 0.5\right) \]
    5. metadata-eval99.4%

      \[\leadsto \mathsf{fma}\left(\color{blue}{0.16666666666666666}, {\left(-2 \cdot \log u1\right)}^{0.5} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot \left(-u2\right)\right), 0.5\right) \]
    6. unpow1/299.4%

      \[\leadsto \mathsf{fma}\left(0.16666666666666666, \color{blue}{\sqrt{-2 \cdot \log u1}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot \left(-u2\right)\right), 0.5\right) \]
    7. distribute-rgt-neg-out99.4%

      \[\leadsto \mathsf{fma}\left(0.16666666666666666, \sqrt{-2 \cdot \log u1} \cdot \cos \color{blue}{\left(-\left(2 \cdot \pi\right) \cdot u2\right)}, 0.5\right) \]
    8. cos-neg99.4%

      \[\leadsto \mathsf{fma}\left(0.16666666666666666, \sqrt{-2 \cdot \log u1} \cdot \color{blue}{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)}, 0.5\right) \]
    9. associate-*l*99.4%

      \[\leadsto \mathsf{fma}\left(0.16666666666666666, \sqrt{-2 \cdot \log u1} \cdot \cos \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)}, 0.5\right) \]
  3. Simplified99.4%

    \[\leadsto \color{blue}{\mathsf{fma}\left(0.16666666666666666, \sqrt{-2 \cdot \log u1} \cdot \cos \left(2 \cdot \left(\pi \cdot u2\right)\right), 0.5\right)} \]
  4. Final simplification99.4%

    \[\leadsto \mathsf{fma}\left(0.16666666666666666, \sqrt{\log u1 \cdot -2} \cdot \cos \left(2 \cdot \left(\pi \cdot u2\right)\right), 0.5\right) \]

Alternative 3: 99.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 + \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \left(0.16666666666666666 \cdot \sqrt{\log u1 \cdot -2}\right) \end{array} \]
(FPCore (u1 u2)
 :precision binary64
 (+
  0.5
  (*
   (cos (* (* 2.0 PI) u2))
   (* 0.16666666666666666 (sqrt (* (log u1) -2.0))))))
double code(double u1, double u2) {
	return 0.5 + (cos(((2.0 * ((double) M_PI)) * u2)) * (0.16666666666666666 * sqrt((log(u1) * -2.0))));
}
public static double code(double u1, double u2) {
	return 0.5 + (Math.cos(((2.0 * Math.PI) * u2)) * (0.16666666666666666 * Math.sqrt((Math.log(u1) * -2.0))));
}
def code(u1, u2):
	return 0.5 + (math.cos(((2.0 * math.pi) * u2)) * (0.16666666666666666 * math.sqrt((math.log(u1) * -2.0))))
function code(u1, u2)
	return Float64(0.5 + Float64(cos(Float64(Float64(2.0 * pi) * u2)) * Float64(0.16666666666666666 * sqrt(Float64(log(u1) * -2.0)))))
end
function tmp = code(u1, u2)
	tmp = 0.5 + (cos(((2.0 * pi) * u2)) * (0.16666666666666666 * sqrt((log(u1) * -2.0))));
end
code[u1_, u2_] := N[(0.5 + N[(N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision] * N[(0.16666666666666666 * N[Sqrt[N[(N[Log[u1], $MachinePrecision] * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 + \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \left(0.16666666666666666 \cdot \sqrt{\log u1 \cdot -2}\right)
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Step-by-step derivation
    1. log1p-expm1-u_binary6499.4%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right)\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5} \]
  3. Applied rewrite-once99.4%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  4. Step-by-step derivation
    1. log1p-expm199.4%

      \[\leadsto \color{blue}{\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    2. metadata-eval99.4%

      \[\leadsto \left(\color{blue}{0.16666666666666666} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    3. unpow1/299.4%

      \[\leadsto \left(0.16666666666666666 \cdot \color{blue}{\sqrt{-2 \cdot \log u1}}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    4. *-commutative99.4%

      \[\leadsto \left(0.16666666666666666 \cdot \sqrt{\color{blue}{\log u1 \cdot -2}}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  5. Simplified99.4%

    \[\leadsto \color{blue}{\left(0.16666666666666666 \cdot \sqrt{\log u1 \cdot -2}\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  6. Final simplification99.4%

    \[\leadsto 0.5 + \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \left(0.16666666666666666 \cdot \sqrt{\log u1 \cdot -2}\right) \]

Alternative 4: 98.3% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \sqrt{-\log u1} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right) + 0.5 \end{array} \]
(FPCore (u1 u2)
 :precision binary64
 (+ (* (sqrt (- (log u1))) (* (sqrt 2.0) 0.16666666666666666)) 0.5))
double code(double u1, double u2) {
	return (sqrt(-log(u1)) * (sqrt(2.0) * 0.16666666666666666)) + 0.5;
}
real(8) function code(u1, u2)
    real(8), intent (in) :: u1
    real(8), intent (in) :: u2
    code = (sqrt(-log(u1)) * (sqrt(2.0d0) * 0.16666666666666666d0)) + 0.5d0
end function
public static double code(double u1, double u2) {
	return (Math.sqrt(-Math.log(u1)) * (Math.sqrt(2.0) * 0.16666666666666666)) + 0.5;
}
def code(u1, u2):
	return (math.sqrt(-math.log(u1)) * (math.sqrt(2.0) * 0.16666666666666666)) + 0.5
function code(u1, u2)
	return Float64(Float64(sqrt(Float64(-log(u1))) * Float64(sqrt(2.0) * 0.16666666666666666)) + 0.5)
end
function tmp = code(u1, u2)
	tmp = (sqrt(-log(u1)) * (sqrt(2.0) * 0.16666666666666666)) + 0.5;
end
code[u1_, u2_] := N[(N[(N[Sqrt[(-N[Log[u1], $MachinePrecision])], $MachinePrecision] * N[(N[Sqrt[2.0], $MachinePrecision] * 0.16666666666666666), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}

\\
\sqrt{-\log u1} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right) + 0.5
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Taylor expanded in u1 around inf 99.6%

    \[\leadsto \color{blue}{\left(0.16666666666666666 \cdot \left(\sqrt{\log \left(\frac{1}{u1}\right)} \cdot \sqrt{2}\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  3. Step-by-step derivation
    1. *-commutative99.6%

      \[\leadsto \color{blue}{\left(\left(\sqrt{\log \left(\frac{1}{u1}\right)} \cdot \sqrt{2}\right) \cdot 0.16666666666666666\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    2. associate-*l*99.7%

      \[\leadsto \color{blue}{\left(\sqrt{\log \left(\frac{1}{u1}\right)} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    3. log-rec99.7%

      \[\leadsto \left(\sqrt{\color{blue}{-\log u1}} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  4. Simplified99.7%

    \[\leadsto \color{blue}{\left(\sqrt{-\log u1} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  5. Taylor expanded in u2 around 0 97.8%

    \[\leadsto \left(\sqrt{-\log u1} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right)\right) \cdot \color{blue}{1} + 0.5 \]
  6. Final simplification97.8%

    \[\leadsto \sqrt{-\log u1} \cdot \left(\sqrt{2} \cdot 0.16666666666666666\right) + 0.5 \]

Alternative 5: 21.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ 0.5 + \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot 4 \end{array} \]
(FPCore (u1 u2) :precision binary64 (+ 0.5 (* (cos (* (* 2.0 PI) u2)) 4.0)))
double code(double u1, double u2) {
	return 0.5 + (cos(((2.0 * ((double) M_PI)) * u2)) * 4.0);
}
public static double code(double u1, double u2) {
	return 0.5 + (Math.cos(((2.0 * Math.PI) * u2)) * 4.0);
}
def code(u1, u2):
	return 0.5 + (math.cos(((2.0 * math.pi) * u2)) * 4.0)
function code(u1, u2)
	return Float64(0.5 + Float64(cos(Float64(Float64(2.0 * pi) * u2)) * 4.0))
end
function tmp = code(u1, u2)
	tmp = 0.5 + (cos(((2.0 * pi) * u2)) * 4.0);
end
code[u1_, u2_] := N[(0.5 + N[(N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision] * 4.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 + \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot 4
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Applied egg-rr21.8%

    \[\leadsto \color{blue}{4} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  3. Final simplification21.8%

    \[\leadsto 0.5 + \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot 4 \]

Alternative 6: 98.2% accurate, 2.0× speedup?

\[\begin{array}{l} \\ 0.5 + 0.16666666666666666 \cdot \sqrt{\log u1 \cdot -2} \end{array} \]
(FPCore (u1 u2)
 :precision binary64
 (+ 0.5 (* 0.16666666666666666 (sqrt (* (log u1) -2.0)))))
double code(double u1, double u2) {
	return 0.5 + (0.16666666666666666 * sqrt((log(u1) * -2.0)));
}
real(8) function code(u1, u2)
    real(8), intent (in) :: u1
    real(8), intent (in) :: u2
    code = 0.5d0 + (0.16666666666666666d0 * sqrt((log(u1) * (-2.0d0))))
end function
public static double code(double u1, double u2) {
	return 0.5 + (0.16666666666666666 * Math.sqrt((Math.log(u1) * -2.0)));
}
def code(u1, u2):
	return 0.5 + (0.16666666666666666 * math.sqrt((math.log(u1) * -2.0)))
function code(u1, u2)
	return Float64(0.5 + Float64(0.16666666666666666 * sqrt(Float64(log(u1) * -2.0))))
end
function tmp = code(u1, u2)
	tmp = 0.5 + (0.16666666666666666 * sqrt((log(u1) * -2.0)));
end
code[u1_, u2_] := N[(0.5 + N[(0.16666666666666666 * N[Sqrt[N[(N[Log[u1], $MachinePrecision] * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 + 0.16666666666666666 \cdot \sqrt{\log u1 \cdot -2}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Step-by-step derivation
    1. log1p-expm1-u_binary6499.4%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right)\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5} \]
  3. Applied rewrite-once99.4%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  4. Step-by-step derivation
    1. log1p-expm199.4%

      \[\leadsto \color{blue}{\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    2. metadata-eval99.4%

      \[\leadsto \left(\color{blue}{0.16666666666666666} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    3. unpow1/299.4%

      \[\leadsto \left(0.16666666666666666 \cdot \color{blue}{\sqrt{-2 \cdot \log u1}}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
    4. *-commutative99.4%

      \[\leadsto \left(0.16666666666666666 \cdot \sqrt{\color{blue}{\log u1 \cdot -2}}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  5. Simplified99.4%

    \[\leadsto \color{blue}{\left(0.16666666666666666 \cdot \sqrt{\log u1 \cdot -2}\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  6. Taylor expanded in u2 around 0 97.5%

    \[\leadsto \left(0.16666666666666666 \cdot \sqrt{\log u1 \cdot -2}\right) \cdot \color{blue}{1} + 0.5 \]
  7. Final simplification97.5%

    \[\leadsto 0.5 + 0.16666666666666666 \cdot \sqrt{\log u1 \cdot -2} \]

Alternative 7: 17.6% accurate, 415.0× speedup?

\[\begin{array}{l} \\ 1.25 \end{array} \]
(FPCore (u1 u2) :precision binary64 1.25)
double code(double u1, double u2) {
	return 1.25;
}
real(8) function code(u1, u2)
    real(8), intent (in) :: u1
    real(8), intent (in) :: u2
    code = 1.25d0
end function
public static double code(double u1, double u2) {
	return 1.25;
}
def code(u1, u2):
	return 1.25
function code(u1, u2)
	return 1.25
end
function tmp = code(u1, u2)
	tmp = 1.25;
end
code[u1_, u2_] := 1.25
\begin{array}{l}

\\
1.25
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Applied egg-rr17.5%

    \[\leadsto \color{blue}{0.75} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  3. Taylor expanded in u2 around 0 17.5%

    \[\leadsto \color{blue}{1.25} \]
  4. Final simplification17.5%

    \[\leadsto 1.25 \]

Alternative 8: 18.0% accurate, 415.0× speedup?

\[\begin{array}{l} \\ 1.5 \end{array} \]
(FPCore (u1 u2) :precision binary64 1.5)
double code(double u1, double u2) {
	return 1.5;
}
real(8) function code(u1, u2)
    real(8), intent (in) :: u1
    real(8), intent (in) :: u2
    code = 1.5d0
end function
public static double code(double u1, double u2) {
	return 1.5;
}
def code(u1, u2):
	return 1.5
function code(u1, u2)
	return 1.5
end
function tmp = code(u1, u2)
	tmp = 1.5;
end
code[u1_, u2_] := 1.5
\begin{array}{l}

\\
1.5
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Applied egg-rr17.9%

    \[\leadsto \color{blue}{1} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  3. Taylor expanded in u2 around 0 17.9%

    \[\leadsto \color{blue}{1.5} \]
  4. Final simplification17.9%

    \[\leadsto 1.5 \]

Alternative 9: 18.2% accurate, 415.0× speedup?

\[\begin{array}{l} \\ 1.625 \end{array} \]
(FPCore (u1 u2) :precision binary64 1.625)
double code(double u1, double u2) {
	return 1.625;
}
real(8) function code(u1, u2)
    real(8), intent (in) :: u1
    real(8), intent (in) :: u2
    code = 1.625d0
end function
public static double code(double u1, double u2) {
	return 1.625;
}
def code(u1, u2):
	return 1.625
function code(u1, u2)
	return 1.625
end
function tmp = code(u1, u2)
	tmp = 1.625;
end
code[u1_, u2_] := 1.625
\begin{array}{l}

\\
1.625
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Applied egg-rr18.1%

    \[\leadsto \color{blue}{1.125} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  3. Taylor expanded in u2 around 0 18.1%

    \[\leadsto \color{blue}{1.625} \]
  4. Final simplification18.1%

    \[\leadsto 1.625 \]

Alternative 10: 19.4% accurate, 415.0× speedup?

\[\begin{array}{l} \\ 2.5 \end{array} \]
(FPCore (u1 u2) :precision binary64 2.5)
double code(double u1, double u2) {
	return 2.5;
}
real(8) function code(u1, u2)
    real(8), intent (in) :: u1
    real(8), intent (in) :: u2
    code = 2.5d0
end function
public static double code(double u1, double u2) {
	return 2.5;
}
def code(u1, u2):
	return 2.5
function code(u1, u2)
	return 2.5
end
function tmp = code(u1, u2)
	tmp = 2.5;
end
code[u1_, u2_] := 2.5
\begin{array}{l}

\\
2.5
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Applied egg-rr19.2%

    \[\leadsto \color{blue}{2} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  3. Taylor expanded in u2 around 0 19.3%

    \[\leadsto \color{blue}{2.5} \]
  4. Final simplification19.3%

    \[\leadsto 2.5 \]

Alternative 11: 21.6% accurate, 415.0× speedup?

\[\begin{array}{l} \\ 4.5 \end{array} \]
(FPCore (u1 u2) :precision binary64 4.5)
double code(double u1, double u2) {
	return 4.5;
}
real(8) function code(u1, u2)
    real(8), intent (in) :: u1
    real(8), intent (in) :: u2
    code = 4.5d0
end function
public static double code(double u1, double u2) {
	return 4.5;
}
def code(u1, u2):
	return 4.5
function code(u1, u2)
	return 4.5
end
function tmp = code(u1, u2)
	tmp = 4.5;
end
code[u1_, u2_] := 4.5
\begin{array}{l}

\\
4.5
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  2. Applied egg-rr21.8%

    \[\leadsto \color{blue}{4} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5 \]
  3. Taylor expanded in u2 around 0 21.8%

    \[\leadsto \color{blue}{4.5} \]
  4. Final simplification21.8%

    \[\leadsto 4.5 \]

Reproduce

?
herbie shell --seed 2023297 
(FPCore (u1 u2)
  :name "normal distribution"
  :precision binary64
  :pre (and (and (<= 0.0 u1) (<= u1 1.0)) (and (<= 0.0 u2) (<= u2 1.0)))
  (+ (* (* (/ 1.0 6.0) (pow (* -2.0 (log u1)) 0.5)) (cos (* (* 2.0 PI) u2))) 0.5))