
(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:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (x) :precision binary64 (- 1.0 (/ (fabs x) x)))
double code(double x) {
return 1.0 - (fabs(x) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 - (abs(x) / x)
end function
public static double code(double x) {
return 1.0 - (Math.abs(x) / x);
}
def code(x): return 1.0 - (math.fabs(x) / x)
function code(x) return Float64(1.0 - Float64(abs(x) / x)) end
function tmp = code(x) tmp = 1.0 - (abs(x) / x); end
code[x_] := N[(1.0 - N[(N[Abs[x], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 - \frac{\left|x\right|}{x}
\end{array}
Initial program 47.6%
cancel-sign-sub-inv47.6%
*-inverses47.6%
distribute-frac-neg247.6%
sqr-neg47.6%
*-inverses47.6%
cancel-sign-sub47.6%
*-inverses47.6%
*-inverses47.6%
distribute-neg-frac47.6%
*-inverses47.6%
metadata-eval47.6%
associate-*l/53.3%
neg-mul-153.3%
distribute-neg-frac53.3%
distribute-neg-frac253.3%
Simplified100.0%
(FPCore (x) :precision binary64 (if (<= x -5e-311) 1.0 0.0))
double code(double x) {
double tmp;
if (x <= -5e-311) {
tmp = 1.0;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-5d-311)) then
tmp = 1.0d0
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -5e-311) {
tmp = 1.0;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= -5e-311: tmp = 1.0 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= -5e-311) tmp = 1.0; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -5e-311) tmp = 1.0; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -5e-311], 1.0, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < -5.00000000000023e-311Initial program 52.4%
cancel-sign-sub-inv52.4%
*-inverses52.4%
distribute-frac-neg252.4%
sqr-neg52.4%
*-inverses52.4%
cancel-sign-sub52.4%
*-inverses52.4%
*-inverses52.4%
distribute-neg-frac52.4%
*-inverses52.4%
metadata-eval52.4%
associate-*l/52.4%
neg-mul-152.4%
distribute-neg-frac52.4%
distribute-neg-frac252.4%
Simplified100.0%
Taylor expanded in x around inf 18.8%
if -5.00000000000023e-311 < x Initial program 42.4%
cancel-sign-sub-inv42.4%
*-inverses42.4%
distribute-frac-neg242.4%
sqr-neg42.4%
*-inverses42.4%
cancel-sign-sub42.4%
*-inverses42.4%
*-inverses42.4%
distribute-neg-frac42.4%
*-inverses42.4%
metadata-eval42.4%
associate-*l/54.3%
neg-mul-154.3%
distribute-neg-frac54.3%
distribute-neg-frac254.3%
Simplified100.0%
Taylor expanded in x around 0 100.0%
div-sub100.0%
*-inverses100.0%
rem-square-sqrt51.6%
fabs-sqr51.6%
rem-square-sqrt100.0%
*-inverses100.0%
metadata-eval100.0%
Simplified100.0%
(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}
Initial program 47.6%
cancel-sign-sub-inv47.6%
*-inverses47.6%
distribute-frac-neg247.6%
sqr-neg47.6%
*-inverses47.6%
cancel-sign-sub47.6%
*-inverses47.6%
*-inverses47.6%
distribute-neg-frac47.6%
*-inverses47.6%
metadata-eval47.6%
associate-*l/53.3%
neg-mul-153.3%
distribute-neg-frac53.3%
distribute-neg-frac253.3%
Simplified100.0%
Taylor expanded in x around 0 100.0%
div-sub100.0%
*-inverses100.0%
rem-square-sqrt24.6%
fabs-sqr24.6%
rem-square-sqrt49.3%
*-inverses49.3%
metadata-eval49.3%
Simplified49.3%
(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}
herbie shell --seed 2024131
(FPCore (x)
:name "sqrt sqr"
:precision binary64
:alt
(! :herbie-platform default (if (< x 0) 2 0))
(- (/ x x) (* (/ 1.0 x) (sqrt (* x x)))))