ab-angle->ABCF D

Percentage Accurate: 82.4% → 99.6%
Time: 3.7s
Alternatives: 2
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ -\left(\left(a \cdot a\right) \cdot b\right) \cdot b \end{array} \]
(FPCore (a b) :precision binary64 (- (* (* (* a a) b) b)))
double code(double a, double b) {
	return -(((a * a) * b) * b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = -(((a * a) * b) * b)
end function
public static double code(double a, double b) {
	return -(((a * a) * b) * b);
}
def code(a, b):
	return -(((a * a) * b) * b)
function code(a, b)
	return Float64(-Float64(Float64(Float64(a * a) * b) * b))
end
function tmp = code(a, b)
	tmp = -(((a * a) * b) * b);
end
code[a_, b_] := (-N[(N[(N[(a * a), $MachinePrecision] * b), $MachinePrecision] * b), $MachinePrecision])
\begin{array}{l}

\\
-\left(\left(a \cdot a\right) \cdot b\right) \cdot b
\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: 82.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ -\left(\left(a \cdot a\right) \cdot b\right) \cdot b \end{array} \]
(FPCore (a b) :precision binary64 (- (* (* (* a a) b) b)))
double code(double a, double b) {
	return -(((a * a) * b) * b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = -(((a * a) * b) * b)
end function
public static double code(double a, double b) {
	return -(((a * a) * b) * b);
}
def code(a, b):
	return -(((a * a) * b) * b)
function code(a, b)
	return Float64(-Float64(Float64(Float64(a * a) * b) * b))
end
function tmp = code(a, b)
	tmp = -(((a * a) * b) * b);
end
code[a_, b_] := (-N[(N[(N[(a * a), $MachinePrecision] * b), $MachinePrecision] * b), $MachinePrecision])
\begin{array}{l}

\\
-\left(\left(a \cdot a\right) \cdot b\right) \cdot b
\end{array}

Alternative 1: 99.6% accurate, 0.0× speedup?

\[\begin{array}{l} a_m = \left|a\right| \\ b_m = \left|b\right| \\ {\left(a_m \cdot b_m\right)}^{1.5} \cdot \left(\sqrt{b_m} \cdot \left(-\sqrt{a_m}\right)\right) \end{array} \]
a_m = (fabs.f64 a)
b_m = (fabs.f64 b)
(FPCore (a_m b_m)
 :precision binary64
 (* (pow (* a_m b_m) 1.5) (* (sqrt b_m) (- (sqrt a_m)))))
a_m = fabs(a);
b_m = fabs(b);
double code(double a_m, double b_m) {
	return pow((a_m * b_m), 1.5) * (sqrt(b_m) * -sqrt(a_m));
}
a_m = abs(a)
b_m = abs(b)
real(8) function code(a_m, b_m)
    real(8), intent (in) :: a_m
    real(8), intent (in) :: b_m
    code = ((a_m * b_m) ** 1.5d0) * (sqrt(b_m) * -sqrt(a_m))
end function
a_m = Math.abs(a);
b_m = Math.abs(b);
public static double code(double a_m, double b_m) {
	return Math.pow((a_m * b_m), 1.5) * (Math.sqrt(b_m) * -Math.sqrt(a_m));
}
a_m = math.fabs(a)
b_m = math.fabs(b)
def code(a_m, b_m):
	return math.pow((a_m * b_m), 1.5) * (math.sqrt(b_m) * -math.sqrt(a_m))
a_m = abs(a)
b_m = abs(b)
function code(a_m, b_m)
	return Float64((Float64(a_m * b_m) ^ 1.5) * Float64(sqrt(b_m) * Float64(-sqrt(a_m))))
end
a_m = abs(a);
b_m = abs(b);
function tmp = code(a_m, b_m)
	tmp = ((a_m * b_m) ^ 1.5) * (sqrt(b_m) * -sqrt(a_m));
end
a_m = N[Abs[a], $MachinePrecision]
b_m = N[Abs[b], $MachinePrecision]
code[a$95$m_, b$95$m_] := N[(N[Power[N[(a$95$m * b$95$m), $MachinePrecision], 1.5], $MachinePrecision] * N[(N[Sqrt[b$95$m], $MachinePrecision] * (-N[Sqrt[a$95$m], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|

\\
{\left(a_m \cdot b_m\right)}^{1.5} \cdot \left(\sqrt{b_m} \cdot \left(-\sqrt{a_m}\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 99.7% accurate, 1.0× speedup?

\[\begin{array}{l} a_m = \left|a\right| \\ b_m = \left|b\right| \\ \left(a_m \cdot b_m\right) \cdot \left(a_m \cdot \left(-b_m\right)\right) \end{array} \]
a_m = (fabs.f64 a)
b_m = (fabs.f64 b)
(FPCore (a_m b_m) :precision binary64 (* (* a_m b_m) (* a_m (- b_m))))
a_m = fabs(a);
b_m = fabs(b);
double code(double a_m, double b_m) {
	return (a_m * b_m) * (a_m * -b_m);
}
a_m = abs(a)
b_m = abs(b)
real(8) function code(a_m, b_m)
    real(8), intent (in) :: a_m
    real(8), intent (in) :: b_m
    code = (a_m * b_m) * (a_m * -b_m)
end function
a_m = Math.abs(a);
b_m = Math.abs(b);
public static double code(double a_m, double b_m) {
	return (a_m * b_m) * (a_m * -b_m);
}
a_m = math.fabs(a)
b_m = math.fabs(b)
def code(a_m, b_m):
	return (a_m * b_m) * (a_m * -b_m)
a_m = abs(a)
b_m = abs(b)
function code(a_m, b_m)
	return Float64(Float64(a_m * b_m) * Float64(a_m * Float64(-b_m)))
end
a_m = abs(a);
b_m = abs(b);
function tmp = code(a_m, b_m)
	tmp = (a_m * b_m) * (a_m * -b_m);
end
a_m = N[Abs[a], $MachinePrecision]
b_m = N[Abs[b], $MachinePrecision]
code[a$95$m_, b$95$m_] := N[(N[(a$95$m * b$95$m), $MachinePrecision] * N[(a$95$m * (-b$95$m)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|

\\
\left(a_m \cdot b_m\right) \cdot \left(a_m \cdot \left(-b_m\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

?
herbie shell --seed 2023364 
(FPCore (a b)
  :name "ab-angle->ABCF D"
  :precision binary64
  (- (* (* (* a a) b) b)))