From 81e0b408fa8e7e45eaf70e19ab0b28a36959fe8a Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 27 Mar 2025 11:47:55 +0100 Subject: [PATCH] App: Use std::numeric_limits and std::numbers instead of defines --- src/App/Datums.cpp | 12 +++++----- src/App/Expression.cpp | 41 +++++++++++++--------------------- src/App/ExpressionParser.l | 8 +++---- src/App/Link.cpp | 3 ++- src/App/ObjectIdentifier.cpp | 17 ++++++++------ src/App/ObjectIdentifier.h | 22 +++++++++++------- src/App/PreCompiled.h | 2 +- src/App/PropertyStandard.cpp | 5 +++-- src/App/PropertyUnits.cpp | 4 ++-- src/App/lex.ExpressionParser.c | 4 ++-- 10 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/App/Datums.cpp b/src/App/Datums.cpp index f595277856..0d315ed859 100644 --- a/src/App/Datums.cpp +++ b/src/App/Datums.cpp @@ -33,10 +33,6 @@ #include "Datums.h" -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - using namespace App; PROPERTY_SOURCE(App::DatumElement, App::GeoFeature) @@ -243,14 +239,16 @@ App::DocumentObjectExecReturn* LocalCoordinateSystem::execute() const std::vector& LocalCoordinateSystem::getSetupData() { + using std::numbers::pi; + static const std::vector setupData = { // clang-format off {App::Line::getClassTypeId(), AxisRoles[0], tr("X-axis"), Base::Rotation()}, - {App::Line::getClassTypeId(), AxisRoles[1], tr("Y-axis"), Base::Rotation(Base::Vector3d(1, 1, 1), M_PI * 2 / 3)}, - {App::Line::getClassTypeId(), AxisRoles[2], tr("Z-axis"), Base::Rotation(Base::Vector3d(1,-1, 1), M_PI * 2 / 3)}, + {App::Line::getClassTypeId(), AxisRoles[1], tr("Y-axis"), Base::Rotation(Base::Vector3d(1, 1, 1), pi * 2 / 3)}, + {App::Line::getClassTypeId(), AxisRoles[2], tr("Z-axis"), Base::Rotation(Base::Vector3d(1,-1, 1), pi * 2 / 3)}, {App::Plane::getClassTypeId(), PlaneRoles[0], tr("XY-plane"), Base::Rotation()}, {App::Plane::getClassTypeId(), PlaneRoles[1], tr("XZ-plane"), Base::Rotation(1.0, 0.0, 0.0, 1.0)}, - {App::Plane::getClassTypeId(), PlaneRoles[2], tr("YZ-plane"), Base::Rotation(Base::Vector3d(1, 1, 1), M_PI * 2 / 3)}, + {App::Plane::getClassTypeId(), PlaneRoles[2], tr("YZ-plane"), Base::Rotation(Base::Vector3d(1, 1, 1), pi * 2 / 3)}, {App::Point::getClassTypeId(), PointRoles[0], tr("Origin"), Base::Rotation()} // clang-format on }; diff --git a/src/App/Expression.cpp b/src/App/Expression.cpp index 115db3dd9b..1baebc48bd 100644 --- a/src/App/Expression.cpp +++ b/src/App/Expression.cpp @@ -34,6 +34,8 @@ #include #include +#include +#include #include #include #include @@ -62,19 +64,6 @@ using namespace App; FC_LOG_LEVEL_INIT("Expression", true, true) -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif -#ifndef M_E -#define M_E 2.71828182845904523536 -#endif -#ifndef DOUBLE_MAX -# define DOUBLE_MAX 1.7976931348623157E+308 /* max decimal value of a "double"*/ -#endif -#ifndef DOUBLE_MIN -# define DOUBLE_MIN 2.2250738585072014E-308 /* min decimal value of a "double"*/ -#endif - #if defined(_MSC_VER) #define strtoll _strtoi64 #pragma warning(disable : 4003) @@ -336,22 +325,22 @@ static inline int essentiallyInteger(double a, long &l, int &i) { double intpart; if (std::modf(a,&intpart) == 0.0) { if (intpart<0.0) { - if (intpart >= INT_MIN) { + if (intpart >= std::numeric_limits::min()) { i = static_cast(intpart); l = i; return 1; } - if (intpart >= LONG_MIN) { + if (intpart >= std::numeric_limits::min()) { l = static_cast(intpart); return 2; } } - else if (intpart <= INT_MAX) { + else if (intpart <= std::numeric_limits::max()) { i = static_cast(intpart); l = i; return 1; } - else if (intpart <= static_cast(LONG_MAX)) { + else if (intpart <= static_cast(std::numeric_limits::max())) { l = static_cast(intpart); return 2; } @@ -363,12 +352,12 @@ static inline bool essentiallyInteger(double a, long &l) { double intpart; if (std::modf(a,&intpart) == 0.0) { if (intpart<0.0) { - if (intpart >= LONG_MIN) { + if (intpart >= std::numeric_limits::min()) { l = static_cast(intpart); return true; } } - else if (intpart <= static_cast(LONG_MAX)) { + else if (intpart <= static_cast(std::numeric_limits::max())) { l = static_cast(intpart); return true; } @@ -2150,6 +2139,8 @@ Base::Vector3d FunctionExpression::extractVectorArgument( Py::Object FunctionExpression::evaluate(const Expression *expr, int f, const std::vector &args) { + using std::numbers::pi; + if(!expr || !expr->getOwner()) _EXPR_THROW("Invalid owner.", expr); @@ -2186,7 +2177,7 @@ Py::Object FunctionExpression::evaluate(const Expression *expr, int f, const std Py::Object pyobj = args[0]->getPyValue(); if (PyObject_TypeCheck(pyobj.ptr(), &Base::MatrixPy::Type)) { auto m = static_cast(pyobj.ptr())->value(); - if (fabs(m.determinant()) <= DBL_EPSILON) + if (fabs(m.determinant()) <= std::numeric_limits::epsilon()) _EXPR_THROW("Cannot invert singular matrix.", expr); m.inverseGauss(); return Py::asObject(new Base::MatrixPy(m)); @@ -2227,7 +2218,7 @@ Py::Object FunctionExpression::evaluate(const Expression *expr, int f, const std Rotation rotation = Base::Rotation( Vector3d(static_cast(f == MROTATEX), static_cast(f == MROTATEY), static_cast(f == MROTATEZ)), - rotationAngle.getValue() * M_PI / 180.0); + rotationAngle.getValue() * pi / 180.0); Base::Matrix4D rotationMatrix; rotation.getValue(rotationMatrix); @@ -2366,7 +2357,7 @@ Py::Object FunctionExpression::evaluate(const Expression *expr, int f, const std switch (f) { case VANGLE: - return Py::asObject(new QuantityPy(new Quantity(vector1.GetAngle(vector2) * 180 / M_PI, Unit::Angle))); + return Py::asObject(new QuantityPy(new Quantity(vector1.GetAngle(vector2) * 180 / pi, Unit::Angle))); case VCROSS: return Py::asObject(new Base::VectorPy(vector1.Cross(vector2))); case VDOT: @@ -2425,7 +2416,7 @@ Py::Object FunctionExpression::evaluate(const Expression *expr, int f, const std _EXPR_THROW("Unit must be either empty or an angle.", expr); // Convert value to radians - value *= M_PI / 180.0; + value *= pi / 180.0; unit = Unit(); break; case ACOS: @@ -2434,7 +2425,7 @@ Py::Object FunctionExpression::evaluate(const Expression *expr, int f, const std if (!v1.isDimensionless()) _EXPR_THROW("Unit must be empty.", expr); unit = Unit::Angle; - scaler = 180.0 / M_PI; + scaler = 180.0 / pi; break; case EXP: case LOG: @@ -2466,7 +2457,7 @@ Py::Object FunctionExpression::evaluate(const Expression *expr, int f, const std if (v1.getUnit() != v2.getUnit()) _EXPR_THROW("Units must be equal.",expr); unit = Unit::Angle; - scaler = 180.0 / M_PI; + scaler = 180.0 / pi; break; case MOD: if (e2.isNone()) diff --git a/src/App/ExpressionParser.l b/src/App/ExpressionParser.l index 49e6e3672c..bed2fc5f33 100644 --- a/src/App/ExpressionParser.l +++ b/src/App/ExpressionParser.l @@ -341,15 +341,15 @@ EXPO [eE][-+]?[0-9]+ {DIGIT}+{EXPO} COUNTCHARS; yylval.fvalue = num_change(yytext,',','.'); return yylval.fvalue == 1 ? ONE : NUM; {DIGIT}+ { COUNTCHARS; yylval.ivalue = strtoll( yytext, NULL, 10 ); - if (yylval.ivalue == LLONG_MIN) + if (yylval.ivalue == std::numeric_limits::min) throw Base::UnderflowError("Integer underflow"); - else if (yylval.ivalue == LLONG_MAX) + else if (yylval.ivalue == std::numeric_limits::max) throw Base::OverflowError("Integer overflow"); if (yylval.ivalue == 1) { yylval.fvalue = 1; return ONE; } else return INTEGER; } -"pi" COUNTCHARS; yylval.constant.fvalue = M_PI; yylval.constant.name = "pi"; return CONSTANT; // constant pi -"e" COUNTCHARS; yylval.constant.fvalue = M_E; yylval.constant.name = "e"; return CONSTANT; // constant e +"pi" COUNTCHARS; yylval.constant.fvalue = std::numbers::pi; yylval.constant.name = "pi"; return CONSTANT; // constant pi +"e" COUNTCHARS; yylval.constant.fvalue = std::numbers::e; yylval.constant.name = "e"; return CONSTANT; // constant e "None" COUNTCHARS; yylval.constant.fvalue = 0; yylval.constant.name = "None"; return CONSTANT; "True" COUNTCHARS; yylval.constant.fvalue = 1; yylval.constant.name = "True"; return CONSTANT; diff --git a/src/App/Link.cpp b/src/App/Link.cpp index 2762040d42..9312aa13d2 100644 --- a/src/App/Link.cpp +++ b/src/App/Link.cpp @@ -2624,7 +2624,8 @@ Link::Link() { LINK_PROPS_ADD(LINK_PARAMS_LINK); LinkExtension::initExtension(this); - static const PropertyIntegerConstraint::Constraints s_constraints = {0, INT_MAX, 1}; + static const PropertyIntegerConstraint::Constraints s_constraints = { + 0, std::numeric_limits::max(), 1}; ElementCount.setConstraints(&s_constraints); } diff --git a/src/App/ObjectIdentifier.cpp b/src/App/ObjectIdentifier.cpp index 28637d638a..0c80b13672 100644 --- a/src/App/ObjectIdentifier.cpp +++ b/src/App/ObjectIdentifier.cpp @@ -130,7 +130,7 @@ ObjectIdentifier::ObjectIdentifier(const App::PropertyContainer* _owner, } if (!property.empty()) { addComponent(SimpleComponent(property)); - if (index != INT_MAX) { + if (index != std::numeric_limits::max()) { addComponent(ArrayComponent(index)); } } @@ -179,7 +179,7 @@ ObjectIdentifier::ObjectIdentifier(const Property& prop, int index) setDocumentObjectName(docObj); addComponent(SimpleComponent(String(prop.getName()))); - if (index != INT_MAX) { + if (index != std::numeric_limits::max()) { addComponent(ArrayComponent(index)); } } @@ -703,8 +703,9 @@ Py::Object ObjectIdentifier::Component::get(const Py::Object& pyobj) const } else { assert(isRange()); + constexpr int max = std::numeric_limits::max(); Py::Object slice(PySlice_New(Py::Long(begin).ptr(), - end != INT_MAX ? Py::Long(end).ptr() : nullptr, + end != max ? Py::Long(end).ptr() : nullptr, step != 1 ? Py::Long(step).ptr() : nullptr), true); PyObject* r = PyObject_GetItem(pyobj.ptr(), slice.ptr()); @@ -742,8 +743,9 @@ void ObjectIdentifier::Component::set(Py::Object& pyobj, const Py::Object& value } else { assert(isRange()); + constexpr int max = std::numeric_limits::max(); Py::Object slice(PySlice_New(Py::Long(begin).ptr(), - end != INT_MAX ? Py::Long(end).ptr() : nullptr, + end != max ? Py::Long(end).ptr() : nullptr, step != 1 ? Py::Long(step).ptr() : nullptr), true); if (PyObject_SetItem(pyobj.ptr(), slice.ptr(), value.ptr()) < 0) { @@ -770,8 +772,9 @@ void ObjectIdentifier::Component::del(Py::Object& pyobj) const } else { assert(isRange()); + constexpr int max = std::numeric_limits::max(); Py::Object slice(PySlice_New(Py::Long(begin).ptr(), - end != INT_MAX ? Py::Long(end).ptr() : nullptr, + end != max ? Py::Long(end).ptr() : nullptr, step != 1 ? Py::Long(step).ptr() : nullptr), true); if (PyObject_DelItem(pyobj.ptr(), slice.ptr()) < 0) { @@ -897,11 +900,11 @@ void ObjectIdentifier::Component::toString(std::ostream& ss, bool toPython) cons break; case Component::RANGE: ss << '['; - if (begin != INT_MAX) { + if (begin != std::numeric_limits::max()) { ss << begin; } ss << ':'; - if (end != INT_MAX) { + if (end != std::numeric_limits::max()) { ss << end; } if (step != 1) { diff --git a/src/App/ObjectIdentifier.h b/src/App/ObjectIdentifier.h index c62f69c47c..60cb5e19c6 100644 --- a/src/App/ObjectIdentifier.h +++ b/src/App/ObjectIdentifier.h @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -211,13 +212,13 @@ public: Component(const String& _name = String(), typeEnum _type = SIMPLE, - int begin = INT_MAX, - int end = INT_MAX, + int begin = std::numeric_limits::max(), + int end = std::numeric_limits::max(), int step = 1); // explicit bombs Component(String&& _name, typeEnum _type = SIMPLE, - int begin = INT_MAX, - int end = INT_MAX, + int begin = std::numeric_limits::max(), + int end = std::numeric_limits::max(), int step = 1); // explicit bombs static Component SimpleComponent(const char* _component); @@ -227,7 +228,9 @@ public: static Component ArrayComponent(int _index); - static Component RangeComponent(int _begin, int _end = INT_MAX, int _step = 1); + static Component RangeComponent(int _begin, + int _end = std::numeric_limits::max(), + int _step = 1); static Component MapComponent(const String& _key); static Component MapComponent(String&& _key); @@ -325,7 +328,9 @@ public: return Component::ArrayComponent(_index); } - static Component RangeComponent(int _begin, int _end = INT_MAX, int _step = 1) + static Component RangeComponent(int _begin, + int _end = std::numeric_limits::max(), + int _step = 1) { return Component::RangeComponent(_begin, _end, _step); } @@ -342,11 +347,12 @@ public: explicit ObjectIdentifier(const App::PropertyContainer* _owner = nullptr, const std::string& property = std::string(), - int index = INT_MAX); + int index = std::numeric_limits::max()); ObjectIdentifier(const App::PropertyContainer* _owner, bool localProperty); - ObjectIdentifier(const App::Property& prop, int index = INT_MAX); // explicit bombs + ObjectIdentifier(const App::Property& prop, + int index = std::numeric_limits::max()); // explicit bombs FC_DEFAULT_CTORS(ObjectIdentifier) { diff --git a/src/App/PreCompiled.h b/src/App/PreCompiled.h index 38d675970a..bd32e3058f 100644 --- a/src/App/PreCompiled.h +++ b/src/App/PreCompiled.h @@ -50,7 +50,6 @@ #include #include #include -#include #ifdef FC_OS_WIN32 #include @@ -74,6 +73,7 @@ #include #include #include +#include #include #include #include diff --git a/src/App/PropertyStandard.cpp b/src/App/PropertyStandard.cpp index 77f12cd513..4ef6b2e335 100644 --- a/src/App/PropertyStandard.cpp +++ b/src/App/PropertyStandard.cpp @@ -1250,7 +1250,7 @@ void PropertyFloatConstraint::setPyObject(PyObject* value) double stepSize = valConstr[3]; // need a value > 0 - if (stepSize < DBL_EPSILON) { + if (stepSize < std::numeric_limits::epsilon()) { throw Base::ValueError("Step size must be greater than zero"); } @@ -1282,7 +1282,8 @@ TYPESYSTEM_SOURCE(App::PropertyPrecision, App::PropertyFloatConstraint) //************************************************************************** // Construction/Destruction // -const PropertyFloatConstraint::Constraints PrecisionStandard = {0.0, DBL_MAX, 0.001}; +const PropertyFloatConstraint::Constraints PrecisionStandard = { + 0.0, std::numeric_limits::max(), 0.001}; PropertyPrecision::PropertyPrecision() { diff --git a/src/App/PropertyUnits.cpp b/src/App/PropertyUnits.cpp index f2c30c6dce..153b64bfa2 100644 --- a/src/App/PropertyUnits.cpp +++ b/src/App/PropertyUnits.cpp @@ -22,7 +22,6 @@ #include "PreCompiled.h" #ifndef _PreComp_ -#include #endif #include @@ -37,7 +36,8 @@ using namespace Base; using namespace std; -const PropertyQuantityConstraint::Constraints LengthStandard = {0.0, DBL_MAX, 1.0}; +const PropertyQuantityConstraint::Constraints LengthStandard = { + 0.0, std::numeric_limits::max(), 1.0}; const PropertyQuantityConstraint::Constraints AngleStandard = {-360, 360, 1.0}; //************************************************************************** diff --git a/src/App/lex.ExpressionParser.c b/src/App/lex.ExpressionParser.c index 2916336b02..cc9777e092 100644 --- a/src/App/lex.ExpressionParser.c +++ b/src/App/lex.ExpressionParser.c @@ -9583,12 +9583,12 @@ YY_RULE_SETUP case 138: YY_RULE_SETUP #line 351 "ExpressionParser.l" -COUNTCHARS; yylval.constant.fvalue = M_PI; yylval.constant.name = "pi"; return CONSTANT; // constant pi +COUNTCHARS; yylval.constant.fvalue = std::numbers::pi; yylval.constant.name = "pi"; return CONSTANT; // constant pi YY_BREAK case 139: YY_RULE_SETUP #line 352 "ExpressionParser.l" -COUNTCHARS; yylval.constant.fvalue = M_E; yylval.constant.name = "e"; return CONSTANT; // constant e +COUNTCHARS; yylval.constant.fvalue = std::numbers::e; yylval.constant.name = "e"; return CONSTANT; // constant e YY_BREAK case 140: YY_RULE_SETUP