Diagrams.TwoD.Apollonian:initialConfig from diagrams-contrib-1.3.0.5, B

Percentage Accurate: 68.4% → 99.6%
Time: 5.7s
Alternatives: 2
Speedup: 4.8×

Specification

?
\[\begin{array}{l} \\ x \cdot \sqrt{y \cdot y - z \cdot z} \end{array} \]
(FPCore (x y z) :precision binary64 (* x (sqrt (- (* y y) (* z z)))))
double code(double x, double y, double z) {
	return x * sqrt(((y * y) - (z * z)));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * sqrt(((y * y) - (z * z)))
end function
public static double code(double x, double y, double z) {
	return x * Math.sqrt(((y * y) - (z * z)));
}
def code(x, y, z):
	return x * math.sqrt(((y * y) - (z * z)))
function code(x, y, z)
	return Float64(x * sqrt(Float64(Float64(y * y) - Float64(z * z))))
end
function tmp = code(x, y, z)
	tmp = x * sqrt(((y * y) - (z * z)));
end
code[x_, y_, z_] := N[(x * N[Sqrt[N[(N[(y * y), $MachinePrecision] - N[(z * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \sqrt{y \cdot y - z \cdot z}
\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: 68.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot \sqrt{y \cdot y - z \cdot z} \end{array} \]
(FPCore (x y z) :precision binary64 (* x (sqrt (- (* y y) (* z z)))))
double code(double x, double y, double z) {
	return x * sqrt(((y * y) - (z * z)));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * sqrt(((y * y) - (z * z)))
end function
public static double code(double x, double y, double z) {
	return x * Math.sqrt(((y * y) - (z * z)));
}
def code(x, y, z):
	return x * math.sqrt(((y * y) - (z * z)))
function code(x, y, z)
	return Float64(x * sqrt(Float64(Float64(y * y) - Float64(z * z))))
end
function tmp = code(x, y, z)
	tmp = x * sqrt(((y * y) - (z * z)));
end
code[x_, y_, z_] := N[(x * N[Sqrt[N[(N[(y * y), $MachinePrecision] - N[(z * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \sqrt{y \cdot y - z \cdot z}
\end{array}

Alternative 1: 99.6% accurate, 1.0× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ x \cdot \mathsf{fma}\left(-0.5 \cdot z, \frac{z}{y\_m}, y\_m\right) \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m z) :precision binary64 (* x (fma (* -0.5 z) (/ z y_m) y_m)))
y_m = fabs(y);
double code(double x, double y_m, double z) {
	return x * fma((-0.5 * z), (z / y_m), y_m);
}
y_m = abs(y)
function code(x, y_m, z)
	return Float64(x * fma(Float64(-0.5 * z), Float64(z / y_m), y_m))
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_, z_] := N[(x * N[(N[(-0.5 * z), $MachinePrecision] * N[(z / y$95$m), $MachinePrecision] + y$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y_m = \left|y\right|

\\
x \cdot \mathsf{fma}\left(-0.5 \cdot z, \frac{z}{y\_m}, y\_m\right)
\end{array}
Derivation
  1. Initial program 72.2%

    \[x \cdot \sqrt{y \cdot y - z \cdot z} \]
  2. Add Preprocessing
  3. Taylor expanded in y around inf

    \[\leadsto \color{blue}{x \cdot y} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{y \cdot x} \]
    2. lower-*.f6458.3

      \[\leadsto \color{blue}{y \cdot x} \]
  5. Applied rewrites58.3%

    \[\leadsto \color{blue}{y \cdot x} \]
  6. Taylor expanded in z around 0

    \[\leadsto \color{blue}{\frac{-1}{2} \cdot \frac{x \cdot {z}^{2}}{y} + x \cdot y} \]
  7. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{\frac{x \cdot {z}^{2}}{y} \cdot \frac{-1}{2}} + x \cdot y \]
    2. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{x \cdot {z}^{2}}{y}, \frac{-1}{2}, x \cdot y\right)} \]
    3. associate-/l*N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot \frac{{z}^{2}}{y}}, \frac{-1}{2}, x \cdot y\right) \]
    4. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot \frac{{z}^{2}}{y}}, \frac{-1}{2}, x \cdot y\right) \]
    5. lower-/.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \color{blue}{\frac{{z}^{2}}{y}}, \frac{-1}{2}, x \cdot y\right) \]
    6. unpow2N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \frac{\color{blue}{z \cdot z}}{y}, \frac{-1}{2}, x \cdot y\right) \]
    7. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \frac{\color{blue}{z \cdot z}}{y}, \frac{-1}{2}, x \cdot y\right) \]
    8. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(x \cdot \frac{z \cdot z}{y}, \frac{-1}{2}, \color{blue}{y \cdot x}\right) \]
    9. lower-*.f6455.8

      \[\leadsto \mathsf{fma}\left(x \cdot \frac{z \cdot z}{y}, -0.5, \color{blue}{y \cdot x}\right) \]
  8. Applied rewrites55.8%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot \frac{z \cdot z}{y}, -0.5, y \cdot x\right)} \]
  9. Step-by-step derivation
    1. Applied rewrites56.5%

      \[\leadsto \mathsf{fma}\left(\left(x \cdot z\right) \cdot \frac{z}{y}, -0.5, y \cdot x\right) \]
    2. Step-by-step derivation
      1. Applied rewrites58.5%

        \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left(-0.5 \cdot z, \frac{z}{y}, y\right)} \]
      2. Add Preprocessing

      Alternative 2: 99.2% accurate, 4.8× speedup?

      \[\begin{array}{l} y_m = \left|y\right| \\ y\_m \cdot x \end{array} \]
      y_m = (fabs.f64 y)
      (FPCore (x y_m z) :precision binary64 (* y_m x))
      y_m = fabs(y);
      double code(double x, double y_m, double z) {
      	return y_m * x;
      }
      
      y_m = abs(y)
      real(8) function code(x, y_m, z)
          real(8), intent (in) :: x
          real(8), intent (in) :: y_m
          real(8), intent (in) :: z
          code = y_m * x
      end function
      
      y_m = Math.abs(y);
      public static double code(double x, double y_m, double z) {
      	return y_m * x;
      }
      
      y_m = math.fabs(y)
      def code(x, y_m, z):
      	return y_m * x
      
      y_m = abs(y)
      function code(x, y_m, z)
      	return Float64(y_m * x)
      end
      
      y_m = abs(y);
      function tmp = code(x, y_m, z)
      	tmp = y_m * x;
      end
      
      y_m = N[Abs[y], $MachinePrecision]
      code[x_, y$95$m_, z_] := N[(y$95$m * x), $MachinePrecision]
      
      \begin{array}{l}
      y_m = \left|y\right|
      
      \\
      y\_m \cdot x
      \end{array}
      
      Derivation
      1. Initial program 72.2%

        \[x \cdot \sqrt{y \cdot y - z \cdot z} \]
      2. Add Preprocessing
      3. Taylor expanded in y around inf

        \[\leadsto \color{blue}{x \cdot y} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{y \cdot x} \]
        2. lower-*.f6458.3

          \[\leadsto \color{blue}{y \cdot x} \]
      5. Applied rewrites58.3%

        \[\leadsto \color{blue}{y \cdot x} \]
      6. Add Preprocessing

      Developer Target 1: 99.3% accurate, 0.7× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y < 2.5816096488251695 \cdot 10^{-278}:\\ \;\;\;\;-x \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(\sqrt{y + z} \cdot \sqrt{y - z}\right)\\ \end{array} \end{array} \]
      (FPCore (x y z)
       :precision binary64
       (if (< y 2.5816096488251695e-278)
         (- (* x y))
         (* x (* (sqrt (+ y z)) (sqrt (- y z))))))
      double code(double x, double y, double z) {
      	double tmp;
      	if (y < 2.5816096488251695e-278) {
      		tmp = -(x * y);
      	} else {
      		tmp = x * (sqrt((y + z)) * sqrt((y - z)));
      	}
      	return tmp;
      }
      
      real(8) function code(x, y, z)
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          real(8), intent (in) :: z
          real(8) :: tmp
          if (y < 2.5816096488251695d-278) then
              tmp = -(x * y)
          else
              tmp = x * (sqrt((y + z)) * sqrt((y - z)))
          end if
          code = tmp
      end function
      
      public static double code(double x, double y, double z) {
      	double tmp;
      	if (y < 2.5816096488251695e-278) {
      		tmp = -(x * y);
      	} else {
      		tmp = x * (Math.sqrt((y + z)) * Math.sqrt((y - z)));
      	}
      	return tmp;
      }
      
      def code(x, y, z):
      	tmp = 0
      	if y < 2.5816096488251695e-278:
      		tmp = -(x * y)
      	else:
      		tmp = x * (math.sqrt((y + z)) * math.sqrt((y - z)))
      	return tmp
      
      function code(x, y, z)
      	tmp = 0.0
      	if (y < 2.5816096488251695e-278)
      		tmp = Float64(-Float64(x * y));
      	else
      		tmp = Float64(x * Float64(sqrt(Float64(y + z)) * sqrt(Float64(y - z))));
      	end
      	return tmp
      end
      
      function tmp_2 = code(x, y, z)
      	tmp = 0.0;
      	if (y < 2.5816096488251695e-278)
      		tmp = -(x * y);
      	else
      		tmp = x * (sqrt((y + z)) * sqrt((y - z)));
      	end
      	tmp_2 = tmp;
      end
      
      code[x_, y_, z_] := If[Less[y, 2.5816096488251695e-278], (-N[(x * y), $MachinePrecision]), N[(x * N[(N[Sqrt[N[(y + z), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(y - z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;y < 2.5816096488251695 \cdot 10^{-278}:\\
      \;\;\;\;-x \cdot y\\
      
      \mathbf{else}:\\
      \;\;\;\;x \cdot \left(\sqrt{y + z} \cdot \sqrt{y - z}\right)\\
      
      
      \end{array}
      \end{array}
      

      Reproduce

      ?
      herbie shell --seed 2024326 
      (FPCore (x y z)
        :name "Diagrams.TwoD.Apollonian:initialConfig from diagrams-contrib-1.3.0.5, B"
        :precision binary64
      
        :alt
        (! :herbie-platform default (if (< y 5163219297650339/200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (- (* x y)) (* x (* (sqrt (+ y z)) (sqrt (- y z))))))
      
        (* x (sqrt (- (* y y) (* z z)))))