(FPCore (a b c)
:precision binary64
(if (>= b 0.0)
(/ (- (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a))
(/ (* 2.0 c) (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))))))
↓
(FPCore (a b c)
:precision binary64
(let* ((t_0 (sqrt (- (* b b) (* c (* 4.0 a))))))
(if (<= b -4e+137)
(if (>= b 0.0) (/ c b) (/ (- c) b))
(if (<= b 1.25e+70)
(if (>= b 0.0) (/ (- (- b) t_0) (* a 2.0)) (/ (* c 2.0) (- t_0 b)))
(if (>= b 0.0)
(/ (- b) a)
(* c (/ -2.0 (+ (* -2.0 (/ (* c a) b)) (* b 2.0)))))))))
double code(double a, double b, double c) {
double tmp;
if (b >= 0.0) {
tmp = (-b - sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
} else {
tmp = (2.0 * c) / (-b + sqrt(((b * b) - ((4.0 * a) * c))));
}
return tmp;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: tmp
if (b >= 0.0d0) then
tmp = (-b - sqrt(((b * b) - ((4.0d0 * a) * c)))) / (2.0d0 * a)
else
tmp = (2.0d0 * c) / (-b + sqrt(((b * b) - ((4.0d0 * a) * c))))
end if
code = tmp
end function
↓
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: t_0
real(8) :: tmp
real(8) :: tmp_1
real(8) :: tmp_2
real(8) :: tmp_3
t_0 = sqrt(((b * b) - (c * (4.0d0 * a))))
if (b <= (-4d+137)) then
if (b >= 0.0d0) then
tmp_2 = c / b
else
tmp_2 = -c / b
end if
tmp_1 = tmp_2
else if (b <= 1.25d+70) then
if (b >= 0.0d0) then
tmp_3 = (-b - t_0) / (a * 2.0d0)
else
tmp_3 = (c * 2.0d0) / (t_0 - b)
end if
tmp_1 = tmp_3
else if (b >= 0.0d0) then
tmp_1 = -b / a
else
tmp_1 = c * ((-2.0d0) / (((-2.0d0) * ((c * a) / b)) + (b * 2.0d0)))
end if
code = tmp_1
end function
public static double code(double a, double b, double c) {
double tmp;
if (b >= 0.0) {
tmp = (-b - Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
} else {
tmp = (2.0 * c) / (-b + Math.sqrt(((b * b) - ((4.0 * a) * c))));
}
return tmp;
}
↓
public static double code(double a, double b, double c) {
double t_0 = Math.sqrt(((b * b) - (c * (4.0 * a))));
double tmp_1;
if (b <= -4e+137) {
double tmp_2;
if (b >= 0.0) {
tmp_2 = c / b;
} else {
tmp_2 = -c / b;
}
tmp_1 = tmp_2;
} else if (b <= 1.25e+70) {
double tmp_3;
if (b >= 0.0) {
tmp_3 = (-b - t_0) / (a * 2.0);
} else {
tmp_3 = (c * 2.0) / (t_0 - b);
}
tmp_1 = tmp_3;
} else if (b >= 0.0) {
tmp_1 = -b / a;
} else {
tmp_1 = c * (-2.0 / ((-2.0 * ((c * a) / b)) + (b * 2.0)));
}
return tmp_1;
}
def code(a, b, c):
tmp = 0
if b >= 0.0:
tmp = (-b - math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
else:
tmp = (2.0 * c) / (-b + math.sqrt(((b * b) - ((4.0 * a) * c))))
return tmp
↓
def code(a, b, c):
t_0 = math.sqrt(((b * b) - (c * (4.0 * a))))
tmp_1 = 0
if b <= -4e+137:
tmp_2 = 0
if b >= 0.0:
tmp_2 = c / b
else:
tmp_2 = -c / b
tmp_1 = tmp_2
elif b <= 1.25e+70:
tmp_3 = 0
if b >= 0.0:
tmp_3 = (-b - t_0) / (a * 2.0)
else:
tmp_3 = (c * 2.0) / (t_0 - b)
tmp_1 = tmp_3
elif b >= 0.0:
tmp_1 = -b / a
else:
tmp_1 = c * (-2.0 / ((-2.0 * ((c * a) / b)) + (b * 2.0)))
return tmp_1
function code(a, b, c)
tmp = 0.0
if (b >= 0.0)
tmp = Float64(Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a));
else
tmp = Float64(Float64(2.0 * c) / Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))));
end
return tmp
end
↓
function code(a, b, c)
t_0 = sqrt(Float64(Float64(b * b) - Float64(c * Float64(4.0 * a))))
tmp_1 = 0.0
if (b <= -4e+137)
tmp_2 = 0.0
if (b >= 0.0)
tmp_2 = Float64(c / b);
else
tmp_2 = Float64(Float64(-c) / b);
end
tmp_1 = tmp_2;
elseif (b <= 1.25e+70)
tmp_3 = 0.0
if (b >= 0.0)
tmp_3 = Float64(Float64(Float64(-b) - t_0) / Float64(a * 2.0));
else
tmp_3 = Float64(Float64(c * 2.0) / Float64(t_0 - b));
end
tmp_1 = tmp_3;
elseif (b >= 0.0)
tmp_1 = Float64(Float64(-b) / a);
else
tmp_1 = Float64(c * Float64(-2.0 / Float64(Float64(-2.0 * Float64(Float64(c * a) / b)) + Float64(b * 2.0))));
end
return tmp_1
end
function tmp_2 = code(a, b, c)
tmp = 0.0;
if (b >= 0.0)
tmp = (-b - sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
else
tmp = (2.0 * c) / (-b + sqrt(((b * b) - ((4.0 * a) * c))));
end
tmp_2 = tmp;
end
↓
function tmp_5 = code(a, b, c)
t_0 = sqrt(((b * b) - (c * (4.0 * a))));
tmp_2 = 0.0;
if (b <= -4e+137)
tmp_3 = 0.0;
if (b >= 0.0)
tmp_3 = c / b;
else
tmp_3 = -c / b;
end
tmp_2 = tmp_3;
elseif (b <= 1.25e+70)
tmp_4 = 0.0;
if (b >= 0.0)
tmp_4 = (-b - t_0) / (a * 2.0);
else
tmp_4 = (c * 2.0) / (t_0 - b);
end
tmp_2 = tmp_4;
elseif (b >= 0.0)
tmp_2 = -b / a;
else
tmp_2 = c * (-2.0 / ((-2.0 * ((c * a) / b)) + (b * 2.0)));
end
tmp_5 = tmp_2;
end
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.
Herbie found 8 alternatives:
Alternative
Accuracy
Speedup
Accuracy vs Speed
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.
herbie shell --seed 2023256
(FPCore (a b c)
:name "jeff quadratic root 1"
:precision binary64
(if (>= b 0.0) (/ (- (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)) (/ (* 2.0 c) (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))))))