sqrt E (should all be same)

Percentage Accurate: 54.5% → 99.3%
Time: 1.8s
Alternatives: 3
Speedup: 13.5×

Specification

?
\[\begin{array}{l} \\ \sqrt{{x}^{2} + {x}^{2}} \end{array} \]
(FPCore (x) :precision binary64 (sqrt (+ (pow x 2.0) (pow x 2.0))))
double code(double x) {
	return sqrt((pow(x, 2.0) + pow(x, 2.0)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = sqrt(((x ** 2.0d0) + (x ** 2.0d0)))
end function
public static double code(double x) {
	return Math.sqrt((Math.pow(x, 2.0) + Math.pow(x, 2.0)));
}
def code(x):
	return math.sqrt((math.pow(x, 2.0) + math.pow(x, 2.0)))
function code(x)
	return sqrt(Float64((x ^ 2.0) + (x ^ 2.0)))
end
function tmp = code(x)
	tmp = sqrt(((x ^ 2.0) + (x ^ 2.0)));
end
code[x_] := N[Sqrt[N[(N[Power[x, 2.0], $MachinePrecision] + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{{x}^{2} + {x}^{2}}
\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 3 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: 54.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{{x}^{2} + {x}^{2}} \end{array} \]
(FPCore (x) :precision binary64 (sqrt (+ (pow x 2.0) (pow x 2.0))))
double code(double x) {
	return sqrt((pow(x, 2.0) + pow(x, 2.0)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = sqrt(((x ** 2.0d0) + (x ** 2.0d0)))
end function
public static double code(double x) {
	return Math.sqrt((Math.pow(x, 2.0) + Math.pow(x, 2.0)));
}
def code(x):
	return math.sqrt((math.pow(x, 2.0) + math.pow(x, 2.0)))
function code(x)
	return sqrt(Float64((x ^ 2.0) + (x ^ 2.0)))
end
function tmp = code(x)
	tmp = sqrt(((x ^ 2.0) + (x ^ 2.0)));
end
code[x_] := N[Sqrt[N[(N[Power[x, 2.0], $MachinePrecision] + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{{x}^{2} + {x}^{2}}
\end{array}

Alternative 1: 99.3% accurate, 8.6× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \frac{x\_m + x\_m}{\sqrt{2}} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m) :precision binary64 (/ (+ x_m x_m) (sqrt 2.0)))
x_m = fabs(x);
double code(double x_m) {
	return (x_m + x_m) / sqrt(2.0);
}
x_m =     private
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x_m)
use fmin_fmax_functions
    real(8), intent (in) :: x_m
    code = (x_m + x_m) / sqrt(2.0d0)
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	return (x_m + x_m) / Math.sqrt(2.0);
}
x_m = math.fabs(x)
def code(x_m):
	return (x_m + x_m) / math.sqrt(2.0)
x_m = abs(x)
function code(x_m)
	return Float64(Float64(x_m + x_m) / sqrt(2.0))
end
x_m = abs(x);
function tmp = code(x_m)
	tmp = (x_m + x_m) / sqrt(2.0);
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(N[(x$95$m + x$95$m), $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
\frac{x\_m + x\_m}{\sqrt{2}}
\end{array}
Derivation
  1. Initial program 54.3%

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

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

      \[\leadsto \color{blue}{\sqrt{2} \cdot x} \]
    2. Step-by-step derivation
      1. Applied rewrites51.5%

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

          \[\leadsto \frac{2 \cdot x}{\color{blue}{\sqrt{2}}} \]
        2. Step-by-step derivation
          1. Applied rewrites51.6%

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

          Alternative 2: 99.3% accurate, 13.5× speedup?

          \[\begin{array}{l} x_m = \left|x\right| \\ \sqrt{2} \cdot x\_m \end{array} \]
          x_m = (fabs.f64 x)
          (FPCore (x_m) :precision binary64 (* (sqrt 2.0) x_m))
          x_m = fabs(x);
          double code(double x_m) {
          	return sqrt(2.0) * x_m;
          }
          
          x_m =     private
          module fmin_fmax_functions
              implicit none
              private
              public fmax
              public fmin
          
              interface fmax
                  module procedure fmax88
                  module procedure fmax44
                  module procedure fmax84
                  module procedure fmax48
              end interface
              interface fmin
                  module procedure fmin88
                  module procedure fmin44
                  module procedure fmin84
                  module procedure fmin48
              end interface
          contains
              real(8) function fmax88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(4) function fmax44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(8) function fmax84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmax48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
              end function
              real(8) function fmin88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(4) function fmin44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(8) function fmin84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmin48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
              end function
          end module
          
          real(8) function code(x_m)
          use fmin_fmax_functions
              real(8), intent (in) :: x_m
              code = sqrt(2.0d0) * x_m
          end function
          
          x_m = Math.abs(x);
          public static double code(double x_m) {
          	return Math.sqrt(2.0) * x_m;
          }
          
          x_m = math.fabs(x)
          def code(x_m):
          	return math.sqrt(2.0) * x_m
          
          x_m = abs(x)
          function code(x_m)
          	return Float64(sqrt(2.0) * x_m)
          end
          
          x_m = abs(x);
          function tmp = code(x_m)
          	tmp = sqrt(2.0) * x_m;
          end
          
          x_m = N[Abs[x], $MachinePrecision]
          code[x$95$m_] := N[(N[Sqrt[2.0], $MachinePrecision] * x$95$m), $MachinePrecision]
          
          \begin{array}{l}
          x_m = \left|x\right|
          
          \\
          \sqrt{2} \cdot x\_m
          \end{array}
          
          Derivation
          1. Initial program 54.3%

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

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

              \[\leadsto \color{blue}{\sqrt{2} \cdot x} \]
            2. Add Preprocessing

            Alternative 3: 5.4% accurate, 19.6× speedup?

            \[\begin{array}{l} x_m = \left|x\right| \\ \sqrt{2} \end{array} \]
            x_m = (fabs.f64 x)
            (FPCore (x_m) :precision binary64 (sqrt 2.0))
            x_m = fabs(x);
            double code(double x_m) {
            	return sqrt(2.0);
            }
            
            x_m =     private
            module fmin_fmax_functions
                implicit none
                private
                public fmax
                public fmin
            
                interface fmax
                    module procedure fmax88
                    module procedure fmax44
                    module procedure fmax84
                    module procedure fmax48
                end interface
                interface fmin
                    module procedure fmin88
                    module procedure fmin44
                    module procedure fmin84
                    module procedure fmin48
                end interface
            contains
                real(8) function fmax88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(4) function fmax44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(8) function fmax84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmax48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                end function
                real(8) function fmin88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(4) function fmin44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(8) function fmin84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmin48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                end function
            end module
            
            real(8) function code(x_m)
            use fmin_fmax_functions
                real(8), intent (in) :: x_m
                code = sqrt(2.0d0)
            end function
            
            x_m = Math.abs(x);
            public static double code(double x_m) {
            	return Math.sqrt(2.0);
            }
            
            x_m = math.fabs(x)
            def code(x_m):
            	return math.sqrt(2.0)
            
            x_m = abs(x)
            function code(x_m)
            	return sqrt(2.0)
            end
            
            x_m = abs(x);
            function tmp = code(x_m)
            	tmp = sqrt(2.0);
            end
            
            x_m = N[Abs[x], $MachinePrecision]
            code[x$95$m_] := N[Sqrt[2.0], $MachinePrecision]
            
            \begin{array}{l}
            x_m = \left|x\right|
            
            \\
            \sqrt{2}
            \end{array}
            
            Derivation
            1. Initial program 54.3%

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

              \[\leadsto \sqrt{\color{blue}{2 \cdot {x}^{2}}} \]
            4. Step-by-step derivation
              1. Applied rewrites5.4%

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

              Reproduce

              ?
              herbie shell --seed 2025026 
              (FPCore (x)
                :name "sqrt E (should all be same)"
                :precision binary64
                (sqrt (+ (pow x 2.0) (pow x 2.0))))