Data.Octree.Internal:octantDistance from Octree-0.5.4.2

Percentage Accurate: 53.2% → 99.0%
Time: 1.7s
Alternatives: 1
Speedup: 24.0×

Specification

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

\\
\sqrt{x \cdot x + y \cdot 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 1 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: 53.2% accurate, 1.0× speedup?

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

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

Alternative 1: 99.0% accurate, 24.0× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ x_m = \left|x\right| \\ [x_m, y_m] = \mathsf{sort}([x_m, y_m])\\ \\ y\_m \end{array} \]
y_m = (fabs.f64 y)
x_m = (fabs.f64 x)
NOTE: x_m and y_m should be sorted in increasing order before calling this function.
(FPCore (x_m y_m) :precision binary64 y_m)
y_m = fabs(y);
x_m = fabs(x);
assert(x_m < y_m);
double code(double x_m, double y_m) {
	return y_m;
}
y_m =     private
x_m =     private
NOTE: x_m and y_m should be sorted in increasing order before calling this function.
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, y_m)
use fmin_fmax_functions
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y_m
    code = y_m
end function
y_m = Math.abs(y);
x_m = Math.abs(x);
assert x_m < y_m;
public static double code(double x_m, double y_m) {
	return y_m;
}
y_m = math.fabs(y)
x_m = math.fabs(x)
[x_m, y_m] = sort([x_m, y_m])
def code(x_m, y_m):
	return y_m
y_m = abs(y)
x_m = abs(x)
x_m, y_m = sort([x_m, y_m])
function code(x_m, y_m)
	return y_m
end
y_m = abs(y);
x_m = abs(x);
x_m, y_m = num2cell(sort([x_m, y_m])){:}
function tmp = code(x_m, y_m)
	tmp = y_m;
end
y_m = N[Abs[y], $MachinePrecision]
x_m = N[Abs[x], $MachinePrecision]
NOTE: x_m and y_m should be sorted in increasing order before calling this function.
code[x$95$m_, y$95$m_] := y$95$m
\begin{array}{l}
y_m = \left|y\right|
\\
x_m = \left|x\right|
\\
[x_m, y_m] = \mathsf{sort}([x_m, y_m])\\
\\
y\_m
\end{array}
Derivation
  1. Initial program 59.0%

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

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

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

    Developer Target 1: 52.3% accurate, 0.7× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x < -1.1236950826599826 \cdot 10^{+145}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x < 1.116557621183362 \cdot 10^{+93}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
    (FPCore (x y)
     :precision binary64
     (if (< x -1.1236950826599826e+145)
       (- x)
       (if (< x 1.116557621183362e+93) (sqrt (+ (* x x) (* y y))) x)))
    double code(double x, double y) {
    	double tmp;
    	if (x < -1.1236950826599826e+145) {
    		tmp = -x;
    	} else if (x < 1.116557621183362e+93) {
    		tmp = sqrt(((x * x) + (y * y)));
    	} else {
    		tmp = x;
    	}
    	return tmp;
    }
    
    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, y)
    use fmin_fmax_functions
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8) :: tmp
        if (x < (-1.1236950826599826d+145)) then
            tmp = -x
        else if (x < 1.116557621183362d+93) then
            tmp = sqrt(((x * x) + (y * y)))
        else
            tmp = x
        end if
        code = tmp
    end function
    
    public static double code(double x, double y) {
    	double tmp;
    	if (x < -1.1236950826599826e+145) {
    		tmp = -x;
    	} else if (x < 1.116557621183362e+93) {
    		tmp = Math.sqrt(((x * x) + (y * y)));
    	} else {
    		tmp = x;
    	}
    	return tmp;
    }
    
    def code(x, y):
    	tmp = 0
    	if x < -1.1236950826599826e+145:
    		tmp = -x
    	elif x < 1.116557621183362e+93:
    		tmp = math.sqrt(((x * x) + (y * y)))
    	else:
    		tmp = x
    	return tmp
    
    function code(x, y)
    	tmp = 0.0
    	if (x < -1.1236950826599826e+145)
    		tmp = Float64(-x);
    	elseif (x < 1.116557621183362e+93)
    		tmp = sqrt(Float64(Float64(x * x) + Float64(y * y)));
    	else
    		tmp = x;
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y)
    	tmp = 0.0;
    	if (x < -1.1236950826599826e+145)
    		tmp = -x;
    	elseif (x < 1.116557621183362e+93)
    		tmp = sqrt(((x * x) + (y * y)));
    	else
    		tmp = x;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_] := If[Less[x, -1.1236950826599826e+145], (-x), If[Less[x, 1.116557621183362e+93], N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], x]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;x < -1.1236950826599826 \cdot 10^{+145}:\\
    \;\;\;\;-x\\
    
    \mathbf{elif}\;x < 1.116557621183362 \cdot 10^{+93}:\\
    \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\
    
    \mathbf{else}:\\
    \;\;\;\;x\\
    
    
    \end{array}
    \end{array}
    

    Reproduce

    ?
    herbie shell --seed 2025018 
    (FPCore (x y)
      :name "Data.Octree.Internal:octantDistance  from Octree-0.5.4.2"
      :precision binary64
    
      :alt
      (! :herbie-platform default (if (< x -11236950826599826000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (- x) (if (< x 1116557621183362000000000000000000000000000000000000000000000000000000000000000000000000000000) (sqrt (+ (* x x) (* y y))) x)))
    
      (sqrt (+ (* x x) (* y y))))