ECMAScript 4 Netscape Proposal
Libraries
Machine Types
previousupnext

Tuesday, March 4, 2003

Purpose

Machine types are low-level numeric types for use in ECMAScript 4 programs. These types provide Java-style integer operations that are useful for communicating between ECMAScript 4 and other programming languages. These types are not intended to replace Number and Integer for general-purpose scripting.

Contents

The following low-level numeric types are available:

Type Suffix  Values
sbyte   Integer values between –128 and 127 inclusive, excluding –0.0
byte   Integer values between 0 and 255 inclusive, excluding –0.0
short   Integer values between –32768 and 32767 inclusive, excluding –0.0
ushort    Integer values between 0 and 65535 inclusive, excluding –0.0
int   Integer values between –2147483648 and 2147483647 inclusive, excluding –0.0
uint   Integer values between 0 and 4294967295 inclusive, excluding –0.0
long L Long integer values between –9223372036854775808 and 9223372036854775807 inclusive
ulong UL Long integer values between 0 and 18446744073709551615 inclusive
float F Single-precision IEEE floating-point numbers, including positive and negative zeroes, infinities, and NaN

The above type names are not reserved words.

8, 16, and 32-bit Integers

The first six types sbyte, byte, short, ushort, int, and uint are all proper subtypes of Integer, which is itself a subtype of Number. A particular number is a member of multiple types. For example, 3.0 is a member of sbyte, byte, short, ushort, int, uint, Integer, Number, and Object, while –2000.0 is a member of short, int, Integer, Number, and Object. ECMAScript does not distinguish between the literals 3 and 3.0 in any way.

All arithmetic operations and comparisons on sbyte, byte, short, ushort, int, and uint values treat them just like they would any other Number values — the operations are performed using full IEEE double-precision arithmetic.

Implicit Coercions

There are no predefined implicit coercions from values of type sbyte, byte, short, ushort, int, or uint other than the coercions predefined on the type Number. The following predefined implicit coercions are applicable when the destination type is sbyte, byte, short, ushort, int, or uint:

Note that there are no implicit coercions from +, –, or NaN to sbyte, byte, short, ushort, int, or uint.

Explicit Coercions

There are no predefined explicit coercions from values of type sbyte, byte, short, ushort, int, or uint other than the coercions predefined on the type Number. The predefined explicit coercions below are applicable when the destination type T is sbyte, byte, short, ushort, int, or uint. The notation |T| represents the range of the type T, where |sbyte| = |byte| = 256, |short| = |ushort| = 65536, and |int| = |uint| = 232.

64-bit Integers

The types long and ulong represent signed and unsigned 64-bit integers. long and ulong literals are written with the suffix L or UL and no exponent or decimal point. Literal values of type long are written as –9223372036854775808L through 9223372036854775807L. Literal values of type ulong are written as 0UL through 18446744073709551615UL.

The types long and ulong are disjoint from Number, so 5L and 5 are different objects, although they compare == and === to each other. 5L and 5UL are also different objects, although they compare == and === to each other.

Negation, addition, subtraction, and multiplication, and modulo (%) on long and ulong values is exact, and long and ulong values may be mixed in an expression. There are five possible cases depending on the mathematical result x:

Division involving two long or ulong operands returns the most precise quotient available from among the possible long, ulong, and Number values. In some cases the quotient will be a long or ulong; in other cases the quotient will be a Number. See the semantics for the details.

Division and modulo on long and ulong values can produce the Number values positive or negative infinity or NaN when the divisor is zero.

Addition, subtraction, multiplication, division, and modulo mixing a long or ulong operand with a Number (or any subtype of Number) or float operand first checks whether the Number or float operand is an exact integer (including either +0.0 or –0.0 but not infinities or NaN). If it is, then the computation uses the integral semantics above. If not, then the long or ulong operand is coerced to a Number and the operation is done using Number arithmetic.

The bitwise operations &, |, and ^ are 64 bits wide if at least one operand is a long or ulong, in which case the other operand is truncated to an integer and treated modulo 264 if necessary. The result is a ulong if at least one operand is a ulong; otherwise, the result is a long.

The bitwise shifts <<, >>, and >>> are 64 bits wide if the first operand is a long or ulong. The result is a ulong if the first operand is a ulong; otherwise, the result is a long. >> copies the most significant bit and >>> shifts in zero bits regardless of whether the first operand is a long or ulong.

Comparisons mixing a long or ulong operand with a Number (or any subtype of Number) or float operand compare exact mathematical values without any coercions.

Implicit Coercions

The following predefined implicit coercions are applicable when the destination type is long:

The following predefined implicit coercions are applicable when the destination type is ulong:

Note that there are no implicit coercions from NaN or positive or negative infinity to long or ulong.

A long or ulong value can be implicitly coerced to type Number, Integer, or float. The result is the closest representable Number or float value using the same rounding as when a string is converted to a number. If the source is 0L or 0UL then the result is +0.0 or +0.0F.

Explicit Coercions

The predefined explicit coercions below are applicable when the destination type T is long or ulong.

A long or ulong value x can be explicitly coerced to type Number, Integer, float or String. Explicit coercions to Number, Integer, float are the same as the implicit coercions. Explicit coercions to type String produce the x as a string of decimal digits. Negative values have a minus sign prepended. Zero produces the string "0"; all other values produce strings starting with a non-zero digit.

Single-Precision Floats

The type float represents single-precision IEEE floating-point numbers. float literals are written with the suffix F. float infinities and NaN are separate from Number infinities and NaN.

The type float is disjoint from Number, so 5F and 5 are different objects, although they compare == to each other.

Negating a float value returns a float value. All other arithmetic first converts the float value to the corresponding Number value. The bitwise operations &, |, ^, <<, >>, and >>> coerce any float operands to type Number before proceeding.

Implicit Coercions

The following predefined implicit coercions are applicable when the destination type is float:

A float value can be implicitly coerced to type Number. The result is the equivalent Number value.


Waldemar Horwat
Last modified Tuesday, March 4, 2003
previousupnext