please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 3
Variables and Data Types
Next
 

3.2 Numbers

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++.

Arithmetic and Mathematical Functions

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);
And to compute the square-root of a numeric expression, we might use code like this (note the use of the * operator for multiplication):

hypot =index.html Math.sqrt(x*x + y*y);
See the Math object and subsequent listings in the reference section of this book for full details on all the mathematical functions supported by JavaScript.

Special Numeric Values

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.

Table 3.1: Special Numeric Constants
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


Previous Home Next
Variables Book Index Strings

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch03_02.htm, 8KB, , updated: 2019/10/14 15:00, local time: 2024/11/23 23:28,
TOP NEW HELP FIND: 
3.17.166.157: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?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/language/java/script/definitive/ch03_02.htm"> [Chapter 3] 3.2 Numbers</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .