Numbers are the most basic data type there is, and require very little explanation. As we saw in Chapter 2, Lexical Structure, numeric literals can be integer or floating-point, and integers can be expressed in decimal, octal, or hexadecimal notation. JavaScript differs from programming languages like C and Java in that it does not make a distinction between integer value and floating point values. All numbers in JavaScript are represented as floating-point values. JavaScript represents numbers using the standard 8-byte IEEE floating-point numeric format, which means that it can represent numbers as large as +/-1.7976931348623157x10^308, and numbers as small as +/-2.2250738585072014x10^-308.[2]
[2] This format will be familiar to Java programmers as the format of the double type. It is also the double format used in almost all modern implementations of C and C++.
JavaScript programs work with numbers using the arithmetic operators that the language provides. These include + for addition, - for subtraction, * for multiplication, and / for division. Full details on these and other arithmetic operators are in Chapter 4, Expressions and Operators.
In addition to these basic arithmetic operations, JavaScript supports more complex mathematical operations through a large number of mathematical functions that are a core part of the language. For convenience, these functions are all stored as properties of a single object named Math, and so we use always use the literal name Math to access them. For example, to compute the sine of the numeric value x, we would write code like this:
sine_of_x =index.html Math.sin(x);
hypot =index.html Math.sqrt(x*x + y*y);
There are several special numeric values used by JavaScript. When a floating-point value becomes larger than the largest representable type, the result is a special infinity value, which JavaScript prints as Infinity. Similarly, when a negative value becomes more negative than the most negative representable number, the result is negative infinity, printed as -Infinity. (Internet Explorer 3.0 prints these special infinity values in a less intuitive fashion; this will be fixed.)
Another special JavaScript numeric value is returned when a mathematical operation (such as division by zero) yields an undefined result or an error. In this case, the result is the special Not-a-Number value, printed as NaN. The special Not-a-Number value has special behavior: it does not compare equal to any number, including itself! For this reason, a special function isNaN() is required to test for this value. In Navigator 2.0, the NaN value and the isNaN() do not work correctly on Windows and other platforms. On 2.0 Windows platforms, 0 is returned instead of NaN when a numeric value is undefined. Similarly, NaN does not work in Internet Explorer 3.0, although it will in future versions. In IE 3.0, isNaN() always returns false, and functions return 0 instead of NaN.
In Navigator 3.0 (but not IE 3.0), there are constants defined for each of these special numeric values. These constants are listed in Table 3.1.
Constant | Meaning |
---|---|
Number.MAX_VALUE | Largest representable number |
Number.MIN_VALUE | Most negative representable number |
Number.NaN | Special not-a-number value |
Number.POSITIVE_INFINITY | Special value to represent infinity |
Number.NEGATIVE_INFINITY | Special value to represent negative infinity |
file: /Techref/language/JAVA/script/definitive/ch03_02.htm, 8KB, , updated: 2019/10/14 15:00, local time: 2024/11/29 13:33,
18.117.106.23:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://sxlist.com/techref/language/JAVA/script/definitive/ch03_02.htm"> [Chapter 3] 3.2 Numbers</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.