JavaScript. Properties of Number object

Properties of Number object


Contents


Search other resources:

1. Overview of the properties of the Number object

The Number object has the following properties:

  • EPSILON – value that determines the minimum possible accuracy of calculations;
  • MAX_SAFE_INTEGER – maximum safe integer value;
  • MAX_VALUE – maximum possible numerical value;
  • MIN_SAFE_INTEGER – minimum safe integer value;
  • MIN_VALUE – minimum possible value;
  • NaN – Not a Number value;
  • NEGATIVE_INFINITY – designation of negative infinity;
  • POSITIVE_INFINITY – designation of positive infinity.

 

2. Property Number.EPSILON. Precision calculations for floating point numbers

The Number.EPSILON property represents a value that is the difference between 1 and the next floating point value greater than 1. In other words, Number.EPSILON is a value that provides precision calculations for floating point numbers.

Example.

// Property Number.EPSILON

// 1. Display property value
alert(Number.EPSILON) // 2.220446049250313e-16

// 2. Equality/inequality between two numbers
var x = 0.5
var y = 1.2 - 0.8 + 0.1

if (Math.abs(x-y) < Number.EPSILON)
  alert("x == y"); // x == y
else
  alert("x != y");

 

3. Properties Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER. Boundaries of the correct representation of integers

The Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER properties define the boundaries of the correct representation of integers. As you know, all numeric values in JavaScript are 64-bit floating point values according to the IEEE-754 encoding standard. Accordingly, the encoding of integers in 64-bit floating point format for integers has its limitations or allowable limits. These boundaries are represented precisely by these constants.

Example.

// Properties Number.MIN_SAFE_INTEGER та NUMBER.MAX_SAFE_INTEGER

// Display property values
alert(Number.MIN_SAFE_INTEGER) // -9007199254740991
alert(Number.MAX_SAFE_INTEGER) // 9007199254740991

 

4. Properties Number.MIN_VALUE and Number.MAX_VALUE. Minimum and maximum positive value

The Number.MIN_VALUE and Number.MAX_VALUE properties represent the minimum and maximum positive value that can be represented in JavaScript.

The value of Number.MIN_VALUE is a positive floating point number that is the smallest in terms of its proximity to 0, limited to 64-bit encoding according to the ECMAScript specification. The Number.MIN_VALUE property has an approximate value of 2-1024.

The MIN_VALUE and MAX_VALUE properties are accessed as global static properties without creating a Number object.

The Number.MAX_VALUE property represents the maximum possible numeric value that can be obtained for the Number type. The value of Number.MAX_VALUE is approximately equal to 21024.

Example.

// Properties Number.MIN_VALUE and NUMBER.MAX_VALUE

// Display property values
alert(Number.MIN_VALUE) // 5e-324
alert(Number.MAX_VALUE) // 1.7976931348623157e+308

 

5. Property Number.NaN. Not a Number

The Number.NaN property is an object that is not a number (Not a Number).
The operation of the property is closely related to the isNaN() function, which returns true if the Number object is not a number.

Example.

// Properties Number.MIN_VALUE and NUMBER.MAX_VALUE
var a = Number.NaN
var res = Number.isNaN(a) // res = true
a = 0/0 // a = NaN або Number.NaN

 

6. Properties Number.NEGATIVE_INFINITY and Number.POSITIVE_INFINITY. Negative infinity and positive infinity

The static property Number.NEGATIVE_INFINITY defines the negative value of infinity represented by the global property Infinity. According to this, the static property Number.POSITIVE_INFINITY is equal to the value of the global property Infinity.

The Number.NEGATIVE_INFINITY and Number.POSITIVE_INFINITY properties behave differently from mathematical infinity.

For the NEGATIVE_INFINITY value, the following features can be distinguished:

  • multiplying NEGATIVE_INFINITY with any positive value (including POSITIVE_INFINITY) results in NEGATIVE_INFINITY;
  • the product of NEGATIVE_INFINITY and any negative value (including NEGATIVE_INFINITY) is equal to POSITIVE_INFINITY;
  • if any positive value is divided by NEGATIVE_INFINITY, then the result will be negative zero;
  • if any negative value is divided by NEGATIVE_INFINITY, then the result will be positive zero;
  • zero multiplied by NEGATIVE_INFINITY gives NaN;
  • NaN multiplied by NEGATIVE_INFINITY gives NaN;
  • if NEGATIVE_INFINITY is divided by a negative value other than NEGATIVE_INFINITY, then the result will be POSITIVE_INFINITY;
  • if NEGATIVE_INFINITY is divided by any positive value other than POSITIVE_INFINITY, then the result will be equal to NEGATIVE_INFINITY;
  • if NEGATIVE_INFINITY is divided by NEGATIVE_INFINITY, then the result will be equal to NaN;
  • if NEGATIVE_INFINITY is divided by POSITIVE_INFINITY, the result is NaN;
  • for any number x that is not equal to NEGATIVE_INFINITY, the condition is fulfilled: x>NEGATIVE_INFINITY.

The POSITIVE_INFINITY value has the following properties:

  • if you multiply any positive value (including POSITIVE_INFINITY) by POSITIVE_INFINITY, the result is POSITIVE_INFINITY;
  • if you multiply any negative value (including NEGATIVE_INFINITY) by POSITIVE_INFINITY, the result is NEGATIVE_INFINITY;
  • if you divide any positive value by POSITIVE_INFINITY, then the result will be positive zero;
  • if you divide any negative number by POSITIVE_INFINITY, then the result will be negative zero;
  • if you multiply zero by POSITIVE_INFINITY, the result will be NaN;
  • if NaN is multiplied by POSITIVE_INFINITY, the result is NaN;
  • if POSITIVE_INFINITY is divided by any negative value (except NEGATIVE_INFINITY), then the result will be equal to NEGATIVE_INFINITY;
  • if POSITIVE_INFINITY is divided by any positive value other than POSITIVE_INFINITY, the result is POSITIVE_INFINITY;
  • if POSITIVE_INFINITY is divided by NEGATIVE_INFINITY, then the result will be equal to NaN;
  • if POSITIVE_INFINITY is divided by POSITIVE_INFINITY, then the result will be equal to NaN;
  • for any number x other than POSITIVE_INFINITY, the statement is executed: Number.POSITIVE_INFINITY > x.

Example.

// Properties Number.NEGATIVE_INFINITY and NUMBER.POSITIVE_INFINITY

// 1. Assign the Infinity property to variable a
var a = Infinity

// 2. Property Number.NEGATIVE_INFINITY
var b

b = Number.NEGATIVE_INFINITY * Number.POSITIVE_INFINITY // Number.NEGATIVE_INFINITY
b = Number.NEGATIVE_INFINITY * 25 // b = Number.NEGATIVE_INFINITY

b = Number.NEGATIVE_INFINITY * (-25) // b = Number.POSITIVE_INFINITY
b = Number.NEGATIVE_INFINITY * Number.NEGATIVE_INFINITY // Number.NEGATIVE_INFINITY

b = 25 / Number.NEGATIVE_INFINITY // negative 0
b = -25 / Number.NEGATIVE_INFINITY // positive 0

b = 0 * Number.NEGATIVE_INFINITY // NaN

b = NaN * Number.NEGATIVE_INFINITY // NaN

b = Number.NEGATIVE_INFINITY / (-25) // Number.POSITIVE_INFINITY
b = Number.NEGATIVE_INFINITY / 25 // Number.NEGATIVE_INFINITY

b = Number.NEGATIVE_INFINITY / Number.NEGATIVE_INFINITY // NaN

b = Number.NEGATIVE_INFINITY / Number.POSITIVE_INFINITY // NaN

a = 30
if (a>Number.NEGATIVE_INFINITY)
  alert("30 > Number.NEGATIVE_INFINITY"); // +
else
  alert("30 <= Number.NEGATIVE_INFINITY");

// 3. Property Number.POSITIVE_INFINITY

b = Number.POSITIVE_INFINITY * 25 // b = Number.POSITIVE_INFINITY
b = Number.POSITIVE_INFINITY * Number.POSITIVE_INFINITY // POSITIVE_INFINITY

b = Number.POSITIVE_INFINITY * (-25) // b = Number.NEGATIVE_INFINITY
b = Number.POSITIVE_INFINITY * Number.NEGATIVE_INFINITY // Number.NEGATIVE_INFINITY

b = 25 / Number.POSITIVE_INFINITY // positive zero
b = (-25) / Number.POSITIVE_INFINITY // negative zero

b = Number.POSITIVE_INFINITY * 0 // NaN
b = Number.POSITIVE_INFINITY * Number.NaN // NaN

b = Number.POSITIVE_INFINITY / (-25) // Number.NEGATIVE_INFINITY
b = Number.POSITIVE_INFINITY / 25 // Number.POSITIVE_INFINITY

b = Number.POSITIVE_INFINITY / Number.NEGATIVE_INFINITY // NaN
b = Number.POSITIVE_INFINITY / Number.POSITIVE_INFINITY // NaN

a = 30
if (Number.POSITIVE_INFINITY > a)
  alert("Number.POSITIVE_INFINITY > a"); // +
else
  alert("Number.POSITIVE_INFINITY <= a");

 


Related topics