Linear.Quaternion:$clog from linear-1.19.1.3

Percentage Accurate: 68.8% → 99.0%
Time: 3.6s
Alternatives: 4
Speedup: 0.9×

Specification

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

\\
\sqrt{x \cdot x + y}
\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 4 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.8% accurate, 1.0× speedup?

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

\\
\sqrt{x \cdot x + y}
\end{array}

Alternative 1: 99.0% accurate, 0.7× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \cdot x\_m \leq 4 \cdot 10^{+164}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x\_m, x\_m, y\right)}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot x\_m\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m y)
 :precision binary64
 (if (<= (* x_m x_m) 4e+164) (sqrt (fma x_m x_m y)) (* 1.0 x_m)))
x_m = fabs(x);
double code(double x_m, double y) {
	double tmp;
	if ((x_m * x_m) <= 4e+164) {
		tmp = sqrt(fma(x_m, x_m, y));
	} else {
		tmp = 1.0 * x_m;
	}
	return tmp;
}
x_m = abs(x)
function code(x_m, y)
	tmp = 0.0
	if (Float64(x_m * x_m) <= 4e+164)
		tmp = sqrt(fma(x_m, x_m, y));
	else
		tmp = Float64(1.0 * x_m);
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := If[LessEqual[N[(x$95$m * x$95$m), $MachinePrecision], 4e+164], N[Sqrt[N[(x$95$m * x$95$m + y), $MachinePrecision]], $MachinePrecision], N[(1.0 * x$95$m), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \cdot x\_m \leq 4 \cdot 10^{+164}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x\_m, x\_m, y\right)}\\

\mathbf{else}:\\
\;\;\;\;1 \cdot x\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 4e164

    1. Initial program 100.0%

      \[\sqrt{x \cdot x + y} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{x \cdot x + y}} \]
      2. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{x \cdot x} + y} \]
      3. lower-fma.f64100.0

        \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(x, x, y\right)}} \]
    4. Applied rewrites100.0%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(x, x, y\right)}} \]

    if 4e164 < (*.f64 x x)

    1. Initial program 34.0%

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

      \[\leadsto \color{blue}{x \cdot \left(1 + \frac{1}{2} \cdot \frac{y}{{x}^{2}}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

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

        \[\leadsto \color{blue}{\left(1 + \frac{1}{2} \cdot \frac{y}{{x}^{2}}\right) \cdot x} \]
      3. +-commutativeN/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \frac{y}{{x}^{2}} + 1\right)} \cdot x \]
      4. *-commutativeN/A

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\frac{y}{\color{blue}{x \cdot x}}, 0.5, 1\right) \cdot x \]
    5. Applied rewrites46.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y}{x \cdot x}, 0.5, 1\right) \cdot x} \]
    6. Taylor expanded in x around inf

      \[\leadsto 1 \cdot x \]
    7. Step-by-step derivation
      1. Applied rewrites46.3%

        \[\leadsto 1 \cdot x \]
    8. Recombined 2 regimes into one program.
    9. Add Preprocessing

    Alternative 2: 89.0% accurate, 0.9× speedup?

    \[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \cdot x\_m \leq 10^{-63}:\\ \;\;\;\;\sqrt{y}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot x\_m\\ \end{array} \end{array} \]
    x_m = (fabs.f64 x)
    (FPCore (x_m y)
     :precision binary64
     (if (<= (* x_m x_m) 1e-63) (sqrt y) (* 1.0 x_m)))
    x_m = fabs(x);
    double code(double x_m, double y) {
    	double tmp;
    	if ((x_m * x_m) <= 1e-63) {
    		tmp = sqrt(y);
    	} else {
    		tmp = 1.0 * x_m;
    	}
    	return tmp;
    }
    
    x_m = abs(x)
    real(8) function code(x_m, y)
        real(8), intent (in) :: x_m
        real(8), intent (in) :: y
        real(8) :: tmp
        if ((x_m * x_m) <= 1d-63) then
            tmp = sqrt(y)
        else
            tmp = 1.0d0 * x_m
        end if
        code = tmp
    end function
    
    x_m = Math.abs(x);
    public static double code(double x_m, double y) {
    	double tmp;
    	if ((x_m * x_m) <= 1e-63) {
    		tmp = Math.sqrt(y);
    	} else {
    		tmp = 1.0 * x_m;
    	}
    	return tmp;
    }
    
    x_m = math.fabs(x)
    def code(x_m, y):
    	tmp = 0
    	if (x_m * x_m) <= 1e-63:
    		tmp = math.sqrt(y)
    	else:
    		tmp = 1.0 * x_m
    	return tmp
    
    x_m = abs(x)
    function code(x_m, y)
    	tmp = 0.0
    	if (Float64(x_m * x_m) <= 1e-63)
    		tmp = sqrt(y);
    	else
    		tmp = Float64(1.0 * x_m);
    	end
    	return tmp
    end
    
    x_m = abs(x);
    function tmp_2 = code(x_m, y)
    	tmp = 0.0;
    	if ((x_m * x_m) <= 1e-63)
    		tmp = sqrt(y);
    	else
    		tmp = 1.0 * x_m;
    	end
    	tmp_2 = tmp;
    end
    
    x_m = N[Abs[x], $MachinePrecision]
    code[x$95$m_, y_] := If[LessEqual[N[(x$95$m * x$95$m), $MachinePrecision], 1e-63], N[Sqrt[y], $MachinePrecision], N[(1.0 * x$95$m), $MachinePrecision]]
    
    \begin{array}{l}
    x_m = \left|x\right|
    
    \\
    \begin{array}{l}
    \mathbf{if}\;x\_m \cdot x\_m \leq 10^{-63}:\\
    \;\;\;\;\sqrt{y}\\
    
    \mathbf{else}:\\
    \;\;\;\;1 \cdot x\_m\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (*.f64 x x) < 1.00000000000000007e-63

      1. Initial program 100.0%

        \[\sqrt{x \cdot x + y} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\sqrt{y}} \]
      4. Step-by-step derivation
        1. lower-sqrt.f6490.2

          \[\leadsto \color{blue}{\sqrt{y}} \]
      5. Applied rewrites90.2%

        \[\leadsto \color{blue}{\sqrt{y}} \]

      if 1.00000000000000007e-63 < (*.f64 x x)

      1. Initial program 52.5%

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

        \[\leadsto \color{blue}{x \cdot \left(1 + \frac{1}{2} \cdot \frac{y}{{x}^{2}}\right)} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

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

          \[\leadsto \color{blue}{\left(1 + \frac{1}{2} \cdot \frac{y}{{x}^{2}}\right) \cdot x} \]
        3. +-commutativeN/A

          \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \frac{y}{{x}^{2}} + 1\right)} \cdot x \]
        4. *-commutativeN/A

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

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y}{x \cdot x}, 0.5, 1\right) \cdot x} \]
      6. Taylor expanded in x around inf

        \[\leadsto 1 \cdot x \]
      7. Step-by-step derivation
        1. Applied rewrites41.8%

          \[\leadsto 1 \cdot x \]
      8. Recombined 2 regimes into one program.
      9. Add Preprocessing

      Alternative 3: 67.9% accurate, 3.2× speedup?

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

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

        \[\leadsto \color{blue}{x \cdot \left(1 + \frac{1}{2} \cdot \frac{y}{{x}^{2}}\right)} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

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

          \[\leadsto \color{blue}{\left(1 + \frac{1}{2} \cdot \frac{y}{{x}^{2}}\right) \cdot x} \]
        3. +-commutativeN/A

          \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \frac{y}{{x}^{2}} + 1\right)} \cdot x \]
        4. *-commutativeN/A

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

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

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

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

          \[\leadsto \mathsf{fma}\left(\frac{y}{\color{blue}{x \cdot x}}, 0.5, 1\right) \cdot x \]
      5. Applied rewrites30.5%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y}{x \cdot x}, 0.5, 1\right) \cdot x} \]
      6. Taylor expanded in x around inf

        \[\leadsto 1 \cdot x \]
      7. Step-by-step derivation
        1. Applied rewrites30.9%

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

        Alternative 4: 1.2% accurate, 6.3× speedup?

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

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

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

            \[\leadsto \color{blue}{\mathsf{neg}\left(x\right)} \]
          2. lower-neg.f6438.7

            \[\leadsto \color{blue}{-x} \]
        5. Applied rewrites38.7%

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

        Developer Target 1: 98.3% accurate, 0.6× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \frac{y}{x} + x\\ \mathbf{if}\;x < -1.5097698010472593 \cdot 10^{+153}:\\ \;\;\;\;-t\_0\\ \mathbf{elif}\;x < 5.582399551122541 \cdot 10^{+57}:\\ \;\;\;\;\sqrt{x \cdot x + y}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
        (FPCore (x y)
         :precision binary64
         (let* ((t_0 (+ (* 0.5 (/ y x)) x)))
           (if (< x -1.5097698010472593e+153)
             (- t_0)
             (if (< x 5.582399551122541e+57) (sqrt (+ (* x x) y)) t_0))))
        double code(double x, double y) {
        	double t_0 = (0.5 * (y / x)) + x;
        	double tmp;
        	if (x < -1.5097698010472593e+153) {
        		tmp = -t_0;
        	} else if (x < 5.582399551122541e+57) {
        		tmp = sqrt(((x * x) + y));
        	} else {
        		tmp = t_0;
        	}
        	return tmp;
        }
        
        real(8) function code(x, y)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            real(8) :: t_0
            real(8) :: tmp
            t_0 = (0.5d0 * (y / x)) + x
            if (x < (-1.5097698010472593d+153)) then
                tmp = -t_0
            else if (x < 5.582399551122541d+57) then
                tmp = sqrt(((x * x) + y))
            else
                tmp = t_0
            end if
            code = tmp
        end function
        
        public static double code(double x, double y) {
        	double t_0 = (0.5 * (y / x)) + x;
        	double tmp;
        	if (x < -1.5097698010472593e+153) {
        		tmp = -t_0;
        	} else if (x < 5.582399551122541e+57) {
        		tmp = Math.sqrt(((x * x) + y));
        	} else {
        		tmp = t_0;
        	}
        	return tmp;
        }
        
        def code(x, y):
        	t_0 = (0.5 * (y / x)) + x
        	tmp = 0
        	if x < -1.5097698010472593e+153:
        		tmp = -t_0
        	elif x < 5.582399551122541e+57:
        		tmp = math.sqrt(((x * x) + y))
        	else:
        		tmp = t_0
        	return tmp
        
        function code(x, y)
        	t_0 = Float64(Float64(0.5 * Float64(y / x)) + x)
        	tmp = 0.0
        	if (x < -1.5097698010472593e+153)
        		tmp = Float64(-t_0);
        	elseif (x < 5.582399551122541e+57)
        		tmp = sqrt(Float64(Float64(x * x) + y));
        	else
        		tmp = t_0;
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, y)
        	t_0 = (0.5 * (y / x)) + x;
        	tmp = 0.0;
        	if (x < -1.5097698010472593e+153)
        		tmp = -t_0;
        	elseif (x < 5.582399551122541e+57)
        		tmp = sqrt(((x * x) + y));
        	else
        		tmp = t_0;
        	end
        	tmp_2 = tmp;
        end
        
        code[x_, y_] := Block[{t$95$0 = N[(N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision]}, If[Less[x, -1.5097698010472593e+153], (-t$95$0), If[Less[x, 5.582399551122541e+57], N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        t_0 := 0.5 \cdot \frac{y}{x} + x\\
        \mathbf{if}\;x < -1.5097698010472593 \cdot 10^{+153}:\\
        \;\;\;\;-t\_0\\
        
        \mathbf{elif}\;x < 5.582399551122541 \cdot 10^{+57}:\\
        \;\;\;\;\sqrt{x \cdot x + y}\\
        
        \mathbf{else}:\\
        \;\;\;\;t\_0\\
        
        
        \end{array}
        \end{array}
        

        Reproduce

        ?
        herbie shell --seed 2024329 
        (FPCore (x y)
          :name "Linear.Quaternion:$clog from linear-1.19.1.3"
          :precision binary64
        
          :alt
          (! :herbie-platform default (if (< x -1509769801047259300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (- (+ (* 1/2 (/ y x)) x)) (if (< x 5582399551122541000000000000000000000000000000000000000000) (sqrt (+ (* x x) y)) (+ (* 1/2 (/ y x)) x))))
        
          (sqrt (+ (* x x) y)))