Linear.Quaternion:$c/ from linear-1.19.1.3, C

Percentage Accurate: 63.5% → 100.0%
Time: 1.2s
Alternatives: 3
Speedup: 3.2×

Specification

?
\[\left(\left(x \cdot y + y \cdot y\right) - y \cdot z\right) - y \cdot y \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (- (- (+ (* x y) (* y y)) (* y z)) (* y y)))
double code(double x, double y, double z) {
	return (((x * y) + (y * y)) - (y * z)) - (y * y);
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (((x * y) + (y * y)) - (y * z)) - (y * y)
end function
public static double code(double x, double y, double z) {
	return (((x * y) + (y * y)) - (y * z)) - (y * y);
}
def code(x, y, z):
	return (((x * y) + (y * y)) - (y * z)) - (y * y)
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * y) + Float64(y * y)) - Float64(y * z)) - Float64(y * y))
end
function tmp = code(x, y, z)
	tmp = (((x * y) + (y * y)) - (y * z)) - (y * y);
end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(((x * y) + (y * y)) - (y * z)) - (y * y)
END code
\left(\left(x \cdot y + y \cdot y\right) - y \cdot z\right) - y \cdot y

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: 63.5% accurate, 1.0× speedup?

\[\left(\left(x \cdot y + y \cdot y\right) - y \cdot z\right) - y \cdot y \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (- (- (+ (* x y) (* y y)) (* y z)) (* y y)))
double code(double x, double y, double z) {
	return (((x * y) + (y * y)) - (y * z)) - (y * y);
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (((x * y) + (y * y)) - (y * z)) - (y * y)
end function
public static double code(double x, double y, double z) {
	return (((x * y) + (y * y)) - (y * z)) - (y * y);
}
def code(x, y, z):
	return (((x * y) + (y * y)) - (y * z)) - (y * y)
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * y) + Float64(y * y)) - Float64(y * z)) - Float64(y * y))
end
function tmp = code(x, y, z)
	tmp = (((x * y) + (y * y)) - (y * z)) - (y * y);
end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(((x * y) + (y * y)) - (y * z)) - (y * y)
END code
\left(\left(x \cdot y + y \cdot y\right) - y \cdot z\right) - y \cdot y

Alternative 1: 100.0% accurate, 3.2× speedup?

\[y \cdot \left(x - z\right) \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (* y (- x z)))
double code(double x, double y, double z) {
	return y * (x - z);
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = y * (x - z)
end function
public static double code(double x, double y, double z) {
	return y * (x - z);
}
def code(x, y, z):
	return y * (x - z)
function code(x, y, z)
	return Float64(y * Float64(x - z))
end
function tmp = code(x, y, z)
	tmp = y * (x - z);
end
code[x_, y_, z_] := N[(y * N[(x - z), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	y * (x - z)
END code
y \cdot \left(x - z\right)
Derivation
  1. Initial program 63.5%

    \[\left(\left(x \cdot y + y \cdot y\right) - y \cdot z\right) - y \cdot y \]
  2. Step-by-step derivation
    1. Applied rewrites100.0%

      \[\leadsto \mathsf{fma}\left(y, x - z, 0\right) \]
    2. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto y \cdot \left(x - z\right) \]
      2. Add Preprocessing

      Alternative 2: 76.7% accurate, 1.7× speedup?

      \[\begin{array}{l} t_0 := -z \cdot y\\ \mathbf{if}\;z \leq -1.5998878487717716 \cdot 10^{-87}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 1.4132947115915006 \cdot 10^{-40}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
      (FPCore (x y z)
        :precision binary64
        :pre TRUE
        (let* ((t_0 (- (* z y))))
        (if (<= z -1.5998878487717716e-87)
          t_0
          (if (<= z 1.4132947115915006e-40) (* x y) t_0))))
      double code(double x, double y, double z) {
      	double t_0 = -(z * y);
      	double tmp;
      	if (z <= -1.5998878487717716e-87) {
      		tmp = t_0;
      	} else if (z <= 1.4132947115915006e-40) {
      		tmp = x * y;
      	} else {
      		tmp = t_0;
      	}
      	return tmp;
      }
      
      real(8) function code(x, y, z)
      use fmin_fmax_functions
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          real(8), intent (in) :: z
          real(8) :: t_0
          real(8) :: tmp
          t_0 = -(z * y)
          if (z <= (-1.5998878487717716d-87)) then
              tmp = t_0
          else if (z <= 1.4132947115915006d-40) then
              tmp = x * y
          else
              tmp = t_0
          end if
          code = tmp
      end function
      
      public static double code(double x, double y, double z) {
      	double t_0 = -(z * y);
      	double tmp;
      	if (z <= -1.5998878487717716e-87) {
      		tmp = t_0;
      	} else if (z <= 1.4132947115915006e-40) {
      		tmp = x * y;
      	} else {
      		tmp = t_0;
      	}
      	return tmp;
      }
      
      def code(x, y, z):
      	t_0 = -(z * y)
      	tmp = 0
      	if z <= -1.5998878487717716e-87:
      		tmp = t_0
      	elif z <= 1.4132947115915006e-40:
      		tmp = x * y
      	else:
      		tmp = t_0
      	return tmp
      
      function code(x, y, z)
      	t_0 = Float64(-Float64(z * y))
      	tmp = 0.0
      	if (z <= -1.5998878487717716e-87)
      		tmp = t_0;
      	elseif (z <= 1.4132947115915006e-40)
      		tmp = Float64(x * y);
      	else
      		tmp = t_0;
      	end
      	return tmp
      end
      
      function tmp_2 = code(x, y, z)
      	t_0 = -(z * y);
      	tmp = 0.0;
      	if (z <= -1.5998878487717716e-87)
      		tmp = t_0;
      	elseif (z <= 1.4132947115915006e-40)
      		tmp = x * y;
      	else
      		tmp = t_0;
      	end
      	tmp_2 = tmp;
      end
      
      code[x_, y_, z_] := Block[{t$95$0 = (-N[(z * y), $MachinePrecision])}, If[LessEqual[z, -1.5998878487717716e-87], t$95$0, If[LessEqual[z, 1.4132947115915006e-40], N[(x * y), $MachinePrecision], t$95$0]]]
      
      f(x, y, z):
      	x in [-inf, +inf],
      	y in [-inf, +inf],
      	z in [-inf, +inf]
      code: THEORY
      BEGIN
      f(x, y, z: real): real =
      	LET t_0 = (- (z * y)) IN
      		LET tmp_1 = IF (z <= (14132947115915006468182910235642061199906833362565967651395812855413318177857125598375794078304482317867511731446228395725484006106853485107421875e-185)) THEN (x * y) ELSE t_0 ENDIF IN
      		LET tmp = IF (z <= (-159988784877177164685523371169830722870522374072384409119026858684832480050318864005140114871679013601761552483193181404403016128515199488354968070059220853336754264519220820635743157089849151568897638446024723319315030689580225953250192105770111083984375e-341)) THEN t_0 ELSE tmp_1 ENDIF IN
      	tmp
      END code
      \begin{array}{l}
      t_0 := -z \cdot y\\
      \mathbf{if}\;z \leq -1.5998878487717716 \cdot 10^{-87}:\\
      \;\;\;\;t\_0\\
      
      \mathbf{elif}\;z \leq 1.4132947115915006 \cdot 10^{-40}:\\
      \;\;\;\;x \cdot y\\
      
      \mathbf{else}:\\
      \;\;\;\;t\_0\\
      
      
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if z < -1.5998878487717716e-87 or 1.4132947115915006e-40 < z

        1. Initial program 63.5%

          \[\left(\left(x \cdot y + y \cdot y\right) - y \cdot z\right) - y \cdot y \]
        2. Taylor expanded in x around 0

          \[\leadsto -1 \cdot \left(y \cdot z\right) \]
        3. Step-by-step derivation
          1. Applied rewrites54.2%

            \[\leadsto -1 \cdot \left(y \cdot z\right) \]
          2. Step-by-step derivation
            1. Applied rewrites54.2%

              \[\leadsto -z \cdot y \]

            if -1.5998878487717716e-87 < z < 1.4132947115915006e-40

            1. Initial program 63.5%

              \[\left(\left(x \cdot y + y \cdot y\right) - y \cdot z\right) - y \cdot y \]
            2. Step-by-step derivation
              1. Applied rewrites100.0%

                \[\leadsto \mathsf{fma}\left(y, x - z, 0\right) \]
              2. Step-by-step derivation
                1. Applied rewrites100.0%

                  \[\leadsto y \cdot \left(x - z\right) \]
                2. Taylor expanded in x around inf

                  \[\leadsto x \cdot y \]
                3. Step-by-step derivation
                  1. Applied rewrites53.8%

                    \[\leadsto x \cdot y \]
                4. Recombined 2 regimes into one program.
                5. Add Preprocessing

                Alternative 3: 53.8% accurate, 5.2× speedup?

                \[x \cdot y \]
                (FPCore (x y z)
                  :precision binary64
                  :pre TRUE
                  (* x y))
                double code(double x, double y, double z) {
                	return x * y;
                }
                
                real(8) function code(x, y, z)
                use fmin_fmax_functions
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    real(8), intent (in) :: z
                    code = x * y
                end function
                
                public static double code(double x, double y, double z) {
                	return x * y;
                }
                
                def code(x, y, z):
                	return x * y
                
                function code(x, y, z)
                	return Float64(x * y)
                end
                
                function tmp = code(x, y, z)
                	tmp = x * y;
                end
                
                code[x_, y_, z_] := N[(x * y), $MachinePrecision]
                
                f(x, y, z):
                	x in [-inf, +inf],
                	y in [-inf, +inf],
                	z in [-inf, +inf]
                code: THEORY
                BEGIN
                f(x, y, z: real): real =
                	x * y
                END code
                x \cdot y
                
                Derivation
                1. Initial program 63.5%

                  \[\left(\left(x \cdot y + y \cdot y\right) - y \cdot z\right) - y \cdot y \]
                2. Step-by-step derivation
                  1. Applied rewrites100.0%

                    \[\leadsto \mathsf{fma}\left(y, x - z, 0\right) \]
                  2. Step-by-step derivation
                    1. Applied rewrites100.0%

                      \[\leadsto y \cdot \left(x - z\right) \]
                    2. Taylor expanded in x around inf

                      \[\leadsto x \cdot y \]
                    3. Step-by-step derivation
                      1. Applied rewrites53.8%

                        \[\leadsto x \cdot y \]
                      2. Add Preprocessing

                      Reproduce

                      ?
                      herbie shell --seed 2026092 
                      (FPCore (x y z)
                        :name "Linear.Quaternion:$c/ from linear-1.19.1.3, C"
                        :precision binary64
                        (- (- (+ (* x y) (* y y)) (* y z)) (* y y)))