diff --git a/src/confscreen.cpp b/src/confscreen.cpp index 646f91e0..cdf6e518 100644 --- a/src/confscreen.cpp +++ b/src/confscreen.cpp @@ -448,7 +448,7 @@ bool TextWindow::EditControlDoneForConfiguration(const std::string &s) { Expr *e = Expr::From(s, /*popUpError=*/true); if(e) { double ev = e->Eval(); - if(fabs(ev) < 0.001 || isnan(ev)) { + if(fabs(ev) < 0.001 || IsReasonable(ev)) { Error(_("Export scale must not be zero!")); } else { SS.exportScale = (float)ev; @@ -460,7 +460,7 @@ bool TextWindow::EditControlDoneForConfiguration(const std::string &s) { Expr *e = Expr::From(s, /*popUpError=*/true); if(e) { double ev = SS.ExprToMm(e); - if(isnan(ev) || ev < 0) { + if(IsReasonable(ev) || ev < 0) { Error(_("Cutter radius offset must not be negative!")); } else { SS.exportOffset = (float)ev; diff --git a/src/modify.cpp b/src/modify.cpp index 48c10957..94b24787 100644 --- a/src/modify.cpp +++ b/src/modify.cpp @@ -383,7 +383,7 @@ void GraphicsWindow::MakeTangentArc() { if(fabs(tp[0] - t[0]) > 1e-3 || fabs(tp[1] - t[1]) > 1e-3 || t[0] < 0.01 || t[1] < 0.01 || t[0] > 0.99 || t[1] > 0.99 || - isnan(t[0]) || isnan(t[1])) + IsReasonable(t[0]) || IsReasonable(t[1])) { Error(_("Couldn't round this corner. Try a smaller radius, or try " "creating the desired geometry by hand with tangency " diff --git a/src/solvespace.h b/src/solvespace.h index 3a535a5a..165fd086 100644 --- a/src/solvespace.h +++ b/src/solvespace.h @@ -76,10 +76,6 @@ typedef struct _cairo_surface cairo_surface_t; dbp("tri: (%.3f %.3f %.3f) (%.3f %.3f %.3f) (%.3f %.3f %.3f)", \ CO((tri).a), CO((tri).b), CO((tri).c)) -#ifndef isnan -# define isnan(x) (((x) != (x)) || (x > 1e11) || (x < -1e11)) -#endif - namespace SolveSpace { using std::min; @@ -98,6 +94,10 @@ __attribute__((__format__ (__printf__, 1, 2))) #endif std::string ssprintf(const char *fmt, ...); +inline bool IsReasonable(double x) { + return std::isnan(x) || x > 1e11 || x < -1e11; +} + inline int WRAP(int v, int n) { // Clamp it to the range [0, n) while(v >= n) v -= n; diff --git a/src/srf/ratpoly.cpp b/src/srf/ratpoly.cpp index 9e70192a..1ffa3385 100644 --- a/src/srf/ratpoly.cpp +++ b/src/srf/ratpoly.cpp @@ -443,7 +443,7 @@ void SSurface::ClosestPointTo(Vector p, double *u, double *v, bool mustConverge) dbp("want %.3f %.3f %.3f", CO(p)); dbp("distance = %g", (p.Minus(p0)).Magnitude()); } - if(isnan(*u) || isnan(*v)) { + if(IsReasonable(*u) || IsReasonable(*v)) { *u = *v = 0; } } diff --git a/src/system.cpp b/src/system.cpp index 9c64b308..933f153a 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -293,7 +293,7 @@ bool System::NewtonSolve(int tag) { for(i = 0; i < mat.n; i++) { Param *p = param.FindById(mat.param[i]); p->val -= mat.X[i]; - if(isnan(p->val)) { + if(IsReasonable(p->val)) { // Very bad, and clearly not convergent return false; } @@ -306,7 +306,7 @@ bool System::NewtonSolve(int tag) { // Check for convergence converged = true; for(i = 0; i < mat.m; i++) { - if(isnan(mat.B.num[i])) { + if(IsReasonable(mat.B.num[i])) { return false; } if(fabs(mat.B.num[i]) > CONVERGE_TOLERANCE) { @@ -493,7 +493,7 @@ didnt_converge: SK.constraint.ClearTags(); // Not using range-for here because index is used in additional ways for(i = 0; i < eq.n; i++) { - if(fabs(mat.B.num[i]) > CONVERGE_TOLERANCE || isnan(mat.B.num[i])) { + if(fabs(mat.B.num[i]) > CONVERGE_TOLERANCE || IsReasonable(mat.B.num[i])) { // This constraint is unsatisfied. if(!mat.eq[i].isFromConstraint()) continue;