Optimal throwing angle

Percentage Accurate: 67.1% → 98.8%
Time: 8.2s
Alternatives: 9
Speedup: 1.3×

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 9 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.1% 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.8% accurate, 0.9× speedup?

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

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

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

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


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

    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. Simplified100.0%

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

      if -9.99999999999999953e157 < v < 4.99999999999999961e80

      1. Initial program 99.8%

        \[\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. lift-*.f64N/A

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

          \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \color{blue}{\left(2 \cdot \frac{49}{5}\right) \cdot H}}}\right) \]
        4. 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) \]
        5. lift-sqrt.f64N/A

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

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

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

          \[\leadsto \tan^{-1} \color{blue}{\left(\frac{1}{\sqrt{v \cdot v - \left(2 \cdot \frac{49}{5}\right) \cdot H}} \cdot v\right)} \]
      4. Applied egg-rr99.8%

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

      if 4.99999999999999961e80 < v

      1. Initial program 30.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. Simplified100.0%

          \[\leadsto \tan^{-1} \color{blue}{1} \]
      5. Recombined 3 regimes into one program.
      6. Final simplification99.9%

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

      Alternative 2: 98.8% accurate, 1.0× speedup?

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

        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. Simplified100.0%

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

          if -9.99999999999999953e157 < v < 4.99999999999999961e80

          1. Initial program 98.9%

            \[\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. lift-*.f64N/A

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

              \[\leadsto \tan^{-1} \left(\frac{v}{\sqrt{v \cdot v - \color{blue}{\left(2 \cdot \frac{49}{5}\right) \cdot H}}}\right) \]
            4. 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) \]
            5. lift-*.f64N/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) \]
            6. lower-fma.f64N/A

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

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

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

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

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

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

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

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

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

          if 4.99999999999999961e80 < v

          1. Initial program 32.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. Simplified97.9%

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

          Reproduce

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