Linear.Quaternion:$clog from linear-1.19.1.3

Percentage Accurate: 68.9% → 99.4%
Time: 1.7s
Alternatives: 5
Speedup: 0.6×

Specification

?
\[\sqrt{x \cdot x + y} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (sqrt (+ (* x x) y)))
double code(double x, double y) {
	return sqrt(((x * x) + y));
}
real(8) function code(x, y)
use fmin_fmax_functions
    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]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	sqrt(((x * x) + y))
END code
\sqrt{x \cdot x + 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 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: 68.9% accurate, 1.0× speedup?

\[\sqrt{x \cdot x + y} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (sqrt (+ (* x x) y)))
double code(double x, double y) {
	return sqrt(((x * x) + y));
}
real(8) function code(x, y)
use fmin_fmax_functions
    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]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	sqrt(((x * x) + y))
END code
\sqrt{x \cdot x + y}

Alternative 1: 99.4% accurate, 0.5× speedup?

\[\begin{array}{l} \mathbf{if}\;\left|x\right| \cdot \left|x\right| \leq 10^{+200}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\left|x\right|, \left|x\right|, y\right)}\\ \mathbf{else}:\\ \;\;\;\;\left|x\right| \cdot 1\\ \end{array} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (if (<= (* (fabs x) (fabs x)) 1e+200)
  (sqrt (fma (fabs x) (fabs x) y))
  (* (fabs x) 1.0)))
double code(double x, double y) {
	double tmp;
	if ((fabs(x) * fabs(x)) <= 1e+200) {
		tmp = sqrt(fma(fabs(x), fabs(x), y));
	} else {
		tmp = fabs(x) * 1.0;
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (Float64(abs(x) * abs(x)) <= 1e+200)
		tmp = sqrt(fma(abs(x), abs(x), y));
	else
		tmp = Float64(abs(x) * 1.0);
	end
	return tmp
end
code[x_, y_] := If[LessEqual[N[(N[Abs[x], $MachinePrecision] * N[Abs[x], $MachinePrecision]), $MachinePrecision], 1e+200], N[Sqrt[N[(N[Abs[x], $MachinePrecision] * N[Abs[x], $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * 1.0), $MachinePrecision]]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	LET tmp = IF (((abs(x)) * (abs(x))) <= (99999999999999996973312221251036165947450327545502362648241750950346848435554075534196338404706251868027512415973882408182135734368278484639385041047239877871023591066789981811181813306167128854888448)) THEN (sqrt((((abs(x)) * (abs(x))) + y))) ELSE ((abs(x)) * (1)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\left|x\right| \cdot \left|x\right| \leq 10^{+200}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\left|x\right|, \left|x\right|, y\right)}\\

\mathbf{else}:\\
\;\;\;\;\left|x\right| \cdot 1\\


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

    1. Initial program 68.9%

      \[\sqrt{x \cdot x + y} \]
    2. Step-by-step derivation
      1. Applied rewrites68.9%

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

      if 9.9999999999999997e199 < (*.f64 x x)

      1. Initial program 68.9%

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

        \[\leadsto x \cdot \left(1 + \frac{1}{2} \cdot \frac{y}{{x}^{2}}\right) \]
      3. Step-by-step derivation
        1. Applied rewrites34.4%

          \[\leadsto x \cdot \left(1 + 0.5 \cdot \frac{y}{{x}^{2}}\right) \]
        2. Taylor expanded in x around inf

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

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

        Alternative 2: 89.2% accurate, 0.6× speedup?

        \[\begin{array}{l} \mathbf{if}\;\left|x\right| \cdot \left|x\right| \leq 2 \cdot 10^{-99}:\\ \;\;\;\;\sqrt{y}\\ \mathbf{else}:\\ \;\;\;\;\left|x\right| \cdot 1\\ \end{array} \]
        (FPCore (x y)
          :precision binary64
          :pre TRUE
          (if (<= (* (fabs x) (fabs x)) 2e-99) (sqrt y) (* (fabs x) 1.0)))
        double code(double x, double y) {
        	double tmp;
        	if ((fabs(x) * fabs(x)) <= 2e-99) {
        		tmp = sqrt(y);
        	} else {
        		tmp = fabs(x) * 1.0;
        	}
        	return tmp;
        }
        
        real(8) function code(x, y)
        use fmin_fmax_functions
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            real(8) :: tmp
            if ((abs(x) * abs(x)) <= 2d-99) then
                tmp = sqrt(y)
            else
                tmp = abs(x) * 1.0d0
            end if
            code = tmp
        end function
        
        public static double code(double x, double y) {
        	double tmp;
        	if ((Math.abs(x) * Math.abs(x)) <= 2e-99) {
        		tmp = Math.sqrt(y);
        	} else {
        		tmp = Math.abs(x) * 1.0;
        	}
        	return tmp;
        }
        
        def code(x, y):
        	tmp = 0
        	if (math.fabs(x) * math.fabs(x)) <= 2e-99:
        		tmp = math.sqrt(y)
        	else:
        		tmp = math.fabs(x) * 1.0
        	return tmp
        
        function code(x, y)
        	tmp = 0.0
        	if (Float64(abs(x) * abs(x)) <= 2e-99)
        		tmp = sqrt(y);
        	else
        		tmp = Float64(abs(x) * 1.0);
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, y)
        	tmp = 0.0;
        	if ((abs(x) * abs(x)) <= 2e-99)
        		tmp = sqrt(y);
        	else
        		tmp = abs(x) * 1.0;
        	end
        	tmp_2 = tmp;
        end
        
        code[x_, y_] := If[LessEqual[N[(N[Abs[x], $MachinePrecision] * N[Abs[x], $MachinePrecision]), $MachinePrecision], 2e-99], N[Sqrt[y], $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * 1.0), $MachinePrecision]]
        
        f(x, y):
        	x in [-inf, +inf],
        	y in [-inf, +inf]
        code: THEORY
        BEGIN
        f(x, y: real): real =
        	LET tmp = IF (((abs(x)) * (abs(x))) <= (20000000000000000399837996052057672392955215770683188403652060118731913985110869352353525772265859791654921496218237015970565410794993080445368720839225272167125662825574358854498578849381613317832611860008691572046029005015889997371182867751115974641606953809969127178192138671875e-379)) THEN (sqrt(y)) ELSE ((abs(x)) * (1)) ENDIF IN
        	tmp
        END code
        \begin{array}{l}
        \mathbf{if}\;\left|x\right| \cdot \left|x\right| \leq 2 \cdot 10^{-99}:\\
        \;\;\;\;\sqrt{y}\\
        
        \mathbf{else}:\\
        \;\;\;\;\left|x\right| \cdot 1\\
        
        
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if (*.f64 x x) < 2e-99

          1. Initial program 68.9%

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

            \[\leadsto \sqrt{y} \]
          3. Step-by-step derivation
            1. Applied rewrites34.6%

              \[\leadsto \sqrt{y} \]

            if 2e-99 < (*.f64 x x)

            1. Initial program 68.9%

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

              \[\leadsto x \cdot \left(1 + \frac{1}{2} \cdot \frac{y}{{x}^{2}}\right) \]
            3. Step-by-step derivation
              1. Applied rewrites34.4%

                \[\leadsto x \cdot \left(1 + 0.5 \cdot \frac{y}{{x}^{2}}\right) \]
              2. Taylor expanded in x around inf

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

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

              Alternative 3: 34.7% accurate, 2.9× speedup?

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

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

                \[\leadsto \sqrt{y} \]
              3. Step-by-step derivation
                1. Applied rewrites34.6%

                  \[\leadsto \sqrt{y} \]
                2. Add Preprocessing

                Alternative 4: 34.6% accurate, 4.3× speedup?

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

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

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

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

                      \[\leadsto -x \]
                    2. Taylor expanded in undef-var around zero

                      \[\leadsto -0 \]
                    3. Step-by-step derivation
                      1. Applied rewrites2.6%

                        \[\leadsto -0 \]
                      2. Add Preprocessing

                      Alternative 5: 2.6% accurate, 4.3× speedup?

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

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

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

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

                            \[\leadsto -x \]
                          2. Add Preprocessing

                          Reproduce

                          ?
                          herbie shell --seed 2026092 
                          (FPCore (x y)
                            :name "Linear.Quaternion:$clog from linear-1.19.1.3"
                            :precision binary64
                            (sqrt (+ (* x x) y)))