
(FPCore (x y) :precision binary64 (sqrt (+ (pow x 2.0) (pow y 2.0))))
double code(double x, double y) {
return sqrt((pow(x, 2.0) + pow(y, 2.0)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x ** 2.0d0) + (y ** 2.0d0)))
end function
public static double code(double x, double y) {
return Math.sqrt((Math.pow(x, 2.0) + Math.pow(y, 2.0)));
}
def code(x, y): return math.sqrt((math.pow(x, 2.0) + math.pow(y, 2.0)))
function code(x, y) return sqrt(Float64((x ^ 2.0) + (y ^ 2.0))) end
function tmp = code(x, y) tmp = sqrt(((x ^ 2.0) + (y ^ 2.0))); end
code[x_, y_] := N[Sqrt[N[(N[Power[x, 2.0], $MachinePrecision] + N[Power[y, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\sqrt{{x}^{2} + {y}^{2}}
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (sqrt (+ (pow x 2.0) (pow y 2.0))))
double code(double x, double y) {
return sqrt((pow(x, 2.0) + pow(y, 2.0)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x ** 2.0d0) + (y ** 2.0d0)))
end function
public static double code(double x, double y) {
return Math.sqrt((Math.pow(x, 2.0) + Math.pow(y, 2.0)));
}
def code(x, y): return math.sqrt((math.pow(x, 2.0) + math.pow(y, 2.0)))
function code(x, y) return sqrt(Float64((x ^ 2.0) + (y ^ 2.0))) end
function tmp = code(x, y) tmp = sqrt(((x ^ 2.0) + (y ^ 2.0))); end
code[x_, y_] := N[Sqrt[N[(N[Power[x, 2.0], $MachinePrecision] + N[Power[y, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\sqrt{{x}^{2} + {y}^{2}}
(FPCore (x y) :precision binary64 (hypot y x))
double code(double x, double y) {
return hypot(y, x);
}
public static double code(double x, double y) {
return Math.hypot(y, x);
}
def code(x, y): return math.hypot(y, x)
function code(x, y) return hypot(y, x) end
function tmp = code(x, y) tmp = hypot(y, x); end
code[x_, y_] := N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision]
\mathsf{hypot}\left(y, x\right)
Initial program 54.7%
lift-sqrt.f64N/A
sqrt-fabs-revN/A
lift-sqrt.f64N/A
rem-sqrt-square-revN/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
lift-+.f64N/A
+-commutativeN/A
lift-pow.f64N/A
unpow2N/A
sqr-abs-revN/A
sqr-neg-revN/A
fp-cancel-sign-sub-invN/A
fp-cancel-sub-sign-invN/A
lift-pow.f64N/A
unpow2N/A
distribute-lft-neg-inN/A
distribute-rgt-neg-outN/A
sqr-neg-revN/A
sqr-neg-revN/A
sqr-abs-revN/A
Applied rewrites100.0%
(FPCore (x y) :precision binary64 (sqrt (fma y y (* x x))))
double code(double x, double y) {
return sqrt(fma(y, y, (x * x)));
}
function code(x, y) return sqrt(fma(y, y, Float64(x * x))) end
code[x_, y_] := N[Sqrt[N[(y * y + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\sqrt{\mathsf{fma}\left(y, y, x \cdot x\right)}
Initial program 54.7%
lift-+.f64N/A
+-commutativeN/A
lift-pow.f64N/A
unpow2N/A
sqr-abs-revN/A
sqr-neg-revN/A
fp-cancel-sign-sub-invN/A
fp-cancel-sub-sign-invN/A
lift-pow.f64N/A
unpow2N/A
distribute-lft-neg-inN/A
distribute-rgt-neg-outN/A
sqr-neg-revN/A
sqr-neg-revN/A
sqr-abs-revN/A
unpow2N/A
lift-pow.f64N/A
lower-fma.f6454.7%
lift-pow.f64N/A
unpow2N/A
lower-*.f6454.7%
Applied rewrites54.7%
(FPCore (x y) :precision binary64 (- (fmin x y)))
double code(double x, double y) {
return -fmin(x, y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
public static double code(double x, double y) {
return -fmin(x, y);
}
def code(x, y): return -fmin(x, y)
function code(x, y) return Float64(-((x != x) ? y : ((y != y) ? x : min(x, y)))) end
function tmp = code(x, y) tmp = -min(x, y); end
code[x_, y_] := (-N[Min[x, y], $MachinePrecision])
-\mathsf{min}\left(x, y\right)
Initial program 54.7%
Taylor expanded in x around -inf
lower-*.f6427.5%
Applied rewrites27.5%
lift-*.f64N/A
mul-1-negN/A
lower-neg.f6427.5%
Applied rewrites27.5%
herbie shell --seed 1
(FPCore (x y)
:name "sqrt(pow(x,2)+pow(y,2))"
:precision binary64
:pre (and (and (<= -1.79e+308 x) (<= x 1.79e+308)) (and (<= -1.79e+308 y) (<= y 1.79e+308)))
(sqrt (+ (pow x 2.0) (pow y 2.0))))