fixed: added in the parser: operator's associativity

operator ^ (powering) is right-associative:
        sample: 2^3^4 is equal 2^(3^4) and it is: 2.41e+24
        previously was: 2^3^4 = (2^3)^4 = 4096



git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@273 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2009-12-25 22:06:49 +00:00
parent 31563ce343
commit 0ada20b4cb
3 changed files with 35 additions and 18 deletions

View File

@@ -1575,7 +1575,7 @@ public:
if( pow.Mod2() )
c += result.Mul(start);
c += pow.exponent.Sub( e_one );
c += pow.exponent.Sub( e_one ); // !! may use SubOne() here?
if( pow < one )
break;

View File

@@ -159,14 +159,20 @@ private:
none,add,sub,mul,div,pow,lt,gt,let,get,eq,neq,lor,land,shortmul
};
enum Assoc
{
right, // right-associative
non_right // associative or left-associative
};
Type GetType() const { return type; }
int GetPriority() const { return priority; }
Type GetType() const { return type; }
int GetPriority() const { return priority; }
Assoc GetAssoc() const { return assoc; }
void SetType(Type t)
{
type = t;
type = t;
assoc = non_right;
switch( type )
{
@@ -200,6 +206,7 @@ private:
case pow:
priority = 14;
assoc = right;
break;
default:
@@ -208,15 +215,15 @@ private:
}
}
MatOperator(): type(none), priority(0)
MatOperator(): type(none), priority(0), assoc(non_right)
{
}
private:
Type type;
int priority;
Type type;
int priority;
Assoc assoc;
}; // end of MatOperator class
@@ -2207,10 +2214,20 @@ void TryRollingUpStackWithOperatorPriority()
{
while( stack_index>=4 &&
stack[stack_index-4].type == Item::numerical_value &&
stack[stack_index-3].type == Item::mat_operator &&
stack[stack_index-3].type == Item::mat_operator &&
stack[stack_index-2].type == Item::numerical_value &&
stack[stack_index-1].type == Item::mat_operator &&
stack[stack_index-3].moperator.GetPriority() >= stack[stack_index-1].moperator.GetPriority()
stack[stack_index-1].type == Item::mat_operator &&
(
(
// the first operator has greater priority
stack[stack_index-3].moperator.GetPriority() > stack[stack_index-1].moperator.GetPriority()
) ||
(
// or both operators have the same priority and the first operator is not right associative
stack[stack_index-3].moperator.GetPriority() == stack[stack_index-1].moperator.GetPriority() &&
stack[stack_index-3].moperator.GetAssoc() == MatOperator::non_right
)
)
)
{
MakeStandardMathematicOperation(stack[stack_index-4].value,