Main:bigenough3 from C

Percentage Accurate: 52.8% → 99.7%
Time: 2.7s
Alternatives: 6
Speedup: 1.0×

Specification

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

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

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

Alternative 1: 99.7% accurate, 0.7× speedup?

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

    \[\sqrt{x + 1} - \sqrt{x} \]
  2. Step-by-step derivation
    1. Applied rewrites99.7%

      \[\leadsto \frac{1}{\sqrt{x} + \sqrt{1 + x}} \]
    2. Add Preprocessing

    Alternative 2: 99.4% accurate, 0.7× speedup?

    \[\begin{array}{l} \mathbf{if}\;x \leq 1907310.3737776245:\\ \;\;\;\;\sqrt{x + 1} - \sqrt{x}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{1}{x}}\\ \end{array} \]
    (FPCore (x)
      :precision binary64
      :pre TRUE
      (if (<= x 1907310.3737776245)
      (- (sqrt (+ x 1.0)) (sqrt x))
      (* 0.5 (sqrt (/ 1.0 x)))))
    double code(double x) {
    	double tmp;
    	if (x <= 1907310.3737776245) {
    		tmp = sqrt((x + 1.0)) - sqrt(x);
    	} else {
    		tmp = 0.5 * sqrt((1.0 / x));
    	}
    	return tmp;
    }
    
    real(8) function code(x)
    use fmin_fmax_functions
        real(8), intent (in) :: x
        real(8) :: tmp
        if (x <= 1907310.3737776245d0) then
            tmp = sqrt((x + 1.0d0)) - sqrt(x)
        else
            tmp = 0.5d0 * sqrt((1.0d0 / x))
        end if
        code = tmp
    end function
    
    public static double code(double x) {
    	double tmp;
    	if (x <= 1907310.3737776245) {
    		tmp = Math.sqrt((x + 1.0)) - Math.sqrt(x);
    	} else {
    		tmp = 0.5 * Math.sqrt((1.0 / x));
    	}
    	return tmp;
    }
    
    def code(x):
    	tmp = 0
    	if x <= 1907310.3737776245:
    		tmp = math.sqrt((x + 1.0)) - math.sqrt(x)
    	else:
    		tmp = 0.5 * math.sqrt((1.0 / x))
    	return tmp
    
    function code(x)
    	tmp = 0.0
    	if (x <= 1907310.3737776245)
    		tmp = Float64(sqrt(Float64(x + 1.0)) - sqrt(x));
    	else
    		tmp = Float64(0.5 * sqrt(Float64(1.0 / x)));
    	end
    	return tmp
    end
    
    function tmp_2 = code(x)
    	tmp = 0.0;
    	if (x <= 1907310.3737776245)
    		tmp = sqrt((x + 1.0)) - sqrt(x);
    	else
    		tmp = 0.5 * sqrt((1.0 / x));
    	end
    	tmp_2 = tmp;
    end
    
    code[x_] := If[LessEqual[x, 1907310.3737776245], N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
    
    f(x):
    	x in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x: real): real =
    	LET tmp = IF (x <= (190731037377762445248663425445556640625e-32)) THEN ((sqrt((x + (1)))) - (sqrt(x))) ELSE ((5e-1) * (sqrt(((1) / x)))) ENDIF IN
    	tmp
    END code
    \begin{array}{l}
    \mathbf{if}\;x \leq 1907310.3737776245:\\
    \;\;\;\;\sqrt{x + 1} - \sqrt{x}\\
    
    \mathbf{else}:\\
    \;\;\;\;0.5 \cdot \sqrt{\frac{1}{x}}\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if x < 1907310.3737776245

      1. Initial program 52.8%

        \[\sqrt{x + 1} - \sqrt{x} \]

      if 1907310.3737776245 < x

      1. Initial program 52.8%

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

        \[\leadsto \frac{\frac{1}{2}}{x \cdot \sqrt{\frac{1}{x}}} \]
      3. Step-by-step derivation
        1. Applied rewrites52.8%

          \[\leadsto \frac{0.5}{x \cdot \sqrt{\frac{1}{x}}} \]
        2. Step-by-step derivation
          1. Applied rewrites52.9%

            \[\leadsto 0.5 \cdot \frac{\sqrt{x}}{x} \]
          2. Taylor expanded in x around inf

            \[\leadsto 0.5 \cdot \sqrt{\frac{1}{x}} \]
          3. Step-by-step derivation
            1. Applied rewrites53.0%

              \[\leadsto 0.5 \cdot \sqrt{\frac{1}{x}} \]
          4. Recombined 2 regimes into one program.
          5. Add Preprocessing

          Alternative 3: 97.7% accurate, 0.7× speedup?

          \[\begin{array}{l} \mathbf{if}\;x \leq 0.19634788085319213:\\ \;\;\;\;1 - \sqrt{x}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{1}{x}}\\ \end{array} \]
          (FPCore (x)
            :precision binary64
            :pre TRUE
            (if (<= x 0.19634788085319213)
            (- 1.0 (sqrt x))
            (* 0.5 (sqrt (/ 1.0 x)))))
          double code(double x) {
          	double tmp;
          	if (x <= 0.19634788085319213) {
          		tmp = 1.0 - sqrt(x);
          	} else {
          		tmp = 0.5 * sqrt((1.0 / x));
          	}
          	return tmp;
          }
          
          real(8) function code(x)
          use fmin_fmax_functions
              real(8), intent (in) :: x
              real(8) :: tmp
              if (x <= 0.19634788085319213d0) then
                  tmp = 1.0d0 - sqrt(x)
              else
                  tmp = 0.5d0 * sqrt((1.0d0 / x))
              end if
              code = tmp
          end function
          
          public static double code(double x) {
          	double tmp;
          	if (x <= 0.19634788085319213) {
          		tmp = 1.0 - Math.sqrt(x);
          	} else {
          		tmp = 0.5 * Math.sqrt((1.0 / x));
          	}
          	return tmp;
          }
          
          def code(x):
          	tmp = 0
          	if x <= 0.19634788085319213:
          		tmp = 1.0 - math.sqrt(x)
          	else:
          		tmp = 0.5 * math.sqrt((1.0 / x))
          	return tmp
          
          function code(x)
          	tmp = 0.0
          	if (x <= 0.19634788085319213)
          		tmp = Float64(1.0 - sqrt(x));
          	else
          		tmp = Float64(0.5 * sqrt(Float64(1.0 / x)));
          	end
          	return tmp
          end
          
          function tmp_2 = code(x)
          	tmp = 0.0;
          	if (x <= 0.19634788085319213)
          		tmp = 1.0 - sqrt(x);
          	else
          		tmp = 0.5 * sqrt((1.0 / x));
          	end
          	tmp_2 = tmp;
          end
          
          code[x_] := If[LessEqual[x, 0.19634788085319213], N[(1.0 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
          
          f(x):
          	x in [-inf, +inf]
          code: THEORY
          BEGIN
          f(x: real): real =
          	LET tmp = IF (x <= (1963478808531921304680878392900922335684299468994140625e-55)) THEN ((1) - (sqrt(x))) ELSE ((5e-1) * (sqrt(((1) / x)))) ENDIF IN
          	tmp
          END code
          \begin{array}{l}
          \mathbf{if}\;x \leq 0.19634788085319213:\\
          \;\;\;\;1 - \sqrt{x}\\
          
          \mathbf{else}:\\
          \;\;\;\;0.5 \cdot \sqrt{\frac{1}{x}}\\
          
          
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if x < 0.19634788085319213

            1. Initial program 52.8%

              \[\sqrt{x + 1} - \sqrt{x} \]
            2. Taylor expanded in x around 0

              \[\leadsto 1 - \sqrt{x} \]
            3. Step-by-step derivation
              1. Applied rewrites49.0%

                \[\leadsto 1 - \sqrt{x} \]

              if 0.19634788085319213 < x

              1. Initial program 52.8%

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

                \[\leadsto \frac{\frac{1}{2}}{x \cdot \sqrt{\frac{1}{x}}} \]
              3. Step-by-step derivation
                1. Applied rewrites52.8%

                  \[\leadsto \frac{0.5}{x \cdot \sqrt{\frac{1}{x}}} \]
                2. Step-by-step derivation
                  1. Applied rewrites52.9%

                    \[\leadsto 0.5 \cdot \frac{\sqrt{x}}{x} \]
                  2. Taylor expanded in x around inf

                    \[\leadsto 0.5 \cdot \sqrt{\frac{1}{x}} \]
                  3. Step-by-step derivation
                    1. Applied rewrites53.0%

                      \[\leadsto 0.5 \cdot \sqrt{\frac{1}{x}} \]
                  4. Recombined 2 regimes into one program.
                  5. Add Preprocessing

                  Alternative 4: 97.6% accurate, 1.0× speedup?

                  \[\begin{array}{l} \mathbf{if}\;x \leq 0.19634788085319213:\\ \;\;\;\;1 - \sqrt{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{\sqrt{x}}\\ \end{array} \]
                  (FPCore (x)
                    :precision binary64
                    :pre TRUE
                    (if (<= x 0.19634788085319213) (- 1.0 (sqrt x)) (/ 0.5 (sqrt x))))
                  double code(double x) {
                  	double tmp;
                  	if (x <= 0.19634788085319213) {
                  		tmp = 1.0 - sqrt(x);
                  	} else {
                  		tmp = 0.5 / sqrt(x);
                  	}
                  	return tmp;
                  }
                  
                  real(8) function code(x)
                  use fmin_fmax_functions
                      real(8), intent (in) :: x
                      real(8) :: tmp
                      if (x <= 0.19634788085319213d0) then
                          tmp = 1.0d0 - sqrt(x)
                      else
                          tmp = 0.5d0 / sqrt(x)
                      end if
                      code = tmp
                  end function
                  
                  public static double code(double x) {
                  	double tmp;
                  	if (x <= 0.19634788085319213) {
                  		tmp = 1.0 - Math.sqrt(x);
                  	} else {
                  		tmp = 0.5 / Math.sqrt(x);
                  	}
                  	return tmp;
                  }
                  
                  def code(x):
                  	tmp = 0
                  	if x <= 0.19634788085319213:
                  		tmp = 1.0 - math.sqrt(x)
                  	else:
                  		tmp = 0.5 / math.sqrt(x)
                  	return tmp
                  
                  function code(x)
                  	tmp = 0.0
                  	if (x <= 0.19634788085319213)
                  		tmp = Float64(1.0 - sqrt(x));
                  	else
                  		tmp = Float64(0.5 / sqrt(x));
                  	end
                  	return tmp
                  end
                  
                  function tmp_2 = code(x)
                  	tmp = 0.0;
                  	if (x <= 0.19634788085319213)
                  		tmp = 1.0 - sqrt(x);
                  	else
                  		tmp = 0.5 / sqrt(x);
                  	end
                  	tmp_2 = tmp;
                  end
                  
                  code[x_] := If[LessEqual[x, 0.19634788085319213], N[(1.0 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], N[(0.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]]
                  
                  f(x):
                  	x in [-inf, +inf]
                  code: THEORY
                  BEGIN
                  f(x: real): real =
                  	LET tmp = IF (x <= (1963478808531921304680878392900922335684299468994140625e-55)) THEN ((1) - (sqrt(x))) ELSE ((5e-1) / (sqrt(x))) ENDIF IN
                  	tmp
                  END code
                  \begin{array}{l}
                  \mathbf{if}\;x \leq 0.19634788085319213:\\
                  \;\;\;\;1 - \sqrt{x}\\
                  
                  \mathbf{else}:\\
                  \;\;\;\;\frac{0.5}{\sqrt{x}}\\
                  
                  
                  \end{array}
                  
                  Derivation
                  1. Split input into 2 regimes
                  2. if x < 0.19634788085319213

                    1. Initial program 52.8%

                      \[\sqrt{x + 1} - \sqrt{x} \]
                    2. Taylor expanded in x around 0

                      \[\leadsto 1 - \sqrt{x} \]
                    3. Step-by-step derivation
                      1. Applied rewrites49.0%

                        \[\leadsto 1 - \sqrt{x} \]

                      if 0.19634788085319213 < x

                      1. Initial program 52.8%

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

                        \[\leadsto \frac{\frac{1}{2}}{x \cdot \sqrt{\frac{1}{x}}} \]
                      3. Step-by-step derivation
                        1. Applied rewrites52.8%

                          \[\leadsto \frac{0.5}{x \cdot \sqrt{\frac{1}{x}}} \]
                        2. Applied rewrites52.9%

                          \[\leadsto \frac{0.5}{\sqrt{x}} \]
                      4. Recombined 2 regimes into one program.
                      5. Add Preprocessing

                      Alternative 5: 50.6% accurate, 1.8× speedup?

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

                        \[\sqrt{x + 1} - \sqrt{x} \]
                      2. Taylor expanded in x around 0

                        \[\leadsto 1 - \sqrt{x} \]
                      3. Step-by-step derivation
                        1. Applied rewrites49.0%

                          \[\leadsto 1 - \sqrt{x} \]
                        2. Taylor expanded in undef-var around zero

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

                            \[\leadsto 1 - \sqrt{0} \]
                          2. Add Preprocessing

                          Alternative 6: 49.0% accurate, 1.8× speedup?

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

                            \[\sqrt{x + 1} - \sqrt{x} \]
                          2. Taylor expanded in x around 0

                            \[\leadsto 1 - \sqrt{x} \]
                          3. Step-by-step derivation
                            1. Applied rewrites49.0%

                              \[\leadsto 1 - \sqrt{x} \]
                            2. Add Preprocessing

                            Reproduce

                            ?
                            herbie shell --seed 2026092 
                            (FPCore (x)
                              :name "Main:bigenough3 from C"
                              :precision binary64
                              (- (sqrt (+ x 1.0)) (sqrt x)))