类型和值:Number 和 BigInt

1. 类型和值:Number 和 BigInt

Number 类型的舍入误差怎么理解?解决方式有哪些?

舍入误差 : 由于JavaScript的小数位有限,一些无线循环小数无法正确表示,会导致一些误差存在

  • 先将转换为整数,相加后再转换为小数,如下

    let x = (0.1*10+0.2*10)/10;
    console.log(x === 0.3)		//true
    
  • 使用number对象中的toFixed方法可以指定小数位

  • es6新增Number.EPSILON方法

学习 BigInt 类型(参考 MDN)

参考链接

描述

可以用在一个整数字面量后面加n的方式定义一个BigInt,如:10n,或者调用函数BigInt()但不包含new运算符,并传递一个整数值或者字符串值

const thiBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
cosnt hugeString = BigInt("9007199254740991n");
cosnt hugeHex = BigInt("0x1fffffffffff");
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111")

它在某些方面类似于Number,但是也有几个关键的不同点:

  1. 不能用于Math对象中的方法;
  2. 不能和任何Number实例混合运算,两者必须转换成同一种类型.在两种类型来回转换时要小心,因为BigInt变量在转换成Number变量时可能会精度丢失
类型信息

使用typeof测试时,BigInt对象返回"bigint":

typeof ln === 'bigint'          //true
typeof BigInt('1') === 'bigint'	//true
运算

以下操作可以和BigInt一起使用:+,-,*,/,**,%,出>>>(无符号右移)之外的位操作也是支持,因为BigInt都是有符号的,BigInt不支持单目(+)操作

const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER);
// ↪ 9007199254740991n

const maxPlusOne = previousMaxSafe + 1n;
// ↪ 9007199254740992n

const theFuture = previousMaxSafe + 2n;
// ↪ 9007199254740993n, this works now!

const multi = previousMaxSafe * 2n;
// ↪ 18014398509481982n

const subtr = multi – 10n;
// ↪ 18014398509481972n

const mod = multi % 10n;
// ↪ 2n

const bigN = 2n ** 54n;
// ↪ 18014398509481984n

bigN * -1n
// ↪ –18014398509481984n

/操作符对于整数的运算也没问题,可是因为这些变量是BigInt而不是BigDecimal.该操作符结果会向零取整,也就是说不会返回小数部分

**警告:**当使用 BigInt 时,带小数的运算会被取整。

const expected = 4n / 2n;
// ↪ 2n

const rounded = 5n / 2n;
// ↪ 2n, not 2.5n
比较

BigIntNumber不是严格相等的,但是宽松相等的.

0n === 0 //false
0n == 0 //true

BigIntNumber可以进行比较

1n < 2 // true
2n > 1 // true
2 > 2 //false
2n > 2 // false
2n >= 2 // true

两者也可以混在一个数组内排序

const mixed = [4n, 6, -12n, 10, 4, 0, 0n];
// ↪  [4n, 6, -12n, 10, 4, 0, 0n]

mixed.sort();
// ↪ [-12n, 0, 0n, 10, 4n, 4, 6]

注意被Object包装的BigInt使用object的比较规则进行比较,只用同一个对象在比较时才会相等

0n === Object(0n); //false
Object(0n) === Object(0n); //false

const o = Object(0n);
o === o //true
条件
if (0n) {
  console.log('Hello from the if!');
} else {
  console.log('Hello from the else!');
}

// ↪ "Hello from the else!"

0n || 12n
// ↪ 12n

0n && 12n
// ↪ 0n

Boolean(0n)
// ↪ false

Boolean(12n)
// ↪ true

!12n
// ↪ false

!0n
// ↪ true
静态方法

BigInt.asIntN()

​ 将 BigInt 值转换为一个 -2^(width-1) 与 2^(width-1) - 1 之间的有符号整数。

BigInt.asUintN()

​ 将一个 BigInt 值转换为 0 与 2^(width) - 1 之间的无符号整数。

实例方法

BigInt.prototype.toLocaleString()

​ 返回此数字的language-sensitive形式的字符串.覆盖 Object.prototype.toLocaleString()

BigInt.prototype.toString()

返回以指定技术(base)表示指定数字的字符串

BigInt.prototype.valueOf()

返回指定对象的基元值

实现一个大数相加算法

const { log } = console;

// 输入两个巨大的整数型字符串
const arr1 = '99';
const arr2 = '7';

// 输出一个正确的字符串表示相加结果
function add(arr1, arr2) {
    const arr1Re = [...arr1].reverse();
    const arr2Re = [...arr2].reverse();

    log(arr1Re,arr2Re)

    let flag= 0;
    const res = [];

    while(arr1Re.length > 0 || arr2Re.length > 0) {
        let t1 = Number.parseInt(arr1Re.pop()) || 0;
        let t2 = Number.parseInt(arr2Re.pop()) || 0;

        let tmp = t1 + t2 + flag;

        flag = Math.floor(tmp/10);

        res.unshift(tmp%10);
    }
    if(flag) {
        res.unshift(flag)
    }
    log(res.join(""))
}

const result = add(arr1, arr2);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
BigInt 是 JavaScript 中的一种数据类型,用于表示任意精度的整数。它可以处理超出 JavaScript Number 类型表示范围的整数。 使用 BigInt 时,需要在数字后面添加 "n" 后缀,以明确告诉 JavaScript 解释器这是一个 BigInt 。例如:`const bigNum = 1234567890123456789012345678901234567890n;` 以下是一些 BigInt 的特性和用法说明: 1. 创建 BigInt:在数字后添加 "n" 后缀即可创建 BigInt。 ```javascript const bigNum = 1234567890123456789012345678901234567890n; ``` 2. 运算操作:可以使用常规的算术和比较运算符来操作 BigInt。 ```javascript const a = 10n; const b = 20n; const sum = a + b; // 30n const difference = b - a; // 10n const product = a * b; // 200n const division = b / a; // 2n ``` 3. 可以与普通数字进行混合运算,但结果将总是 BigInt 类型。 ```javascript const a = 10n; const b = 20; const sum = a + BigInt(b); // 30n ``` 4. BigInt 不支持与 Number 类型直接进行比较,需要使用 BigInt() 函数将 Number 类型转换为 BigInt 类型后再进行比较。 ```javascript const a = 10n; const b = 20; console.log(a > BigInt(b)); // true ``` 5. BigInt 还支持一些内置方法,如 toString()、valueOf() 和 toLocaleString() 等。 请注意,BigInt 在 JavaScript 中是相对较新的功能,可能不被所有浏览器和环境广泛支持。在使用 BigInt 时,请确保你的目标平台兼容该功能,或者考虑使用第三方库来处理大整数运算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰镇白干

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值