Optimal throwing angle

Percentage Accurate: 67.5% → 98.9%
Time: 7.6s
Alternatives: 6
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \left(2 \cdot 9.8\right) \cdot H}}\right) \end{array} \]
(FPCore (v H)
 :precision binary64
 (atan (/ v (sqrt (- (* v v) (* (* 2.0 9.8) H))))))
double code(double v, double H) {
	return atan((v / sqrt(((v * v) - ((2.0 * 9.8) * H)))));
}
real(8) function code(v, h)
    real(8), intent (in) :: v
    real(8), intent (in) :: h
    code = atan((v / sqrt(((v * v) - ((2.0d0 * 9.8d0) * h)))))
end function
public static double code(double v, double H) {
	return Math.atan((v / Math.sqrt(((v * v) - ((2.0 * 9.8) * H)))));
}
def code(v, H):
	return math.atan((v / math.sqrt(((v * v) - ((2.0 * 9.8) * H)))))
function code(v, H)
	return atan(Float64(v / sqrt(Float64(Float64(v * v) - Float64(Float64(2.0 * 9.8) * H)))))
end
function tmp = code(v, H)
	tmp = atan((v / sqrt(((v * v) - ((2.0 * 9.8) * H)))));
end
code[v_, H_] := N[ArcTan[N[(v / N[Sqrt[N[(N[(v * v), $MachinePrecision] - N[(N[(2.0 * 9.8), $MachinePrecision] * H), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \left(2 \cdot 9.8\right) \cdot H}}\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 6 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: 67.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \left(2 \cdot 9.8\right) \cdot H}}\right) \end{array} \]
(FPCore (v H)
 :precision binary64
 (atan (/ v (sqrt (- (* v v) (* (* 2.0 9.8) H))))))
double code(double v, double H) {
	return atan((v / sqrt(((v * v) - ((2.0 * 9.8) * H)))));
}
real(8) function code(v, h)
    real(8), intent (in) :: v
    real(8), intent (in) :: h
    code = atan((v / sqrt(((v * v) - ((2.0d0 * 9.8d0) * h)))))
end function
public static double code(double v, double H) {
	return Math.atan((v / Math.sqrt(((v * v) - ((2.0 * 9.8) * H)))));
}
def code(v, H):
	return math.atan((v / math.sqrt(((v * v) - ((2.0 * 9.8) * H)))))
function code(v, H)
	return atan(Float64(v / sqrt(Float64(Float64(v * v) - Float64(Float64(2.0 * 9.8) * H)))))
end
function tmp = code(v, H)
	tmp = atan((v / sqrt(((v * v) - ((2.0 * 9.8) * H)))));
end
code[v_, H_] := N[ArcTan[N[(v / N[Sqrt[N[(N[(v * v), $MachinePrecision] - N[(N[(2.0 * 9.8), $MachinePrecision] * H), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \left(2 \cdot 9.8\right) \cdot H}}\right)
\end{array}

Alternative 1: 98.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;v \leq -1 \cdot 10^{+154}:\\ \;\;\;\;\tan^{-1} -1\\ \mathbf{elif}\;v \leq 1.02 \cdot 10^{+62}:\\ \;\;\;\;\tan^{-1} \left(\frac{v}{\sqrt{\mathsf{fma}\left(H, -19.6, v \cdot v\right)}}\right)\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1} 1\\ \end{array} \end{array} \]
(FPCore (v H)
 :precision binary64
 (if (<= v -1e+154)
   (atan -1.0)
   (if (<= v 1.02e+62) (atan (/ v (sqrt (fma H -19.6 (* v v))))) (atan 1.0))))
double code(double v, double H) {
	double tmp;
	if (v <= -1e+154) {
		tmp = atan(-1.0);
	} else if (v <= 1.02e+62) {
		tmp = atan((v / sqrt(fma(H, -19.6, (v * v)))));
	} else {
		tmp = atan(1.0);
	}
	return tmp;
}
function code(v, H)
	tmp = 0.0
	if (v <= -1e+154)
		tmp = atan(-1.0);
	elseif (v <= 1.02e+62)
		tmp = atan(Float64(v / sqrt(fma(H, -19.6, Float64(v * v)))));
	else
		tmp = atan(1.0);
	end
	return tmp
end
code[v_, H_] := If[LessEqual[v, -1e+154], N[ArcTan[-1.0], $MachinePrecision], If[LessEqual[v, 1.02e+62], N[ArcTan[N[(v / N[Sqrt[N[(H * -19.6 + N[(v * v), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[ArcTan[1.0], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;v \leq -1 \cdot 10^{+154}:\\
\;\;\;\;\tan^{-1} -1\\

\mathbf{elif}\;v \leq 1.02 \cdot 10^{+62}:\\
\;\;\;\;\tan^{-1} \left(\frac{v}{\sqrt{\mathsf{fma}\left(H, -19.6, v \cdot v\right)}}\right)\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1} 1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if v < -1.00000000000000004e154

    1. Initial program 3.1%

      \[\tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \left(2 \cdot 9.8\right) \cdot H}}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in v around -inf

      \[\leadsto \tan^{-1} \color{blue}{-1} \]
    4. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto \tan^{-1} \color{blue}{-1} \]

      if -1.00000000000000004e154 < v < 1.02000000000000002e62

      1. Initial program 99.7%

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

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\color{blue}{v \cdot v - \left(2 \cdot \frac{49}{5}\right) \cdot H}}}\right) \]
        2. sub-negN/A

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\color{blue}{v \cdot v + \left(\mathsf{neg}\left(\left(2 \cdot \frac{49}{5}\right) \cdot H\right)\right)}}}\right) \]
        3. +-commutativeN/A

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\color{blue}{\left(\mathsf{neg}\left(\left(2 \cdot \frac{49}{5}\right) \cdot H\right)\right) + v \cdot v}}}\right) \]
        4. lift-*.f64N/A

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\left(\mathsf{neg}\left(\color{blue}{\left(2 \cdot \frac{49}{5}\right) \cdot H}\right)\right) + v \cdot v}}\right) \]
        5. *-commutativeN/A

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\left(\mathsf{neg}\left(\color{blue}{H \cdot \left(2 \cdot \frac{49}{5}\right)}\right)\right) + v \cdot v}}\right) \]
        6. distribute-rgt-neg-inN/A

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\color{blue}{H \cdot \left(\mathsf{neg}\left(2 \cdot \frac{49}{5}\right)\right)} + v \cdot v}}\right) \]
        7. lower-fma.f64N/A

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\color{blue}{\mathsf{fma}\left(H, \mathsf{neg}\left(2 \cdot \frac{49}{5}\right), v \cdot v\right)}}}\right) \]
        8. lift-*.f64N/A

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\mathsf{fma}\left(H, \mathsf{neg}\left(\color{blue}{2 \cdot \frac{49}{5}}\right), v \cdot v\right)}}\right) \]
        9. metadata-evalN/A

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\mathsf{fma}\left(H, \mathsf{neg}\left(\color{blue}{\frac{98}{5}}\right), v \cdot v\right)}}\right) \]
        10. metadata-eval99.8

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\mathsf{fma}\left(H, \color{blue}{-19.6}, v \cdot v\right)}}\right) \]
      4. Applied rewrites99.8%

        \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\color{blue}{\mathsf{fma}\left(H, -19.6, v \cdot v\right)}}}\right) \]

      if 1.02000000000000002e62 < v

      1. Initial program 44.6%

        \[\tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \left(2 \cdot 9.8\right) \cdot H}}\right) \]
      2. Add Preprocessing
      3. Taylor expanded in v around inf

        \[\leadsto \tan^{-1} \color{blue}{1} \]
      4. Step-by-step derivation
        1. Applied rewrites100.0%

          \[\leadsto \tan^{-1} \color{blue}{1} \]
      5. Recombined 3 regimes into one program.
      6. Add Preprocessing

      Alternative 2: 88.5% accurate, 1.0× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;v \leq -4.1 \cdot 10^{-43}:\\ \;\;\;\;\tan^{-1} -1\\ \mathbf{elif}\;v \leq 9.5 \cdot 10^{-12}:\\ \;\;\;\;\tan^{-1} \left(\frac{v}{\sqrt{H \cdot -19.6}}\right)\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1} \left(\frac{v}{\mathsf{fma}\left(H, \frac{-9.8}{v}, v\right)}\right)\\ \end{array} \end{array} \]
      (FPCore (v H)
       :precision binary64
       (if (<= v -4.1e-43)
         (atan -1.0)
         (if (<= v 9.5e-12)
           (atan (/ v (sqrt (* H -19.6))))
           (atan (/ v (fma H (/ -9.8 v) v))))))
      double code(double v, double H) {
      	double tmp;
      	if (v <= -4.1e-43) {
      		tmp = atan(-1.0);
      	} else if (v <= 9.5e-12) {
      		tmp = atan((v / sqrt((H * -19.6))));
      	} else {
      		tmp = atan((v / fma(H, (-9.8 / v), v)));
      	}
      	return tmp;
      }
      
      function code(v, H)
      	tmp = 0.0
      	if (v <= -4.1e-43)
      		tmp = atan(-1.0);
      	elseif (v <= 9.5e-12)
      		tmp = atan(Float64(v / sqrt(Float64(H * -19.6))));
      	else
      		tmp = atan(Float64(v / fma(H, Float64(-9.8 / v), v)));
      	end
      	return tmp
      end
      
      code[v_, H_] := If[LessEqual[v, -4.1e-43], N[ArcTan[-1.0], $MachinePrecision], If[LessEqual[v, 9.5e-12], N[ArcTan[N[(v / N[Sqrt[N[(H * -19.6), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[ArcTan[N[(v / N[(H * N[(-9.8 / v), $MachinePrecision] + v), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;v \leq -4.1 \cdot 10^{-43}:\\
      \;\;\;\;\tan^{-1} -1\\
      
      \mathbf{elif}\;v \leq 9.5 \cdot 10^{-12}:\\
      \;\;\;\;\tan^{-1} \left(\frac{v}{\sqrt{H \cdot -19.6}}\right)\\
      
      \mathbf{else}:\\
      \;\;\;\;\tan^{-1} \left(\frac{v}{\mathsf{fma}\left(H, \frac{-9.8}{v}, v\right)}\right)\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 3 regimes
      2. if v < -4.0999999999999998e-43

        1. Initial program 53.3%

          \[\tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \left(2 \cdot 9.8\right) \cdot H}}\right) \]
        2. Add Preprocessing
        3. Taylor expanded in v around -inf

          \[\leadsto \tan^{-1} \color{blue}{-1} \]
        4. Step-by-step derivation
          1. Applied rewrites89.8%

            \[\leadsto \tan^{-1} \color{blue}{-1} \]

          if -4.0999999999999998e-43 < v < 9.4999999999999995e-12

          1. Initial program 99.6%

            \[\tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \left(2 \cdot 9.8\right) \cdot H}}\right) \]
          2. Add Preprocessing
          3. Taylor expanded in v around 0

            \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\color{blue}{\frac{-98}{5} \cdot H}}}\right) \]
          4. Step-by-step derivation
            1. lower-*.f6484.1

              \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\color{blue}{-19.6 \cdot H}}}\right) \]
          5. Applied rewrites84.1%

            \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{\color{blue}{-19.6 \cdot H}}}\right) \]

          if 9.4999999999999995e-12 < v

          1. Initial program 49.4%

            \[\tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \left(2 \cdot 9.8\right) \cdot H}}\right) \]
          2. Add Preprocessing
          3. Taylor expanded in H around 0

            \[\leadsto \tan^{-1} \left(\frac{v}{\color{blue}{v + \frac{-49}{5} \cdot \frac{H}{v}}}\right) \]
          4. Step-by-step derivation
            1. +-commutativeN/A

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

              \[\leadsto \tan^{-1} \left(\frac{v}{\color{blue}{\frac{H}{v} \cdot \frac{-49}{5}} + v}\right) \]
            3. associate-*l/N/A

              \[\leadsto \tan^{-1} \left(\frac{v}{\color{blue}{\frac{H \cdot \frac{-49}{5}}{v}} + v}\right) \]
            4. associate-*r/N/A

              \[\leadsto \tan^{-1} \left(\frac{v}{\color{blue}{H \cdot \frac{\frac{-49}{5}}{v}} + v}\right) \]
            5. metadata-evalN/A

              \[\leadsto \tan^{-1} \left(\frac{v}{H \cdot \frac{\color{blue}{\mathsf{neg}\left(\frac{49}{5}\right)}}{v} + v}\right) \]
            6. distribute-neg-fracN/A

              \[\leadsto \tan^{-1} \left(\frac{v}{H \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{\frac{49}{5}}{v}\right)\right)} + v}\right) \]
            7. metadata-evalN/A

              \[\leadsto \tan^{-1} \left(\frac{v}{H \cdot \left(\mathsf{neg}\left(\frac{\color{blue}{\frac{49}{5} \cdot 1}}{v}\right)\right) + v}\right) \]
            8. associate-*r/N/A

              \[\leadsto \tan^{-1} \left(\frac{v}{H \cdot \left(\mathsf{neg}\left(\color{blue}{\frac{49}{5} \cdot \frac{1}{v}}\right)\right) + v}\right) \]
            9. lower-fma.f64N/A

              \[\leadsto \tan^{-1} \left(\frac{v}{\color{blue}{\mathsf{fma}\left(H, \mathsf{neg}\left(\frac{49}{5} \cdot \frac{1}{v}\right), v\right)}}\right) \]
            10. associate-*r/N/A

              \[\leadsto \tan^{-1} \left(\frac{v}{\mathsf{fma}\left(H, \mathsf{neg}\left(\color{blue}{\frac{\frac{49}{5} \cdot 1}{v}}\right), v\right)}\right) \]
            11. metadata-evalN/A

              \[\leadsto \tan^{-1} \left(\frac{v}{\mathsf{fma}\left(H, \mathsf{neg}\left(\frac{\color{blue}{\frac{49}{5}}}{v}\right), v\right)}\right) \]
            12. distribute-neg-fracN/A

              \[\leadsto \tan^{-1} \left(\frac{v}{\mathsf{fma}\left(H, \color{blue}{\frac{\mathsf{neg}\left(\frac{49}{5}\right)}{v}}, v\right)}\right) \]
            13. metadata-evalN/A

              \[\leadsto \tan^{-1} \left(\frac{v}{\mathsf{fma}\left(H, \frac{\color{blue}{\frac{-49}{5}}}{v}, v\right)}\right) \]
            14. lower-/.f6491.7

              \[\leadsto \tan^{-1} \left(\frac{v}{\mathsf{fma}\left(H, \color{blue}{\frac{-9.8}{v}}, v\right)}\right) \]
          5. Applied rewrites91.7%

            \[\leadsto \tan^{-1} \left(\frac{v}{\color{blue}{\mathsf{fma}\left(H, \frac{-9.8}{v}, v\right)}}\right) \]
        5. Recombined 3 regimes into one program.
        6. Final simplification88.5%

          \[\leadsto \begin{array}{l} \mathbf{if}\;v \leq -4.1 \cdot 10^{-43}:\\ \;\;\;\;\tan^{-1} -1\\ \mathbf{elif}\;v \leq 9.5 \cdot 10^{-12}:\\ \;\;\;\;\tan^{-1} \left(\frac{v}{\sqrt{H \cdot -19.6}}\right)\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1} \left(\frac{v}{\mathsf{fma}\left(H, \frac{-9.8}{v}, v\right)}\right)\\ \end{array} \]
        7. Add Preprocessing

        Reproduce

        ?
        herbie shell --seed 2024230 
        (FPCore (v H)
          :name "Optimal throwing angle"
          :precision binary64
          (atan (/ v (sqrt (- (* v v) (* (* 2.0 9.8) H))))))