mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-25 16:50:50 +01:00
[poincare] Number operations are by Reference to avoid useless copy
This commit is contained in:
@@ -39,9 +39,9 @@ public:
|
||||
* account as it is not an internal node - it will always be turned into a
|
||||
* Rational/Float beforehand. */
|
||||
// TODO: Use references for Addition / Multiplication / Power
|
||||
static Number Addition(const Number i, const Number j);
|
||||
static Number Multiplication(const Number i, const Number j);
|
||||
static Number Power(const Number i, const Number j);
|
||||
static Number Addition(const Number & i, const Number & j);
|
||||
static Number Multiplication(const Number & i, const Number & j);
|
||||
static Number Power(const Number & i, const Number & j);
|
||||
protected:
|
||||
Number() : Expression() {}
|
||||
NumberNode * node() const override { return static_cast<NumberNode *>(Expression::node()); }
|
||||
@@ -49,7 +49,7 @@ private:
|
||||
typedef Integer (*IntegerBinaryOperation)(const Integer & i, const Integer & j);
|
||||
typedef Rational (*RationalBinaryOperation)(const Rational & i, const Rational & j);
|
||||
typedef double (*DoubleBinaryOperation)(double i, double j);
|
||||
static Number BinaryOperation(const Number i, const Number j, IntegerBinaryOperation integerOp, RationalBinaryOperation rationalOp, DoubleBinaryOperation doubleOp);
|
||||
static Number BinaryOperation(const Number & i, const Number & j, IntegerBinaryOperation integerOp, RationalBinaryOperation rationalOp, DoubleBinaryOperation doubleOp);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ Number Number::FloatNumber(double d) {
|
||||
}
|
||||
}
|
||||
|
||||
Number Number::BinaryOperation(const Number i, const Number j, IntegerBinaryOperation integerOp, RationalBinaryOperation rationalOp, DoubleBinaryOperation doubleOp) {
|
||||
Number Number::BinaryOperation(const Number & i, const Number & j, IntegerBinaryOperation integerOp, RationalBinaryOperation rationalOp, DoubleBinaryOperation doubleOp) {
|
||||
if (i.node()->type() == ExpressionNode::Type::Integer && j.node()->type() == ExpressionNode::Type::Integer) {
|
||||
// Integer + Integer
|
||||
Integer k = integerOp(Integer(static_cast<IntegerNode *>(i.node())), Integer(static_cast<IntegerNode *>(j.node())));
|
||||
@@ -102,15 +102,15 @@ Number Number::BinaryOperation(const Number i, const Number j, IntegerBinaryOper
|
||||
return FloatNumber(a);
|
||||
}
|
||||
|
||||
Number Number::Addition(const Number i, const Number j) {
|
||||
Number Number::Addition(const Number & i, const Number & j) {
|
||||
return BinaryOperation(i, j, Integer::Addition, Rational::Addition, [](double a, double b) { return a+b; });
|
||||
}
|
||||
|
||||
Number Number::Multiplication(const Number i, const Number j) {
|
||||
Number Number::Multiplication(const Number & i, const Number & j) {
|
||||
return BinaryOperation(i, j, Integer::Multiplication, Rational::Multiplication, [](double a, double b) { return a*b; });
|
||||
}
|
||||
|
||||
Number Number::Power(const Number i, const Number j) {
|
||||
Number Number::Power(const Number & i, const Number & j) {
|
||||
return BinaryOperation(i, j, Integer::Power,
|
||||
// Special case for Rational^Rational: we escape to Float if the index is not an Integer
|
||||
[](const Rational & i, const Rational & j) {
|
||||
|
||||
Reference in New Issue
Block a user