commit 47fdc34ed6e6e8c89df06c92418bdd76800a0b3f
parent f29287247f3f353c713d963670a540ebdd7ef30b
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 9 Jan 2017 10:01:48 +0100
Fix a GCC warning & refactor the quadric_solve_second function
Diffstat:
1 file changed, 21 insertions(+), 31 deletions(-)
diff --git a/src/ssol_shape.c b/src/ssol_shape.c
@@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#define POSIX_C_SOURCE 200112L
+#define _POSIX_C_SOURCE 200112L /* copysign support */
#include "ssol.h"
#include "ssol_c.h"
@@ -34,6 +34,7 @@
#include <star/scpr.h>
#include <limits.h> /* UINT_MAX constant */
+#include <math.h> /* copysign function */
struct mesh_context {
const double* coords;
@@ -538,10 +539,9 @@ error:
}
goto exit;
}
-
-/* Solve a 2nd degree equation
- * hint is used to select among the 2 solutions (if applies)
- * the selected solution is then the closest to hint */
+
+/* Solve a 2nd degree equation. "hint" is used to select among the 2 solutions
+ * (if applies) the selected solution is then the closest to hint */
static int
quadric_solve_second
(const double a,
@@ -550,37 +550,27 @@ quadric_solve_second
const double hint,
double* dist)
{
- ASSERT(dist);
- ASSERT(hint >= 0);
- if (a != 0) {
- /* Standard case: 2nd degree */
- const double delta = b * b - 4 * a * c;
- if (delta > 0) {
+ double t = -1;
+ ASSERT(dist && hint >= 0);
+
+ if(a == 0) {
+ if(b != 0) t = -c / b; /* Degenerated case: 1st degree only */
+ } else { /* Standard case: 2nd degree */
+ const double delta = b*b - 4*a*c;
+
+ if(delta == 0) {
+ t = -b / (2*a);
+ } else {
const double sqrt_delta = sqrt(delta);
/* Precise formula */
- const double t1 = (-b - copysign(sqrt_delta, b)) / (2 * a);
- const double t2 = c / (a * t1);
+ const double t1 = (-b - copysign(sqrt_delta, b)) / (2*a);
+ const double t2 = c / (a*t1);
/* Choose the closest value to hint */
- *dist = fabs(t1 - hint) < fabs(t2 - hint) ? t1 : t2;
- return 1;
- }
- else if (delta == 0) {
- const double t = -b / (2 * a);
- *dist = t;
- return 1;
+ t = fabs(t1 - hint) < fabs(t2 - hint) ? t1 : t2;
}
- else {
- return 0;
- }
- }
- else if (b != 0) {
- /* degenerated case: 1st degree only */
- const double t = -c / b;
- *dist = t;
- return 1;
}
- /* fully degenerated case: cannot determine dist */
- return 0;
+ *dist = t;
+ return t >= 0;
}
static FINLINE void