Numbers

The topics are:

Number Literal Syntax

Numbers can be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8.)

Note

For C/C++ programmers: Numbers have the same syntax as C and C++ integers and doubles. They are internally represented as 64-bit double precision floating-point numbers.

A decimal number consists of a sequence of digits, followed by an optional fraction, followed by an optional exponent. The fraction consists of a decimal point (.) followed by a sequence of digits; the exponent consists of an e or E followed by an optional + or - sign and a sequence of digits. A decimal number must have at least one digit.

Here are some examples of decimal number literals:

15

3.14

4e100

.25

5.25e-10

A hexadecimal number consists of 0x or 0X prefix, followed by a sequence of hexadecimal digits, which include digits (0-9) and the letters a-f or A-F. For example:

0x3ff

0x0

An octal number consists of a 0 followed by a sequence of octal digits, which include the digits 0-7. For example:

0123

0777

Special Numbers

There are three special numbers: NaN (Not-A-Number), Infinity (positive infinity), and ‑Infinity (negative infinity.)

The special number NaN is used to indicate errors in number manipulations. For example, the square root function Math.sqrt applied to a negative number returns NaN. There is no representation of NaN as a number literal, but the global variable NaN contains its value.

The NaN value is contagious, and a numeric operation involving NaN always returns NaN. A comparison operation involving NaN always returns false—even the NaN == NaN comparison.

Examples:

Math.sqrt(-1) -> NaN

Math.sqrt(NaN) -> NaN

NaN + 3 -> NaN

NaN == NaN -> false

NaN <= 3 -> false

NaN >= 3 -> false

The special numbers Infinity and -Infinity are used to indicate infinite values and overflows in arithmetic operations. The global variable Infinity contains the positive infinity. The negative infinity can be computed using the negation operator (‑Infinity).

Examples:

1/0-> Infinity

-1/0 -> -Infinity

1/Infinity -> 0

Infinity == Infinity -> true

Automatic Conversion to a Number

When a function or a method which expects a number as one of its arguments is passed a non-numeric value, it tries to convert this value to a number using the following rules:

  • A Strings is parsed as a number literal. If the string does not represent a valid number literal, the conversion yields NaN.

  • A Dates yields the corresponding number of milliseconds since 00:00:00 UTC, January 1, 1970.

For example, if the Math.sqrt function is passed a string, this string is converted to the number it represents:

Math.sqrt("25") -> 5

Similarly, operators which take numeric operands attempt to convert any non-numeric operands to a number:

"3" * "4"-> 12

For operators which can take both strings and numbers, such as +, the conversion to a string takes precedence over the conversion to a number (see Automatic Conversion to a String .) In other words, if at least one of the operands is a string, the other operand is converted to a string; if none of the operands is a string, the operands are both converted to numbers. For example:

"3" + true -> "3true"

3 + true -> 4

For comparison operators, such as == and >=, the conversion to a number takes precedence over the conversion to a string. In other words, if at least one of the operands is a number, the other operand is converted to a number. If both operands are strings, the comparison is made on strings. For example:

"10" > "2" -> false

"10" > 2 -> true

Number Methods

The only number method is:

Script Number Method

Syntax

Effect

number.toString( )

Returns a string representing the number as a literal.

Example:

  (14.3e2).toString() -> "1430"

Numeric Functions

The following numeric functions are defined:

Note

For C/C++ programmers: Most of these functions are wrap-ups for standard math library functions.

Script Numeric Functions

Syntax

Effect

Math.abs(x)

Returns the absolute value of x.

Math.max(x, y)
Math.min(x, y)

Math.max(x, y) returns the larger of x and y, and Math.min(x, y) returns the lowest of the two.

Math.random( )

Returns a pseudo-random number between 0, inclusive, and 1, exclusive.

Math.ceil(x)
Math.floor(x)
Math.round(
x)

Math.ceil(x) returns the least integral value greater or equal to x. Math.floor(x) returns the greatest integral value less or equal to x. Math.round(x) returns the nearest integral value of x.

Math.sqrt(x)

Returns the square root of x.

Math.sin(x)
Math.cos(
x)
Math.tan(
x)
Math.asin(
x)
Math.acos(
x)
Math.atan(
x)
Math.atan2(y,
x)

Math.sin(x), Math.cos(x) and Math.tan(x) return trigonometric functions of radian arguments.

Math.asin(x) returns the arc sine of x in the range -PI/2 to PI/2.

Math.acos(x) returns the arc cosine of x in the range 0 to PI.

Math.atan(x) returns the arc tangent of x in the range -PI/2 to PI/2.

Math.atan2(y, x) converts rectangular coordinates (x, y) to polar (r, a)by computing a as an arc tangent of y/x in the range -PI to PI.

Math.exp(x)
Math.log(
x)
Math.pow(
x, y)

Math.exp(x) computes the exponential function ex. Math.log(x) computes the natural logarithm of x. Math.pow(x, y) computes x raised to the power y.

Numeric Constants

The following numeric constants are defined:

Script Numeric Constants

Syntax

Value

NaN

Contains the NaN value.

Infinity

Contains the Infinity value.

Number.NaN

Same as NaN.

Number.MAX_VALUE

The maximum representable number, approximately 1.79E+308.

Number.MIN_VALUE

The smallest representable positive number, approximately 2.22E-308.

Math.E

Euler's constant and the base of natural logarithms, approximately 2.718.

Math.LN10

The natural logarithm of 10, approximately 2.302.

Math.LN2

The natural logarithm of two, approximately 0.693.

Math.LOG2E

The base 2 logarithm of e, approximately 1.442.

Math.LOG10E

The base 10 logarithm of e, approximately 0.434.

Math.PI

The ratio of the circumference of a circle to its diameter, approximately 3.142.

Math.SQRT1_2

The square root of one-half, approximately 0.707.

Math.SQRT2

The square root of two, approximately 1.414.

Numeric Operators

The following numeric operators are available:

Note

For C/C++ programmers: These operators are the same as in C and C++.

Script Numeric Operators

Syntax

Effect

x + y

x
- y

x
* y

x
/ y

The usual arithmetic operations.

Examples:

  3 + 4.2-> 7.2

  100 - 120-> -20

  4 * 7.1-> 28.4

  6 / 5-> 1.2

- x

Negation.

Examples:

  - 142-> -142

x % y

Returns the floating-point remainder of dividing x by y.

Examples:

  12 % 5-> 2

  12.5 % 5-> 2.5

x == y

x
!= y

The operator == returns true if x and y are equal, and false otherwise. The operator != is the converse of ==.

Examples:

  12 == 12 -> true

  12 == 12.1-> false

  12 != 12.1-> true

x < y

x
<= y

x
> y

x
>= y

The operator < returns true if x is smaller than y, and false otherwise. The operator <= returns true if x is smaller or equal to y, and false otherwise; and so on.

Examples:

  -1 < 0-> true

  1 < 1-> false

  1 <= 1-> true

x & y

x
| y

x
^ y

The bitwise operations AND, OR, and XOR. X and y must be integers in the range of -2**32+1 to 2**32-1 (-2147483647 to 2147483647.)

Examples:

  14 & 9-> 8 (1110 & 1001 -> 1000)

  14 | 9-> 15 (1110 | 1001 -> 1111)

  14 ^ 9-> 7 (1110 ^ 1001 -> 111)

~ x

Bitwise NOT. X must be an integer in the range of -2**32+1 to 2**32-1 (-2147483647 to 2147483647.)

Examples:

  ~ 14 -> 1 (~ 1110 -> 0001)

x << y

x
>> y

x
>>> y

Binary shift operations. X and y must be integers in the range of -2**32+1 to 2**32-1 (-2147483647 to 2147483647.) The operator << shifts to the left, >> shifts to the right (maintaining the sign bit), and >>> shifts to the right, shifting in zeros from the left.

Examples:

  9 << 2 -> 36 (1001 << 2 -> 100100)

  9 >> 2 -> 2 (1001 >> 2 -> 10)

  -9 >> 2 -> -2 (1..11001 >> 2 -> 1..11110)

  -9 >>> 2 -> 1073741821 (1..11001 >>> 2 -> 01..11110)