ABCF->ab-angle angle

Percentage Accurate: 53.3% → 53.3%
Time: 3.3s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\mathsf{TRUE}\left(\right)\]
\[\begin{array}{l} \\ 180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\right)} \end{array} \]
(FPCore (A B C)
 :precision binary64
 (*
  180.0
  (/
   (atan (* (/ 1.0 B) (- (- C A) (sqrt (+ (pow (- A C) 2.0) (pow B 2.0))))))
   (PI))))
\begin{array}{l}

\\
180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\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 3 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: 53.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\right)} \end{array} \]
(FPCore (A B C)
 :precision binary64
 (*
  180.0
  (/
   (atan (* (/ 1.0 B) (- (- C A) (sqrt (+ (pow (- A C) 2.0) (pow B 2.0))))))
   (PI))))
\begin{array}{l}

\\
180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\right)}
\end{array}

Alternative 1: 53.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\right)} \end{array} \]
(FPCore (A B C)
 :precision binary64
 (*
  180.0
  (/
   (atan (* (/ 1.0 B) (- (- C A) (sqrt (+ (pow (- A C) 2.0) (pow B 2.0))))))
   (PI))))
\begin{array}{l}

\\
180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\right)}
\end{array}
Derivation
  1. Initial program 51.6%

    \[180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\right)} \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 37.8% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\\ t_1 := \left(C - A\right) - t\_0\\ \mathbf{if}\;\tan^{-1} \left(\frac{1}{B} \cdot t\_1\right) \leq 0:\\ \;\;\;\;180 \cdot \frac{\tan^{-1} t\_1}{\mathsf{PI}\left(\right)}\\ \mathbf{else}:\\ \;\;\;\;180 \cdot \frac{\tan^{-1} t\_0}{\mathsf{PI}\left(\right)}\\ \end{array} \end{array} \]
(FPCore (A B C)
 :precision binary64
 (let* ((t_0 (sqrt (+ (pow (- A C) 2.0) (pow B 2.0)))) (t_1 (- (- C A) t_0)))
   (if (<= (atan (* (/ 1.0 B) t_1)) 0.0)
     (* 180.0 (/ (atan t_1) (PI)))
     (* 180.0 (/ (atan t_0) (PI))))))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\\
t_1 := \left(C - A\right) - t\_0\\
\mathbf{if}\;\tan^{-1} \left(\frac{1}{B} \cdot t\_1\right) \leq 0:\\
\;\;\;\;180 \cdot \frac{\tan^{-1} t\_1}{\mathsf{PI}\left(\right)}\\

\mathbf{else}:\\
\;\;\;\;180 \cdot \frac{\tan^{-1} t\_0}{\mathsf{PI}\left(\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (atan.f64 (*.f64 (/.f64 #s(literal 1 binary64) B) (-.f64 (-.f64 C A) (sqrt.f64 (+.f64 (pow.f64 (-.f64 A C) #s(literal 2 binary64)) (pow.f64 B #s(literal 2 binary64))))))) < 0.0

    1. Initial program 45.2%

      \[180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in A around 0

      \[\leadsto 180 \cdot \frac{\tan^{-1} \color{blue}{\left(\left(A \cdot \left(-1 \cdot \frac{1 + -1 \cdot \left(C \cdot \sqrt{\frac{1}{{B}^{2} + {C}^{2}}}\right)}{B} + \frac{-1}{2} \cdot \left(\frac{A \cdot \left(1 - \frac{{C}^{2}}{{B}^{2} + {C}^{2}}\right)}{B} \cdot \sqrt{\frac{1}{{B}^{2} + {C}^{2}}}\right)\right) + \frac{C}{B}\right) - \frac{1}{B} \cdot \sqrt{{B}^{2} + {C}^{2}}\right)}}{\mathsf{PI}\left(\right)} \]
    4. Applied rewrites33.2%

      \[\leadsto 180 \cdot \frac{\tan^{-1} \color{blue}{\left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)}}{\mathsf{PI}\left(\right)} \]

    if 0.0 < (atan.f64 (*.f64 (/.f64 #s(literal 1 binary64) B) (-.f64 (-.f64 C A) (sqrt.f64 (+.f64 (pow.f64 (-.f64 A C) #s(literal 2 binary64)) (pow.f64 B #s(literal 2 binary64)))))))

    1. Initial program 59.7%

      \[180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in A around 0

      \[\leadsto 180 \cdot \frac{\tan^{-1} \color{blue}{\left(\left(-1 \cdot \frac{A \cdot \left(1 + -1 \cdot \left(C \cdot \sqrt{\frac{1}{{B}^{2} + {C}^{2}}}\right)\right)}{B} + \frac{C}{B}\right) - \frac{1}{B} \cdot \sqrt{{B}^{2} + {C}^{2}}\right)}}{\mathsf{PI}\left(\right)} \]
    4. Applied rewrites40.5%

      \[\leadsto 180 \cdot \frac{\tan^{-1} \color{blue}{\left(\sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)}}{\mathsf{PI}\left(\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 18.7% accurate, 1.1× speedup?

\[\begin{array}{l} \\ 180 \cdot \frac{\tan^{-1} \left(\sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)}{\mathsf{PI}\left(\right)} \end{array} \]
(FPCore (A B C)
 :precision binary64
 (* 180.0 (/ (atan (sqrt (+ (pow (- A C) 2.0) (pow B 2.0)))) (PI))))
\begin{array}{l}

\\
180 \cdot \frac{\tan^{-1} \left(\sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)}{\mathsf{PI}\left(\right)}
\end{array}
Derivation
  1. Initial program 51.6%

    \[180 \cdot \frac{\tan^{-1} \left(\frac{1}{B} \cdot \left(\left(C - A\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)\right)}{\mathsf{PI}\left(\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in A around 0

    \[\leadsto 180 \cdot \frac{\tan^{-1} \color{blue}{\left(\left(-1 \cdot \frac{A \cdot \left(1 + -1 \cdot \left(C \cdot \sqrt{\frac{1}{{B}^{2} + {C}^{2}}}\right)\right)}{B} + \frac{C}{B}\right) - \frac{1}{B} \cdot \sqrt{{B}^{2} + {C}^{2}}\right)}}{\mathsf{PI}\left(\right)} \]
  4. Applied rewrites19.3%

    \[\leadsto 180 \cdot \frac{\tan^{-1} \color{blue}{\left(\sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)}}{\mathsf{PI}\left(\right)} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024321 
(FPCore (A B C)
  :name "ABCF->ab-angle angle"
  :precision binary64
  :pre (TRUE)
  (* 180.0 (/ (atan (* (/ 1.0 B) (- (- C A) (sqrt (+ (pow (- A C) 2.0) (pow B 2.0)))))) (PI))))