Skip to content

Commit

Permalink
+ support comparison with double in quantity number protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jul 14, 2014
1 parent 1fd7bd9 commit 8aec46f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/App/Application.cpp
Expand Up @@ -237,6 +237,8 @@ Application::Application(ParameterManager * /*pcSysParamMngr*/,
PyObject* pUnitsModule = Py_InitModule3("Units", Base::UnitsApi::Methods,
"The Unit API");
Base::Interpreter().addType(&Base::QuantityPy ::Type,pUnitsModule,"Quantity");
// make sure to set the 'nb_true_divide' slot
Base::QuantityPy::Type.tp_as_number->nb_true_divide = Base::QuantityPy::Type.tp_as_number->nb_divide;
Base::Interpreter().addType(&Base::UnitPy ::Type,pUnitsModule,"Unit");

Py_INCREF(pUnitsModule);
Expand Down
40 changes: 37 additions & 3 deletions src/Base/QuantityPyImp.cpp
Expand Up @@ -466,13 +466,47 @@ PyObject* QuantityPy::richCompare(PyObject *v, PyObject *w, int op)
Py_INCREF(res);
return res;
}

}

else if (PyNumber_Check(v) && PyNumber_Check(w)) {
// Try to get floating numbers
double u1 = PyFloat_AsDouble(v);
double u2 = PyFloat_AsDouble(w);
PyObject *res=0;
if (op == Py_NE) {
res = (u1 != u2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
else if (op == Py_LT) {
res = (u1 < u2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
else if (op == Py_LE) {
res = (u1 <= u2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
else if (op == Py_GT) {
res = (u1 > u2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
else if (op == Py_GE) {
res = (u1 >= u2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
else if (op == Py_EQ) {
res = (u1 == u2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
}

// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;

}

Py::Float QuantityPy::getValue(void) const
Expand Down

0 comments on commit 8aec46f

Please sign in to comment.