6.6 Operators


6.6.1 Unary Operators

ECMA Script supports the following unary operators: delete , void , typeof , ++ , -- , + , - , ~ , and ! , with the usual interpretation. For example, the unary - operator converts its operand to Number type and then negates it; negating +0 produces “0, and negating “0 produces +0.

6.6.2 Multiplicative Operators

A multiplicative operator is a binary operator whose evaluation requires evaluating both sides of the operator, and then applying the operator on the result.

6.6.2.1 Applying the * Operator

The * operator performs multiplication, producing the product of its operands. Multiplication is commutative. Multiplication is not always associative in ECMA Script, because of finite precision. The result of a floating-point multiplication is governed by the rules of IEEE 754 double-precision arithmetic:

  • If either operand is NaN, the result is NaN.

  • The sign of the result is positive if both operands have the same sign, and negative if the operands have different signs.

6.6.2.2 Multiplication of an Infinity by a Zero Results in NaN.

Multiplication of an infinity by an infinity results in an infinity. The sign is determined by the rule previously stated. Multiplication of an infinity by a finite nonzero value results in a signed infinity. The sign is determined by the rule previously stated.

In the remaining cases, where neither an infinity or NaN is involved, the product is computed and rounded to the nearest representable value using IEEE 754 round-to- nearest mode. If the magnitude is too large to represent, the result is then an infinity of appropriate sign. If the magnitude is too small to represent, the result is then a zero of appropriate sign. The ECMA Script language requires support of gradual underflow as defined by IEEE 754.

6.6.2.3 Applying the / Operator

The / operator performs division, producing the quotient of its operands. The left operand is the dividend and the right operand is the divisor. ECMA Script does not perform integer division. The operands and result of all division operations are double-precision floating-point numbers . The result of division is determined by the specification of IEEE 754 arithmetic:

  • If either operand is NaN, then the result is NaN.

  • The sign of the result is positive if both operands have the same sign, negative if the operands have different signs.

  • Division of an infinity by an infinity results in NaN.

  • Division of an infinity by a zero results in an infinity. The sign is determined by the rule stated.

  • Division of an infinity by a nonzero finite value results in a signed infinity. The sign is determined by the rule stated.

  • Division of a finite value by an infinity results in zero. The sign is determined by the rule stated.

  • Division of a zero by a zero results in NaN; division of zero by any other finite value results in zero, with the sign determined by the rule stated.

  • Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated.

In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the quotient is computed and rounded to the nearest representable value using IEEE 754 round-to-nearest mode. If the magnitude is too large to represent, the operation overflows; the result is then an infinity of appropriate sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of the appropriate sign. The ECMA Script language requires support of gradual underflow as defined by IEEE 754.

6.6.2.4 Applying the % Operator

The % operator yields the remainder of its operands from an implied division; the left operand is the dividend and the right operand is the divisor. In C and C++, the remainder operator accepts only integral operands; in ECMA Script, it also accepts floating-point operands. The result of a floating-point remainder operation as computed by the % operator is not the same as the remainder operation defined by IEEE 754. The IEEE 754 remainder operation computes the remainder from a rounding division, not a truncating division, and its behavior is not analogous to that of the usual integer remainder operator. Instead the ECMA Script language defines % on floating point operations to behave in a manner analogous to that of the Java integer remainder operator; this may be compared with the C library function fmod() . The result of a ECMA Script floating-point remainder operation is determined by the rules of IEEE arithmetic:

  • If either operand is NaN, the result is NaN.

  • The sign of the result equals the sign of the dividend.

  • If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.

  • If the dividend is finite and the divisor is an infinity, the result equals the dividend.

  • If the dividend is a zero and the divisor is finite, the result is the same as the dividend.

In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r from a dividend n and a divisor d is defined by the mathematical relation r = n “ ( d * q ) where q is an integer that is negative only if n / d is negative and positive only if n / d is positive, and whose magnitude is as large as possible without exceeding the magnitude of the true mathematical quotient of n and d .

6.6.3 Additive Operators

Additive operators are similar to multiplicative operators in that their evaluation requires evaluating both sides prior to the application of the operator's semantics. Applying the Additive Operators (+, “) to Numbers is performed as follows . The + operator performs addition when applied to two operands of numeric type, producing the sum of the operands. The “ operator performs subtraction, producing the difference of two numeric operands. Addition is a commutative operation, but not always associative. The result of addition is determined using the rules of IEEE 754 double-precision arithmetic:

  • If either operand is NaN, the result is NaN.

  • The sum of two infinities of opposite sign is NaN.

  • The sum of two infinities of the same sign is the infinity of that sign.

  • The sum of an infinity and a finite value is equal to the infinite operand.

  • The sum of two negative zeros is “0. The sum of two positive zeros, or of two zeros of opposite sign, is +0.

  • The sum of a zero and a nonzero finite value is equal to the nonzero operand.

  • The sum of two nonzero finite values of the same magnitude and opposite sign is +0.

In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, and the operands have the same sign or have different magnitudes , the sum is computed and rounded to the nearest representable value using IEEE 754 round-to-nearest mode. If the magnitude is too large to represent, the operation overflows and the result is an infinity of appropriate sign. The ECMA Script language requires support of gradual underflow as defined by IEEE 754.

The “ operator performs subtraction when applied to two operands of numeric type, producing the difference of its operands; the left operand is the minuend and the right operand is the subtrahend. Given numeric operands a and b, it is always the case that a “b=a+( “b) .

6.6.4 Bitwise Shift Operators

A bitwise shift operator is applied as part of an evaluation of a ShiftExpression , which is an AdditiveExpression followed by either << , >> , or >>> , followed by another AdditiveExpression expression. The evaluation of a ShiftExpression requires evaluating both sides of the operator, and then applying the operator on the result.

The << operator performs a bitwise left shift operation on the left operand by the amount specified by the right operand. The >> operator performs a sign-filling bitwise right shift operation on the left operand by the amount specified by the right operand. The >>> operator performs a zero-filling bitwise right shift operation on the left operand by the amount specified by the right operand.

6.6.5 Relational Operators

A relational operator is applied as part of an evaluation of a RelationalExpression , which is either a ShiftExpression , or a RelationalExpression followed by either < , > , <= , >= , instanceOf , or in , followed by a ShiftExpression . The evaluation of a RelationalExpression requires evaluating both sides of the operator, and then applying the operator on the result. The result of evaluating a relational operator is always of type Boolean , reflecting whether the relationship named by the operator holds between its two operands.

The comparison of strings uses a simple lexicographic ordering on sequences of code point value values. There is no attempt to use the more complex, semantically oriented definitions of character or string equality and collating order defined in the Unicode 2.0 specification. Therefore strings that are canonically equal according to the Unicode standard could test as unequal . In effect this algorithm assumes that both strings are already in normalized form.

6.6.5.1 Equality Operators

An equality operator is applied as part of an evaluation of a EqualityExpression , which is either a RelationalExpression or an EqualityExpression followed by either == , != , === , !== . The evaluation of an EqualityExpression requires evaluating both sides of the operator, and then applying the operator on the result. The result of evaluating an equality operator is always of type Boolean , reflecting whether the relationship named by the operator holds between its two operands.

String comparison can be forced by ""+a==""+b . Numeric comparison can be forced by a-0==b-0 . Boolean comparison can be forced by !a==!b . The equality operators maintain the invariant A!=B is equivalent to !(A==B) and A==B is equivalent to B==A , except in the order of evaluation of A and B . The equality operator is not always transitive. For example, there might be two distinct String objects, each representing the same string value; each String object would be considered equal to the string value by the == operator, but the two String objects would not be equal to each other.

Comparison of strings uses a simple equality test on sequences of bytes with the first in the sequence being the most significant. There is no attempt to use the more complex, semantically oriented definitions of character or string equality and collating order defined in the Unicode 2.0 specification [Unicode]. Therefore strings that are canonically equal according to the Unicode standard could test as unequal. In effect, this algorithm assumes that both strings are already in normalized form.

6.6.6 Binary Bitwise Operators

Three types of binary bitwise operators are supported: AND, OR, and XOR. These operators are applied as part of the evaluation of a BitwiseANDExpression, BitwiseORExpression , or a BitwiseXORExpression , whose definition implies their precedence order, and evaluation algorithm is described in Table 6.15. Note that in all cases, both operands of a bitwise operator are evaluated.

Table 6.15. Evaluating the expression A @ B for @=AND, OR, and XOR

Step

Action

1

Evaluate A.

2

Call GetValue (the result of step 1 ) .

3

Evaluate B.

4

Call GetValue (result of step 3 ) .

5

Call ToInt32 (result of step 2 )

6

Call ToInt32 (result of step 4 ) .

7

Return the bitwise operator @ to Result(5) and Result(6) that is a signed 32 bit integer.

6.6.6.1 Binary Logical Operators

The two types of binary logical operators supported are AND and OR. These operators are applied as part of the evaluation of a LogicalANDExpression or LogicalORExpression , whose definition implies their precedence order, and evaluation algorithm is described in Tables 6.16 and 6.17.

Table 6.16. Evaluating Logical AND Expressions

Step

Action

1

Evaluate LogicalANDExpression

2

Call GetValue (the result of step 1 ) .

3

Call ToBoolean (the result of step 2 ) .

4

If the result of step 3 is false, then return the result of step 2.

5

Evaluate BitwiseORExpression.

6

Call GetValue (the result of step 5 ) .

7

Return the result of step 6

Table 6.17. Evaluating Logical OR Expressions

Step

Action

1

Evaluate LogicalORExpression

2

Call GetValue (the result of step 1 ) .

3

Call ToBoolean (the result of step 2 ) .

4

If the result of step 3 is true, then return the result of step 2.

5

Evaluate LogicalANDExpression.

6

Return GetValue (the result of step 5 ) .

6.6.7 Conditional Operator

A conditional operator is applied as when evaluating a ConditionExpression , which is either a LogicalORExpression or a LogicalORExpression followed by ? , followed by an AssignmentExpression , followed by : and followed by another AssignmentExpression . Table 6.18 specifies the evaluation process. The syntax for a ConditionalExpression in ECMA Script is somewhat different from that in C and Java, which each allow the second subexpression to be an Expression but restrict the third expression to be a conditional expression. The motivation for this difference in ECMA Script is to allow an assignment expression to be governed by both values of the conditional and to eliminate the confusing and not so useful case of a comma expression as the center expression.

Table 6.18. Evaluating Conditional Expressions

Step

Action

1

Evaluate LogicalORExpression.

2

Call GetValue( the result of step 1 ) .

3

Call ToBoolean( the result of step 2 ) .

4

If the result of step 3 is false, then go to step 8.

5

Evaluate the first AssignmentExpression.

6

Call GetValue( the result of step 5 ) .

7

Return the result of step 6.

8

Evaluate the second AssignmentExpression.

9

Return GetValue( the result of step 8 ) .

6.6.8 Assignment Operators

Twelve types of assignment operators are supported: = , *= , /= , %= , += , -= , <<= , >>= , >>>= , &= , ^= , = . The AssignmentExpression which is composed of LeftHandSideExpression = AssignmentExpression is evaluated as described in Table 6.19. The AssignmentExpression which is composed of LeftHandSideExpression @ = AssignmentExpression , where @ , which represents one of the assignment operators indicated earlier in this paragraph, is evaluated as described in Table 6.20. Comma expressions are supported by ECMA Script using the syntax Expression, AssignmentExpression and are evaluated as described in Table 6.21.

Table 6.19. Evaluating Simple Assignment Expressions

Step

Action

1

Evaluate LeftHandSideExpression.

2

Evaluate AssignmentExpression.

3

Call GetValue( result of step 2 ) .

4

Call PutValue( result of step 1, result of step 3 ) .

Table 6.20. Evaluating Compound Assignment Expressions

Step

Action

1

Evaluate LeftHandSideExpression.

2

Call GetValue( result of step 1 )

3

Evaluate AssignmentExpression.

4

Call GetValue( result of step 3 ) .

5

Apply operator @ to result of steps 2 and 4

6

Return PutValue( result of step 1, result of step 5 ) .

Table 6.21. Evaluating Comma Expressions

Step

Action

1

Evaluate Expression.

2

Call GetValue( result of step 1 ) .

3

Evaluate AssignmentExpression.

4

Return GetValue( result of step 3 ) .



ITV Handbook. Technologies and Standards
ITV Handbook: Technologies and Standards
ISBN: 0131003127
EAN: 2147483647
Year: 2003
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net