sqrt sqr

Percentage Accurate: 49.8% → 100.0%
Time: 2.6s
Alternatives: 2
Speedup: 46.0×

Specification

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

\\
\frac{x}{x} - \frac{1}{x} \cdot \sqrt{x \cdot x}
\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: 49.8% accurate, 1.0× speedup?

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

\\
\frac{x}{x} - \frac{1}{x} \cdot \sqrt{x \cdot x}
\end{array}

Alternative 1: 100.0% accurate, 6.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\ \;\;\;\;2\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \end{array} \]
(FPCore (x) :precision binary64 (if (<= x -5e-310) 2.0 0.0))
double code(double x) {
	double tmp;
	if (x <= -5e-310) {
		tmp = 2.0;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-5d-310)) then
        tmp = 2.0d0
    else
        tmp = 0.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= -5e-310) {
		tmp = 2.0;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= -5e-310:
		tmp = 2.0
	else:
		tmp = 0.0
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -5e-310)
		tmp = 2.0;
	else
		tmp = 0.0;
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -5e-310)
		tmp = 2.0;
	else
		tmp = 0.0;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, -5e-310], 2.0, 0.0]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;2\\

\mathbf{else}:\\
\;\;\;\;0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -4.999999999999985e-310

    1. Initial program 58.8%

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

      \[\leadsto \color{blue}{2} \]
    4. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto \color{blue}{2} \]

      if -4.999999999999985e-310 < x

      1. Initial program 43.2%

        \[\frac{x}{x} - \frac{1}{x} \cdot \sqrt{x \cdot x} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{0} \]
      4. Step-by-step derivation
        1. Applied rewrites100.0%

          \[\leadsto \color{blue}{0} \]
      5. Recombined 2 regimes into one program.
      6. Add Preprocessing

      Alternative 2: 51.1% accurate, 46.0× speedup?

      \[\begin{array}{l} \\ 0 \end{array} \]
      (FPCore (x) :precision binary64 0.0)
      double code(double x) {
      	return 0.0;
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = 0.0d0
      end function
      
      public static double code(double x) {
      	return 0.0;
      }
      
      def code(x):
      	return 0.0
      
      function code(x)
      	return 0.0
      end
      
      function tmp = code(x)
      	tmp = 0.0;
      end
      
      code[x_] := 0.0
      
      \begin{array}{l}
      
      \\
      0
      \end{array}
      
      Derivation
      1. Initial program 51.1%

        \[\frac{x}{x} - \frac{1}{x} \cdot \sqrt{x \cdot x} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{0} \]
      4. Step-by-step derivation
        1. Applied rewrites51.2%

          \[\leadsto \color{blue}{0} \]
        2. Add Preprocessing

        Developer Target 1: 100.0% accurate, 6.6× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x < 0:\\ \;\;\;\;2\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \end{array} \]
        (FPCore (x) :precision binary64 (if (< x 0.0) 2.0 0.0))
        double code(double x) {
        	double tmp;
        	if (x < 0.0) {
        		tmp = 2.0;
        	} else {
        		tmp = 0.0;
        	}
        	return tmp;
        }
        
        real(8) function code(x)
            real(8), intent (in) :: x
            real(8) :: tmp
            if (x < 0.0d0) then
                tmp = 2.0d0
            else
                tmp = 0.0d0
            end if
            code = tmp
        end function
        
        public static double code(double x) {
        	double tmp;
        	if (x < 0.0) {
        		tmp = 2.0;
        	} else {
        		tmp = 0.0;
        	}
        	return tmp;
        }
        
        def code(x):
        	tmp = 0
        	if x < 0.0:
        		tmp = 2.0
        	else:
        		tmp = 0.0
        	return tmp
        
        function code(x)
        	tmp = 0.0
        	if (x < 0.0)
        		tmp = 2.0;
        	else
        		tmp = 0.0;
        	end
        	return tmp
        end
        
        function tmp_2 = code(x)
        	tmp = 0.0;
        	if (x < 0.0)
        		tmp = 2.0;
        	else
        		tmp = 0.0;
        	end
        	tmp_2 = tmp;
        end
        
        code[x_] := If[Less[x, 0.0], 2.0, 0.0]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;x < 0:\\
        \;\;\;\;2\\
        
        \mathbf{else}:\\
        \;\;\;\;0\\
        
        
        \end{array}
        \end{array}
        

        Reproduce

        ?
        herbie shell --seed 2024327 
        (FPCore (x)
          :name "sqrt sqr"
          :precision binary64
        
          :alt
          (! :herbie-platform default (if (< x 0) 2 0))
        
          (- (/ x x) (* (/ 1.0 x) (sqrt (* x x)))))