Numbers in JavaScript
In the Identifiers and Reserved Words blog, we learned about what identifiers are, what reserved words are, and a list of reserved words. JavaScript code involves choosing unique identifiers, steering clear of reserved words like "if" or "function," and punctuating statements with semicolons for flawless execution. In this blog, we will go through the Numbers in JavaScript
What are Numbers?
Unlike many other languages, JavaScript does not distinguish between integer and floating-point values. In JavaScript, all integers are represented as floating-point values. JavaScript uses the 64-bit floating-point format defined by the IEEE 754 standard^1 to represent numbers, which can be as large as ±1.7976931348623157 × 10308 and as small as ±5 × 10−324.
What is IEEE 754 standard?
The IEEE 754 standard is a solid foundation of accurate floating-point representation in computing. Its well-defined structure and formats, single and double precision, ensures that real numbers are faithfully represented, supporting precise calculations in the digital world.
The JavaScript number format allows you to precisely represent all numbers between −9007199254740992 (−253) and 9007199254740992 (253), inclusive. If you use larger integer values, you may lose precision in the tail digits. However, certain JavaScript operations are performed with 32-bit numbers. When a number appears directly in a JavaScript program, it’s called a numeric literal. JavaScript supports numeric literals in several formats, which are as follows:
Integer Literals
In JavaScript, integer literals are used to represent whole numbers (positive or negative) without any fractional or decimal portions. Integer literals can be expressed in a variety of ways in JavaScript:
Decimal Literals(base-10)
Decimal literals are base-10 numbers and are the most common way to represent integers. They consist of a sequence of digits without any leading zeros (except for the number zero itself). For example:
Binary Literals
Binary literals are represented using the 0b or 0B prefix followed by a sequence of binary digits (0 and 1). For example:
Octal Literals(base-8)
An octal literal(base-8) begins with the digit “0o” (Zero ‘o’) and is followed by a sequence of digits, each between 0 and 7. For example:
Hexadecimal Literals
JavaScript, a powerful programming language, understands hexadecimal (base-16) values in its own special way. Hexadecimal literals start with "0x" or "0X," and are followed by a string of significant hexadecimal values. Each hexadecimal digit can represent a value from 10 to 15 by being one of the integers 0 through 9 or the letters 'a' (or 'A') through 'f' (or 'F'). For example:
Floating-Point Literals
Floating-point literals can have a decimal point; they use the traditional syntax for real numbers. A real value is represented as the integral part of the number, followed by a decimal point and the fractional part of the number. The general syntax is as follows:
- [digits]: Refers to a sequence of digits (0-9).
- [.digits]: Indicates an optional decimal point followed by more digits.
- [(E|e)[(+|-)]digits]: Represents an optional exponent (E ore) followed by an optional positive (+) or negative (-) sign, and then a sequence of digits.
In JavaScript the Floating-Point Literals can be written in the following ways:
Standard Decimal Literal
Floating-Point Literals can be represented by using a sequence of digits with decimal points. For example:
Exponential Notation:
Floating-point literals may also be represented using exponential notation: a real number followed by the letter e (or E) where e or Estands for exponential. Let take a look at the example on adding two positive exponent
Let's take a look at the example:
no1 = 2.9e2 i.e. 2.9 * 10 * 10 = 290
no2 = 3.2e3 i.e. 3.2 * 10 * 10 * 10 = 3200
sum = 3200 + 290 = 3490
Now let’s take a look at the other example adding two negative exponent
Let's take a look into the example:
no1 = 2.3e-4 i.e.( 2.3 * 10^(-4)) = (2.3 * 1/10^4) = 0.00023
no2 = 5.5e-3 i.e.(5.5 * 10^(-3)) = (5.5 * 1/10^3) = 0.0055
sum = 0.00023 + 0.0055 = 0.00573
What is the use of toExponential()?
The toExponential() method is used to convert a number to its exponential notation (scientific notation) representation as a string.
Binary Floating Points and Rounding Errors
In JavaScript, there are a ton of real numbers, but the way it stores them has some limits. It can only represent a specific number of these numbers really precisely (that number is18437736874454810627, if curious). So, when we are working with real numbers in JavaScript, the way the numbers are written might not be exactly right.
JavaScript uses a system called IEEE-754 to deal with these numbers, but it struggles with certain fractions like 1/10 or 1/100, especially in financial calculations. Even though JavaScript numbers are quite accurate, representing numbers like 0.1 can still cause some issues. Let's see this in action in the following code.
The difference between the approximations of 0.4 and 0.3 isn't exactly the same as the difference between the approximations of 0.2and 0.1. This happens because of a rounding error. It's important to know that this problem isn't unique to JavaScript; it affects all computer languages using binary floating-point numbers.
In the provided code, the values x and y are very close to the correct values, making them suitable for most purposes. However, problems arise when we want to compare these values for exact equality. In the future, JavaScript might introduce a decimal numeric type to fix these rounding issues.
In JavaScript, numbers may have slight approximations due to how they're stored. Rounding errors, common to many computer languages, affect precise comparisons. Despite this, computed values are generally suitable for most purposes. It's crucial to be mindful of potential differences when comparing numbers for exact equality.