阿里云主机折上折
  • 微信号
Current Site:Index > Extension methods for numerical values

Extension methods for numerical values

Author:Chuan Chen 阅读数:53132人阅读 分类: JavaScript

ECMAScript 6 introduced significant enhancements to numerical processing, adding binary and octal literals, static methods for the Number object, extended methods for the Math object, and global helper functions. These features simplify numerical operations and type checking while introducing more precise mathematical computation capabilities.

Binary and Octal Literals

ES6 introduced binary (prefix 0b or 0B) and octal (prefix 0o or 0O) literal notations, which are directly converted to decimal values:

const binary = 0b1010; // Binary 1010 → Decimal 10
const octal = 0o12;    // Octal 12 → Decimal 10
console.log(binary, octal); // Output: 10 10

Using Number(), string representations of binary/octal numbers can be converted to decimal:

Number('0b1111'); // 15
Number('0o17');   // 15

Static Methods of Number

Number.isFinite() and Number.isNaN()

Number.isFinite() strictly checks whether a value is finite (not Infinity), returning false for non-numeric types:

Number.isFinite(15);      // true
Number.isFinite(Infinity); // false
Number.isFinite('15');    // false (global isFinite('15') returns true)

Number.isNaN() returns true only for NaN, addressing the type coercion issue of the global isNaN():

Number.isNaN(NaN);       // true
Number.isNaN('NaN');     // false (global isNaN('NaN') returns true)

Number.parseInt() and Number.parseFloat()

These behave the same as their global counterparts but are recommended for modular programming:

Number.parseInt('12.5px'); // 12
Number.parseFloat('12.5px'); // 12.5

Number.isInteger()

Checks if a value is an integer, noting that NaN and Infinity return false:

Number.isInteger(25);   // true
Number.isInteger(25.0); // true (In JS, 25.0 is equivalent to 25)
Number.isInteger(25.1); // false

Number.EPSILON

Represents the smallest floating-point difference, useful for addressing precision issues:

function withinErrorMargin(left, right) {
  return Math.abs(left - right) < Number.EPSILON;
}
withinErrorMargin(0.1 + 0.2, 0.3); // true

Safe Integer Range

Number.MAX_SAFE_INTEGER (2^53 - 1) and Number.MIN_SAFE_INTEGER (-2^53 + 1) define the safe integer range, with Number.isSafeInteger() for validation:

Number.isSafeInteger(9007199254740991); // true
Number.isSafeInteger(9007199254740992); // false

Extensions to the Math Object

Math.trunc()

Removes the fractional part, returning the integer:

Math.trunc(4.9);  // 4
Math.trunc(-4.9); // -4

Math.sign()

Determines the sign of a number (positive, negative, zero, or non-numeric):

Math.sign(5);   // 1
Math.sign(-5);  // -1
Math.sign(0);   // 0
Math.sign('x'); // NaN

Math.cbrt()

Calculates the cube root:

Math.cbrt(8); // 2
Math.cbrt(-8); // -2

Math.hypot()

Calculates the square root of the sum of the squares of its arguments:

Math.hypot(3, 4); // 5 (Pythagorean theorem)

Logarithmic Methods

  • Math.expm1(x): e^x - 1
  • Math.log1p(x): ln(1 + x)
  • Math.log10(x): Base-10 logarithm
  • Math.log2(x): Base-2 logarithm
Math.log1p(1);  // ≈0.693
Math.log10(100); // 2

Hyperbolic Functions

New hyperbolic methods like Math.sinh(), Math.cosh(), and Math.tanh().

Exponentiation Operator

The ** operator introduced in ES7 replaces Math.pow():

2 ** 3; // 8
2 ** 3 ** 2; // 512 (evaluated right-to-left)

BigInt Type (ES2020)

Though not part of ES6, as a complement to numerical extensions, BigInt supports very large integer operations:

const bigNum = 9007199254740993n;
console.log(bigNum + 1n); // 9007199254740994n

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.