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

Percentage Accurate: 98.1% → 99.3%
Time: 1.4s
Alternatives: 5
Speedup: 1.7×

Specification

?
\[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
	return (((x * y) + (z * z)) + (z * z)) + (z * 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 = (((x * y) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
	return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z):
	return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z))
end
function tmp = code(x, y, z)
	tmp = (((x * y) + (z * z)) + (z * z)) + (z * z);
end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * 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 =
	(((x * y) + (z * z)) + (z * z)) + (z * z)
END code
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z

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

\[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
	return (((x * y) + (z * z)) + (z * z)) + (z * 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 = (((x * y) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
	return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z):
	return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z))
end
function tmp = code(x, y, z)
	tmp = (((x * y) + (z * z)) + (z * z)) + (z * z);
end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * 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 =
	(((x * y) + (z * z)) + (z * z)) + (z * z)
END code
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z

Alternative 1: 99.3% accurate, 1.7× speedup?

\[\mathsf{fma}\left(x, y, 3 \cdot \left(z \cdot z\right)\right) \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (fma x y (* 3.0 (* z z))))
double code(double x, double y, double z) {
	return fma(x, y, (3.0 * (z * z)));
}
function code(x, y, z)
	return fma(x, y, Float64(3.0 * Float64(z * z)))
end
code[x_, y_, z_] := N[(x * y + N[(3.0 * N[(z * z), $MachinePrecision]), $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) + ((3) * (z * z))
END code
\mathsf{fma}\left(x, y, 3 \cdot \left(z \cdot z\right)\right)
Derivation
  1. Initial program 98.1%

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

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

        \[\leadsto \mathsf{fma}\left(x, y, 3 \cdot \left(z \cdot z\right)\right) \]
      2. Add Preprocessing

      Alternative 2: 98.2% accurate, 1.7× speedup?

      \[\mathsf{fma}\left(3, z \cdot z, y \cdot x\right) \]
      (FPCore (x y z)
        :precision binary64
        :pre TRUE
        (fma 3.0 (* z z) (* y x)))
      double code(double x, double y, double z) {
      	return fma(3.0, (z * z), (y * x));
      }
      
      function code(x, y, z)
      	return fma(3.0, Float64(z * z), Float64(y * x))
      end
      
      code[x_, y_, z_] := N[(3.0 * N[(z * z), $MachinePrecision] + N[(y * x), $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 =
      	((3) * (z * z)) + (y * x)
      END code
      \mathsf{fma}\left(3, z \cdot z, y \cdot x\right)
      
      Derivation
      1. Initial program 98.1%

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

          \[\leadsto \mathsf{fma}\left(3, z \cdot z, y \cdot x\right) \]
        2. Add Preprocessing

        Alternative 3: 83.5% accurate, 1.5× speedup?

        \[\begin{array}{l} \mathbf{if}\;z \cdot z \leq 5 \cdot 10^{+109}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(z \cdot z\right) \cdot 3\\ \end{array} \]
        (FPCore (x y z)
          :precision binary64
          :pre TRUE
          (if (<= (* z z) 5e+109) (* x y) (* (* z z) 3.0)))
        double code(double x, double y, double z) {
        	double tmp;
        	if ((z * z) <= 5e+109) {
        		tmp = x * y;
        	} else {
        		tmp = (z * z) * 3.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) :: tmp
            if ((z * z) <= 5d+109) then
                tmp = x * y
            else
                tmp = (z * z) * 3.0d0
            end if
            code = tmp
        end function
        
        public static double code(double x, double y, double z) {
        	double tmp;
        	if ((z * z) <= 5e+109) {
        		tmp = x * y;
        	} else {
        		tmp = (z * z) * 3.0;
        	}
        	return tmp;
        }
        
        def code(x, y, z):
        	tmp = 0
        	if (z * z) <= 5e+109:
        		tmp = x * y
        	else:
        		tmp = (z * z) * 3.0
        	return tmp
        
        function code(x, y, z)
        	tmp = 0.0
        	if (Float64(z * z) <= 5e+109)
        		tmp = Float64(x * y);
        	else
        		tmp = Float64(Float64(z * z) * 3.0);
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, y, z)
        	tmp = 0.0;
        	if ((z * z) <= 5e+109)
        		tmp = x * y;
        	else
        		tmp = (z * z) * 3.0;
        	end
        	tmp_2 = tmp;
        end
        
        code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 5e+109], N[(x * y), $MachinePrecision], N[(N[(z * z), $MachinePrecision] * 3.0), $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 =
        	LET tmp = IF ((z * z) <= (50000000000000001178468375708512791662476639752844093156495626963414083423308086629915468079622475513115705344)) THEN (x * y) ELSE ((z * z) * (3)) ENDIF IN
        	tmp
        END code
        \begin{array}{l}
        \mathbf{if}\;z \cdot z \leq 5 \cdot 10^{+109}:\\
        \;\;\;\;x \cdot y\\
        
        \mathbf{else}:\\
        \;\;\;\;\left(z \cdot z\right) \cdot 3\\
        
        
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if (*.f64 z z) < 5.0000000000000001e109

          1. Initial program 98.1%

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

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

                \[\leadsto \mathsf{fma}\left(3, {\left(\sqrt{\left|z\right|}\right)}^{4}, y \cdot x\right) \]
              2. Taylor expanded in x around 0

                \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
              3. Step-by-step derivation
                1. Applied rewrites53.7%

                  \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
                2. Taylor expanded in x around inf

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

                    \[\leadsto x \cdot y \]

                  if 5.0000000000000001e109 < (*.f64 z z)

                  1. Initial program 98.1%

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

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

                        \[\leadsto \mathsf{fma}\left(3, {\left(\sqrt{\left|z\right|}\right)}^{4}, y \cdot x\right) \]
                      2. Taylor expanded in x around 0

                        \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
                      3. Step-by-step derivation
                        1. Applied rewrites53.7%

                          \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
                        2. Step-by-step derivation
                          1. Applied rewrites54.0%

                            \[\leadsto \left(z \cdot z\right) \cdot 3 \]
                        3. Recombined 2 regimes into one program.
                        4. Add Preprocessing

                        Alternative 4: 83.5% accurate, 1.5× speedup?

                        \[\begin{array}{l} \mathbf{if}\;z \cdot z \leq 5 \cdot 10^{+109}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(3 \cdot z\right)\\ \end{array} \]
                        (FPCore (x y z)
                          :precision binary64
                          :pre TRUE
                          (if (<= (* z z) 5e+109) (* x y) (* z (* 3.0 z))))
                        double code(double x, double y, double z) {
                        	double tmp;
                        	if ((z * z) <= 5e+109) {
                        		tmp = x * y;
                        	} else {
                        		tmp = z * (3.0 * z);
                        	}
                        	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) :: tmp
                            if ((z * z) <= 5d+109) then
                                tmp = x * y
                            else
                                tmp = z * (3.0d0 * z)
                            end if
                            code = tmp
                        end function
                        
                        public static double code(double x, double y, double z) {
                        	double tmp;
                        	if ((z * z) <= 5e+109) {
                        		tmp = x * y;
                        	} else {
                        		tmp = z * (3.0 * z);
                        	}
                        	return tmp;
                        }
                        
                        def code(x, y, z):
                        	tmp = 0
                        	if (z * z) <= 5e+109:
                        		tmp = x * y
                        	else:
                        		tmp = z * (3.0 * z)
                        	return tmp
                        
                        function code(x, y, z)
                        	tmp = 0.0
                        	if (Float64(z * z) <= 5e+109)
                        		tmp = Float64(x * y);
                        	else
                        		tmp = Float64(z * Float64(3.0 * z));
                        	end
                        	return tmp
                        end
                        
                        function tmp_2 = code(x, y, z)
                        	tmp = 0.0;
                        	if ((z * z) <= 5e+109)
                        		tmp = x * y;
                        	else
                        		tmp = z * (3.0 * z);
                        	end
                        	tmp_2 = tmp;
                        end
                        
                        code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 5e+109], N[(x * y), $MachinePrecision], N[(z * N[(3.0 * 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 =
                        	LET tmp = IF ((z * z) <= (50000000000000001178468375708512791662476639752844093156495626963414083423308086629915468079622475513115705344)) THEN (x * y) ELSE (z * ((3) * z)) ENDIF IN
                        	tmp
                        END code
                        \begin{array}{l}
                        \mathbf{if}\;z \cdot z \leq 5 \cdot 10^{+109}:\\
                        \;\;\;\;x \cdot y\\
                        
                        \mathbf{else}:\\
                        \;\;\;\;z \cdot \left(3 \cdot z\right)\\
                        
                        
                        \end{array}
                        
                        Derivation
                        1. Split input into 2 regimes
                        2. if (*.f64 z z) < 5.0000000000000001e109

                          1. Initial program 98.1%

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

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

                                \[\leadsto \mathsf{fma}\left(3, {\left(\sqrt{\left|z\right|}\right)}^{4}, y \cdot x\right) \]
                              2. Taylor expanded in x around 0

                                \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
                              3. Step-by-step derivation
                                1. Applied rewrites53.7%

                                  \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
                                2. Taylor expanded in x around inf

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

                                    \[\leadsto x \cdot y \]

                                  if 5.0000000000000001e109 < (*.f64 z z)

                                  1. Initial program 98.1%

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

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

                                        \[\leadsto \mathsf{fma}\left(3, {\left(\sqrt{\left|z\right|}\right)}^{4}, y \cdot x\right) \]
                                      2. Taylor expanded in x around 0

                                        \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
                                      3. Step-by-step derivation
                                        1. Applied rewrites53.7%

                                          \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
                                        2. Step-by-step derivation
                                          1. Applied rewrites54.0%

                                            \[\leadsto z \cdot \left(3 \cdot z\right) \]
                                        3. Recombined 2 regimes into one program.
                                        4. Add Preprocessing

                                        Alternative 5: 52.5% 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 98.1%

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

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

                                              \[\leadsto \mathsf{fma}\left(3, {\left(\sqrt{\left|z\right|}\right)}^{4}, y \cdot x\right) \]
                                            2. Taylor expanded in x around 0

                                              \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
                                            3. Step-by-step derivation
                                              1. Applied rewrites53.7%

                                                \[\leadsto 3 \cdot {\left(\sqrt{\left|z\right|}\right)}^{4} \]
                                              2. Taylor expanded in x around inf

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

                                                  \[\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, A"
                                                  :precision binary64
                                                  (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))