
(FPCore (a b angle x-scale y-scale)
:precision binary64
(let* ((t_0 (* (/ angle 180.0) (PI)))
(t_1 (sin t_0))
(t_2 (cos t_0))
(t_3
(/ (/ (+ (pow (* a t_2) 2.0) (pow (* b t_1) 2.0)) y-scale) y-scale))
(t_4
(/ (/ (+ (pow (* a t_1) 2.0) (pow (* b t_2) 2.0)) x-scale) x-scale))
(t_5 (* (* b a) (* b (- a))))
(t_6 (/ (* 4.0 t_5) (pow (* x-scale y-scale) 2.0))))
(/
(-
(sqrt
(*
(* (* 2.0 t_6) t_5)
(-
(+ t_4 t_3)
(sqrt
(+
(pow (- t_4 t_3) 2.0)
(pow
(/
(/ (* (* (* 2.0 (- (pow b 2.0) (pow a 2.0))) t_1) t_2) x-scale)
y-scale)
2.0)))))))
t_6)))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{angle}{180} \cdot \mathsf{PI}\left(\right)\\
t_1 := \sin t\_0\\
t_2 := \cos t\_0\\
t_3 := \frac{\frac{{\left(a \cdot t\_2\right)}^{2} + {\left(b \cdot t\_1\right)}^{2}}{y-scale}}{y-scale}\\
t_4 := \frac{\frac{{\left(a \cdot t\_1\right)}^{2} + {\left(b \cdot t\_2\right)}^{2}}{x-scale}}{x-scale}\\
t_5 := \left(b \cdot a\right) \cdot \left(b \cdot \left(-a\right)\right)\\
t_6 := \frac{4 \cdot t\_5}{{\left(x-scale \cdot y-scale\right)}^{2}}\\
\frac{-\sqrt{\left(\left(2 \cdot t\_6\right) \cdot t\_5\right) \cdot \left(\left(t\_4 + t\_3\right) - \sqrt{{\left(t\_4 - t\_3\right)}^{2} + {\left(\frac{\frac{\left(\left(2 \cdot \left({b}^{2} - {a}^{2}\right)\right) \cdot t\_1\right) \cdot t\_2}{x-scale}}{y-scale}\right)}^{2}}\right)}}{t\_6}
\end{array}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b angle x-scale y-scale)
:precision binary64
(let* ((t_0 (* (/ angle 180.0) (PI)))
(t_1 (sin t_0))
(t_2 (cos t_0))
(t_3
(/ (/ (+ (pow (* a t_2) 2.0) (pow (* b t_1) 2.0)) y-scale) y-scale))
(t_4
(/ (/ (+ (pow (* a t_1) 2.0) (pow (* b t_2) 2.0)) x-scale) x-scale))
(t_5 (* (* b a) (* b (- a))))
(t_6 (/ (* 4.0 t_5) (pow (* x-scale y-scale) 2.0))))
(/
(-
(sqrt
(*
(* (* 2.0 t_6) t_5)
(-
(+ t_4 t_3)
(sqrt
(+
(pow (- t_4 t_3) 2.0)
(pow
(/
(/ (* (* (* 2.0 (- (pow b 2.0) (pow a 2.0))) t_1) t_2) x-scale)
y-scale)
2.0)))))))
t_6)))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{angle}{180} \cdot \mathsf{PI}\left(\right)\\
t_1 := \sin t\_0\\
t_2 := \cos t\_0\\
t_3 := \frac{\frac{{\left(a \cdot t\_2\right)}^{2} + {\left(b \cdot t\_1\right)}^{2}}{y-scale}}{y-scale}\\
t_4 := \frac{\frac{{\left(a \cdot t\_1\right)}^{2} + {\left(b \cdot t\_2\right)}^{2}}{x-scale}}{x-scale}\\
t_5 := \left(b \cdot a\right) \cdot \left(b \cdot \left(-a\right)\right)\\
t_6 := \frac{4 \cdot t\_5}{{\left(x-scale \cdot y-scale\right)}^{2}}\\
\frac{-\sqrt{\left(\left(2 \cdot t\_6\right) \cdot t\_5\right) \cdot \left(\left(t\_4 + t\_3\right) - \sqrt{{\left(t\_4 - t\_3\right)}^{2} + {\left(\frac{\frac{\left(\left(2 \cdot \left({b}^{2} - {a}^{2}\right)\right) \cdot t\_1\right) \cdot t\_2}{x-scale}}{y-scale}\right)}^{2}}\right)}}{t\_6}
\end{array}
\end{array}
a_m = (fabs.f64 a)
b_m = (fabs.f64 b)
x-scale_m = (fabs.f64 x-scale)
(FPCore (a_m b_m angle x-scale_m y-scale)
:precision binary64
(let* ((t_0
(/
(pow (sin (* 0.005555555555555556 (* angle (PI)))) 2.0)
(* y-scale y-scale))))
(if (<= b_m 3.5e-209)
(*
0.25
(* (* b_m (* x-scale_m (* y-scale (sqrt 8.0)))) (sqrt (- t_0 t_0))))
(* a_m x-scale_m))))\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|
\\
x-scale_m = \left|x-scale\right|
\\
\begin{array}{l}
t_0 := \frac{{\sin \left(0.005555555555555556 \cdot \left(angle \cdot \mathsf{PI}\left(\right)\right)\right)}^{2}}{y-scale \cdot y-scale}\\
\mathbf{if}\;b\_m \leq 3.5 \cdot 10^{-209}:\\
\;\;\;\;0.25 \cdot \left(\left(b\_m \cdot \left(x-scale\_m \cdot \left(y-scale \cdot \sqrt{8}\right)\right)\right) \cdot \sqrt{t\_0 - t\_0}\right)\\
\mathbf{else}:\\
\;\;\;\;a\_m \cdot x-scale\_m\\
\end{array}
\end{array}
if b < 3.50000000000000002e-209Initial program 0.1%
Taylor expanded in b around inf
Applied rewrites6.9%
Taylor expanded in x-scale around inf
lift-*.f64N/A
lift-PI.f64N/A
lift-*.f64N/A
lift-sin.f64N/A
lift-pow.f64N/A
pow2N/A
lift-/.f64N/A
lift-*.f6410.5
Applied rewrites10.5%
Taylor expanded in x-scale around inf
pow2N/A
pow-to-expN/A
lift-*.f64N/A
lift-PI.f64N/A
lift-*.f64N/A
lift-sin.f64N/A
lift-pow.f64N/A
pow2N/A
lift-/.f64N/A
Applied rewrites23.0%
if 3.50000000000000002e-209 < b Initial program 0.1%
Taylor expanded in angle around 0
Applied rewrites19.1%
Taylor expanded in a around 0
lower-*.f6419.1
Applied rewrites19.1%
a_m = (fabs.f64 a)
b_m = (fabs.f64 b)
x-scale_m = (fabs.f64 x-scale)
(FPCore (a_m b_m angle x-scale_m y-scale)
:precision binary64
(let* ((t_0 (* (PI) (PI))) (t_1 (/ t_0 (* y-scale y-scale))))
(if (<= b_m 8e-233)
(*
0.25
(*
(* x-scale_m (* y-scale (sqrt 8.0)))
(sqrt
(*
(* a_m a_m)
(-
(pow y-scale -2.0)
(*
16200.0
(/
(fma -6.17283950617284e-5 t_1 (* 0.0001234567901234568 t_1))
t_0)))))))
(* a_m x-scale_m))))\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|
\\
x-scale_m = \left|x-scale\right|
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\\
t_1 := \frac{t\_0}{y-scale \cdot y-scale}\\
\mathbf{if}\;b\_m \leq 8 \cdot 10^{-233}:\\
\;\;\;\;0.25 \cdot \left(\left(x-scale\_m \cdot \left(y-scale \cdot \sqrt{8}\right)\right) \cdot \sqrt{\left(a\_m \cdot a\_m\right) \cdot \left({y-scale}^{-2} - 16200 \cdot \frac{\mathsf{fma}\left(-6.17283950617284 \cdot 10^{-5}, t\_1, 0.0001234567901234568 \cdot t\_1\right)}{t\_0}\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;a\_m \cdot x-scale\_m\\
\end{array}
\end{array}
if b < 7.99999999999999966e-233Initial program 0.1%
Taylor expanded in x-scale around 0
Applied rewrites4.5%
Taylor expanded in a around inf
Applied rewrites13.7%
Taylor expanded in angle around 0
lower--.f64N/A
pow-flipN/A
metadata-evalN/A
lower-pow.f64N/A
lower-*.f64N/A
Applied rewrites17.7%
if 7.99999999999999966e-233 < b Initial program 0.1%
Taylor expanded in angle around 0
Applied rewrites18.7%
Taylor expanded in a around 0
lower-*.f6418.7
Applied rewrites18.7%
a_m = (fabs.f64 a)
b_m = (fabs.f64 b)
x-scale_m = (fabs.f64 x-scale)
(FPCore (a_m b_m angle x-scale_m y-scale)
:precision binary64
(if (<= b_m 5.5e-233)
(*
0.25
(*
(* x-scale_m (* y-scale (sqrt 8.0)))
(sqrt (/ (* 2.0 (* a_m a_m)) (* y-scale y-scale)))))
(* a_m x-scale_m)))a_m = fabs(a);
b_m = fabs(b);
x-scale_m = fabs(x_45_scale);
double code(double a_m, double b_m, double angle, double x_45_scale_m, double y_45_scale) {
double tmp;
if (b_m <= 5.5e-233) {
tmp = 0.25 * ((x_45_scale_m * (y_45_scale * sqrt(8.0))) * sqrt(((2.0 * (a_m * a_m)) / (y_45_scale * y_45_scale))));
} else {
tmp = a_m * x_45_scale_m;
}
return tmp;
}
a_m = private
b_m = private
x-scale_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(a_m, b_m, angle, x_45scale_m, y_45scale)
use fmin_fmax_functions
real(8), intent (in) :: a_m
real(8), intent (in) :: b_m
real(8), intent (in) :: angle
real(8), intent (in) :: x_45scale_m
real(8), intent (in) :: y_45scale
real(8) :: tmp
if (b_m <= 5.5d-233) then
tmp = 0.25d0 * ((x_45scale_m * (y_45scale * sqrt(8.0d0))) * sqrt(((2.0d0 * (a_m * a_m)) / (y_45scale * y_45scale))))
else
tmp = a_m * x_45scale_m
end if
code = tmp
end function
a_m = Math.abs(a);
b_m = Math.abs(b);
x-scale_m = Math.abs(x_45_scale);
public static double code(double a_m, double b_m, double angle, double x_45_scale_m, double y_45_scale) {
double tmp;
if (b_m <= 5.5e-233) {
tmp = 0.25 * ((x_45_scale_m * (y_45_scale * Math.sqrt(8.0))) * Math.sqrt(((2.0 * (a_m * a_m)) / (y_45_scale * y_45_scale))));
} else {
tmp = a_m * x_45_scale_m;
}
return tmp;
}
a_m = math.fabs(a) b_m = math.fabs(b) x-scale_m = math.fabs(x_45_scale) def code(a_m, b_m, angle, x_45_scale_m, y_45_scale): tmp = 0 if b_m <= 5.5e-233: tmp = 0.25 * ((x_45_scale_m * (y_45_scale * math.sqrt(8.0))) * math.sqrt(((2.0 * (a_m * a_m)) / (y_45_scale * y_45_scale)))) else: tmp = a_m * x_45_scale_m return tmp
a_m = abs(a) b_m = abs(b) x-scale_m = abs(x_45_scale) function code(a_m, b_m, angle, x_45_scale_m, y_45_scale) tmp = 0.0 if (b_m <= 5.5e-233) tmp = Float64(0.25 * Float64(Float64(x_45_scale_m * Float64(y_45_scale * sqrt(8.0))) * sqrt(Float64(Float64(2.0 * Float64(a_m * a_m)) / Float64(y_45_scale * y_45_scale))))); else tmp = Float64(a_m * x_45_scale_m); end return tmp end
a_m = abs(a); b_m = abs(b); x-scale_m = abs(x_45_scale); function tmp_2 = code(a_m, b_m, angle, x_45_scale_m, y_45_scale) tmp = 0.0; if (b_m <= 5.5e-233) tmp = 0.25 * ((x_45_scale_m * (y_45_scale * sqrt(8.0))) * sqrt(((2.0 * (a_m * a_m)) / (y_45_scale * y_45_scale)))); else tmp = a_m * x_45_scale_m; end tmp_2 = tmp; end
a_m = N[Abs[a], $MachinePrecision] b_m = N[Abs[b], $MachinePrecision] x-scale_m = N[Abs[x$45$scale], $MachinePrecision] code[a$95$m_, b$95$m_, angle_, x$45$scale$95$m_, y$45$scale_] := If[LessEqual[b$95$m, 5.5e-233], N[(0.25 * N[(N[(x$45$scale$95$m * N[(y$45$scale * N[Sqrt[8.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(N[(2.0 * N[(a$95$m * a$95$m), $MachinePrecision]), $MachinePrecision] / N[(y$45$scale * y$45$scale), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a$95$m * x$45$scale$95$m), $MachinePrecision]]
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|
\\
x-scale_m = \left|x-scale\right|
\\
\begin{array}{l}
\mathbf{if}\;b\_m \leq 5.5 \cdot 10^{-233}:\\
\;\;\;\;0.25 \cdot \left(\left(x-scale\_m \cdot \left(y-scale \cdot \sqrt{8}\right)\right) \cdot \sqrt{\frac{2 \cdot \left(a\_m \cdot a\_m\right)}{y-scale \cdot y-scale}}\right)\\
\mathbf{else}:\\
\;\;\;\;a\_m \cdot x-scale\_m\\
\end{array}
\end{array}
if b < 5.50000000000000005e-233Initial program 0.1%
Taylor expanded in x-scale around 0
Applied rewrites4.5%
Taylor expanded in angle around 0
lower--.f64N/A
Applied rewrites13.7%
Taylor expanded in y-scale around 0
lower-/.f64N/A
lower--.f64N/A
pow2N/A
lift-*.f64N/A
lower-*.f64N/A
pow2N/A
lift-*.f64N/A
pow2N/A
lift-*.f6413.7
Applied rewrites13.7%
Taylor expanded in a around 0
lower-*.f64N/A
pow2N/A
lift-*.f6413.7
Applied rewrites13.7%
if 5.50000000000000005e-233 < b Initial program 0.1%
Taylor expanded in angle around 0
Applied rewrites18.7%
Taylor expanded in a around 0
lower-*.f6418.7
Applied rewrites18.7%
a_m = (fabs.f64 a) b_m = (fabs.f64 b) x-scale_m = (fabs.f64 x-scale) (FPCore (a_m b_m angle x-scale_m y-scale) :precision binary64 (* a_m x-scale_m))
a_m = fabs(a);
b_m = fabs(b);
x-scale_m = fabs(x_45_scale);
double code(double a_m, double b_m, double angle, double x_45_scale_m, double y_45_scale) {
return a_m * x_45_scale_m;
}
a_m = private
b_m = private
x-scale_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(a_m, b_m, angle, x_45scale_m, y_45scale)
use fmin_fmax_functions
real(8), intent (in) :: a_m
real(8), intent (in) :: b_m
real(8), intent (in) :: angle
real(8), intent (in) :: x_45scale_m
real(8), intent (in) :: y_45scale
code = a_m * x_45scale_m
end function
a_m = Math.abs(a);
b_m = Math.abs(b);
x-scale_m = Math.abs(x_45_scale);
public static double code(double a_m, double b_m, double angle, double x_45_scale_m, double y_45_scale) {
return a_m * x_45_scale_m;
}
a_m = math.fabs(a) b_m = math.fabs(b) x-scale_m = math.fabs(x_45_scale) def code(a_m, b_m, angle, x_45_scale_m, y_45_scale): return a_m * x_45_scale_m
a_m = abs(a) b_m = abs(b) x-scale_m = abs(x_45_scale) function code(a_m, b_m, angle, x_45_scale_m, y_45_scale) return Float64(a_m * x_45_scale_m) end
a_m = abs(a); b_m = abs(b); x-scale_m = abs(x_45_scale); function tmp = code(a_m, b_m, angle, x_45_scale_m, y_45_scale) tmp = a_m * x_45_scale_m; end
a_m = N[Abs[a], $MachinePrecision] b_m = N[Abs[b], $MachinePrecision] x-scale_m = N[Abs[x$45$scale], $MachinePrecision] code[a$95$m_, b$95$m_, angle_, x$45$scale$95$m_, y$45$scale_] := N[(a$95$m * x$45$scale$95$m), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|
\\
x-scale_m = \left|x-scale\right|
\\
a\_m \cdot x-scale\_m
\end{array}
Initial program 0.1%
Taylor expanded in angle around 0
Applied rewrites20.2%
Taylor expanded in a around 0
lower-*.f6420.2
Applied rewrites20.2%
herbie shell --seed 2025054
(FPCore (a b angle x-scale y-scale)
:name "b from scale-rotated-ellipse"
:precision binary64
(/ (- (sqrt (* (* (* 2.0 (/ (* 4.0 (* (* b a) (* b (- a)))) (pow (* x-scale y-scale) 2.0))) (* (* b a) (* b (- a)))) (- (+ (/ (/ (+ (pow (* a (sin (* (/ angle 180.0) (PI)))) 2.0) (pow (* b (cos (* (/ angle 180.0) (PI)))) 2.0)) x-scale) x-scale) (/ (/ (+ (pow (* a (cos (* (/ angle 180.0) (PI)))) 2.0) (pow (* b (sin (* (/ angle 180.0) (PI)))) 2.0)) y-scale) y-scale)) (sqrt (+ (pow (- (/ (/ (+ (pow (* a (sin (* (/ angle 180.0) (PI)))) 2.0) (pow (* b (cos (* (/ angle 180.0) (PI)))) 2.0)) x-scale) x-scale) (/ (/ (+ (pow (* a (cos (* (/ angle 180.0) (PI)))) 2.0) (pow (* b (sin (* (/ angle 180.0) (PI)))) 2.0)) y-scale) y-scale)) 2.0) (pow (/ (/ (* (* (* 2.0 (- (pow b 2.0) (pow a 2.0))) (sin (* (/ angle 180.0) (PI)))) (cos (* (/ angle 180.0) (PI)))) x-scale) y-scale) 2.0))))))) (/ (* 4.0 (* (* b a) (* b (- a)))) (pow (* x-scale y-scale) 2.0))))