pow(2, 1/4) * sqrt(1 / 2^size)

Percentage Accurate: 100.0% → 100.0%
Time: 4.4s
Alternatives: 2
Speedup: 2.2×

Specification

?
\[0 \leq size \land size \leq 6\]
\[\begin{array}{l} \\ {2}^{\left(\frac{1}{4}\right)} \cdot \sqrt{\frac{1}{{2}^{size}}} \end{array} \]
(FPCore (size)
 :precision binary64
 (* (pow 2.0 (/ 1.0 4.0)) (sqrt (/ 1.0 (pow 2.0 size)))))
double code(double size) {
	return pow(2.0, (1.0 / 4.0)) * sqrt((1.0 / pow(2.0, size)));
}
real(8) function code(size)
    real(8), intent (in) :: size
    code = (2.0d0 ** (1.0d0 / 4.0d0)) * sqrt((1.0d0 / (2.0d0 ** size)))
end function
public static double code(double size) {
	return Math.pow(2.0, (1.0 / 4.0)) * Math.sqrt((1.0 / Math.pow(2.0, size)));
}
def code(size):
	return math.pow(2.0, (1.0 / 4.0)) * math.sqrt((1.0 / math.pow(2.0, size)))
function code(size)
	return Float64((2.0 ^ Float64(1.0 / 4.0)) * sqrt(Float64(1.0 / (2.0 ^ size))))
end
function tmp = code(size)
	tmp = (2.0 ^ (1.0 / 4.0)) * sqrt((1.0 / (2.0 ^ size)));
end
code[size_] := N[(N[Power[2.0, N[(1.0 / 4.0), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(1.0 / N[Power[2.0, size], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
{2}^{\left(\frac{1}{4}\right)} \cdot \sqrt{\frac{1}{{2}^{size}}}
\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 2 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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ {2}^{\left(\frac{1}{4}\right)} \cdot \sqrt{\frac{1}{{2}^{size}}} \end{array} \]
(FPCore (size)
 :precision binary64
 (* (pow 2.0 (/ 1.0 4.0)) (sqrt (/ 1.0 (pow 2.0 size)))))
double code(double size) {
	return pow(2.0, (1.0 / 4.0)) * sqrt((1.0 / pow(2.0, size)));
}
real(8) function code(size)
    real(8), intent (in) :: size
    code = (2.0d0 ** (1.0d0 / 4.0d0)) * sqrt((1.0d0 / (2.0d0 ** size)))
end function
public static double code(double size) {
	return Math.pow(2.0, (1.0 / 4.0)) * Math.sqrt((1.0 / Math.pow(2.0, size)));
}
def code(size):
	return math.pow(2.0, (1.0 / 4.0)) * math.sqrt((1.0 / math.pow(2.0, size)))
function code(size)
	return Float64((2.0 ^ Float64(1.0 / 4.0)) * sqrt(Float64(1.0 / (2.0 ^ size))))
end
function tmp = code(size)
	tmp = (2.0 ^ (1.0 / 4.0)) * sqrt((1.0 / (2.0 ^ size)));
end
code[size_] := N[(N[Power[2.0, N[(1.0 / 4.0), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(1.0 / N[Power[2.0, size], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
{2}^{\left(\frac{1}{4}\right)} \cdot \sqrt{\frac{1}{{2}^{size}}}
\end{array}

Alternative 1: 100.0% accurate, 2.2× speedup?

\[\begin{array}{l} \\ {2}^{\left(\mathsf{fma}\left(size, -0.5, 0.25\right)\right)} \end{array} \]
(FPCore (size) :precision binary64 (pow 2.0 (fma size -0.5 0.25)))
double code(double size) {
	return pow(2.0, fma(size, -0.5, 0.25));
}
function code(size)
	return 2.0 ^ fma(size, -0.5, 0.25)
end
code[size_] := N[Power[2.0, N[(size * -0.5 + 0.25), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
{2}^{\left(\mathsf{fma}\left(size, -0.5, 0.25\right)\right)}
\end{array}
Derivation
  1. Initial program 100.0%

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

      \[\leadsto \color{blue}{{2}^{\left(\frac{1}{4}\right)} \cdot \sqrt{\frac{1}{{2}^{size}}}} \]
    2. *-commutativeN/A

      \[\leadsto \color{blue}{\sqrt{\frac{1}{{2}^{size}}} \cdot {2}^{\left(\frac{1}{4}\right)}} \]
    3. lift-sqrt.f64N/A

      \[\leadsto \color{blue}{\sqrt{\frac{1}{{2}^{size}}}} \cdot {2}^{\left(\frac{1}{4}\right)} \]
    4. lift-/.f64N/A

      \[\leadsto \sqrt{\color{blue}{\frac{1}{{2}^{size}}}} \cdot {2}^{\left(\frac{1}{4}\right)} \]
    5. lift-pow.f64N/A

      \[\leadsto \sqrt{\frac{1}{\color{blue}{{2}^{size}}}} \cdot {2}^{\left(\frac{1}{4}\right)} \]
    6. pow-flipN/A

      \[\leadsto \sqrt{\color{blue}{{2}^{\left(\mathsf{neg}\left(size\right)\right)}}} \cdot {2}^{\left(\frac{1}{4}\right)} \]
    7. sqrt-pow1N/A

      \[\leadsto \color{blue}{{2}^{\left(\frac{\mathsf{neg}\left(size\right)}{2}\right)}} \cdot {2}^{\left(\frac{1}{4}\right)} \]
    8. lift-pow.f64N/A

      \[\leadsto {2}^{\left(\frac{\mathsf{neg}\left(size\right)}{2}\right)} \cdot \color{blue}{{2}^{\left(\frac{1}{4}\right)}} \]
    9. pow-prod-upN/A

      \[\leadsto \color{blue}{{2}^{\left(\frac{\mathsf{neg}\left(size\right)}{2} + \frac{1}{4}\right)}} \]
    10. lower-pow.f64N/A

      \[\leadsto \color{blue}{{2}^{\left(\frac{\mathsf{neg}\left(size\right)}{2} + \frac{1}{4}\right)}} \]
    11. div-invN/A

      \[\leadsto {2}^{\left(\color{blue}{\left(\mathsf{neg}\left(size\right)\right) \cdot \frac{1}{2}} + \frac{1}{4}\right)} \]
    12. metadata-evalN/A

      \[\leadsto {2}^{\left(\left(\mathsf{neg}\left(size\right)\right) \cdot \color{blue}{\frac{1}{2}} + \frac{1}{4}\right)} \]
    13. lower-fma.f64N/A

      \[\leadsto {2}^{\color{blue}{\left(\mathsf{fma}\left(\mathsf{neg}\left(size\right), \frac{1}{2}, \frac{1}{4}\right)\right)}} \]
    14. lower-neg.f64100.0

      \[\leadsto {2}^{\left(\mathsf{fma}\left(\color{blue}{-size}, 0.5, \frac{1}{4}\right)\right)} \]
    15. lift-/.f64N/A

      \[\leadsto {2}^{\left(\mathsf{fma}\left(-size, \frac{1}{2}, \color{blue}{\frac{1}{4}}\right)\right)} \]
    16. metadata-eval100.0

      \[\leadsto {2}^{\left(\mathsf{fma}\left(-size, 0.5, \color{blue}{0.25}\right)\right)} \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{{2}^{\left(\mathsf{fma}\left(-size, 0.5, 0.25\right)\right)}} \]
  5. Step-by-step derivation
    1. metadata-evalN/A

      \[\leadsto {2}^{\left(\mathsf{fma}\left(-size, \frac{1}{2}, \color{blue}{\frac{1}{4}}\right)\right)} \]
    2. lift-fma.f64N/A

      \[\leadsto {2}^{\color{blue}{\left(\left(-size\right) \cdot \frac{1}{2} + \frac{1}{4}\right)}} \]
    3. lift-neg.f64N/A

      \[\leadsto {2}^{\left(\color{blue}{\left(\mathsf{neg}\left(size\right)\right)} \cdot \frac{1}{2} + \frac{1}{4}\right)} \]
    4. neg-mul-1N/A

      \[\leadsto {2}^{\left(\color{blue}{\left(-1 \cdot size\right)} \cdot \frac{1}{2} + \frac{1}{4}\right)} \]
    5. *-commutativeN/A

      \[\leadsto {2}^{\left(\color{blue}{\left(size \cdot -1\right)} \cdot \frac{1}{2} + \frac{1}{4}\right)} \]
    6. associate-*l*N/A

      \[\leadsto {2}^{\left(\color{blue}{size \cdot \left(-1 \cdot \frac{1}{2}\right)} + \frac{1}{4}\right)} \]
    7. lower-fma.f64N/A

      \[\leadsto {2}^{\color{blue}{\left(\mathsf{fma}\left(size, -1 \cdot \frac{1}{2}, \frac{1}{4}\right)\right)}} \]
    8. metadata-evalN/A

      \[\leadsto {2}^{\left(\mathsf{fma}\left(size, \color{blue}{\frac{-1}{2}}, \frac{1}{4}\right)\right)} \]
    9. metadata-eval100.0

      \[\leadsto {2}^{\left(\mathsf{fma}\left(size, -0.5, \color{blue}{0.25}\right)\right)} \]
  6. Applied rewrites100.0%

    \[\leadsto {2}^{\color{blue}{\left(\mathsf{fma}\left(size, -0.5, 0.25\right)\right)}} \]
  7. Add Preprocessing

Alternative 2: 97.7% accurate, 2.4× speedup?

\[\begin{array}{l} \\ {2}^{0.25} \end{array} \]
(FPCore (size) :precision binary64 (pow 2.0 0.25))
double code(double size) {
	return pow(2.0, 0.25);
}
real(8) function code(size)
    real(8), intent (in) :: size
    code = 2.0d0 ** 0.25d0
end function
public static double code(double size) {
	return Math.pow(2.0, 0.25);
}
def code(size):
	return math.pow(2.0, 0.25)
function code(size)
	return 2.0 ^ 0.25
end
function tmp = code(size)
	tmp = 2.0 ^ 0.25;
end
code[size_] := N[Power[2.0, 0.25], $MachinePrecision]
\begin{array}{l}

\\
{2}^{0.25}
\end{array}
Derivation
  1. Initial program 100.0%

    \[{2}^{\left(\frac{1}{4}\right)} \cdot \sqrt{\frac{1}{{2}^{size}}} \]
  2. Add Preprocessing
  3. Taylor expanded in size around 0

    \[\leadsto \color{blue}{{2}^{\frac{1}{4}}} \]
  4. Step-by-step derivation
    1. lower-pow.f6497.5

      \[\leadsto \color{blue}{{2}^{0.25}} \]
  5. Applied rewrites97.5%

    \[\leadsto \color{blue}{{2}^{0.25}} \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 1 
(FPCore (size)
  :name "pow(2, 1/4) * sqrt(1 / 2^size)"
  :precision binary64
  :pre (and (<= 0.0 size) (<= size 6.0))
  (* (pow 2.0 (/ 1.0 4.0)) (sqrt (/ 1.0 (pow 2.0 size)))))