XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (335 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
12.69Mb size Format: txt, pdf, ePub

If there are several operators with the same priority, they are evaluated from left to right. For example
5-2-2
means
(5-2)-2
, which evaluates to
1
.

Type Promotion

There are special rules for arithmetic operators to determine the type of the result of the expression, based on the types of the operands.

If the operands have the same type, then in general the result is the same type as the operands. So, for example, the sum of two
xs:integer
values is an
xs:integer
, while the result of multiplying two
xs:double
values is an
xs:double
. The exception to this rule is the
div
operator, when the operands are
xs:integer
values: in this case, the result is an
xs:decimal
. For example, the result of
5
div
2
is the
xs:decimal
value
2.5
.

The phrase “the same type” in this rule means the underlying numeric type: one of
xs:integer
,
xs:decimal
,
xs:float
, or
xs:double
. If you add two
xs:short
values, the result will be an
xs:integer
, not an
xs:short
. (At any rate, it's not guaranteed to be an
xs:short
; the only requirement on the implementation is that the result must be an
xs:integer
, and returning an
xs:short
would satisfy that requirement, as long as the result of the operation is in the range of values that
xs:short
can handle.)

If you use these operators to combine two values that are of different types, type promotion kicks in. This defines a pecking order among the four numeric types:
xs:double
wins over
xs:float
,
xs:float
over
xs:decimal
, and
xs:decimal
over
xs:integer
. If you mix two types, then the loser in this pecking order is first converted to the type of the winner, and the result has the same type as the winner. So, for example, the result of the expression
2.5
+
1
is the
xs:decimal
value
3.5
, because
2.5
is an
xs:decimal
and
1
is an
xs:integer
, and
xs:decimal
is higher in the pecking order.

Other books

Reckless Territory by Kate Watterson
Say That Again by Sasson, Gemini
Death Qualified by Kate Wilhelm
Night Over Water by Ken Follett
Black Stallion and Satan by Walter Farley
An Appetite for Passion by Cynthia MacGregor