JavaScript 学习-18.Math数学运算

前言

Math 对象允许您执行数学任务。
Math 不是构造函数。Math 的所有属性/方法都可以通过使用 Math 作为对象来调用,而无需创建它

Math 属性

Math 提供了一些属性,可以快速得到一个数学里面的值,如圆周率π(约为3.14),2的平方根约1.414

const x = Math.PI;            // 返回 PI
const y = Math.SQRT2;      // 返回 2 的平方根
console.log(x);
console.log(y);

Math 方法

Math 对象一些常用的方法:

  • abs(x), 返回x的绝对值
  • max(x, y, z, …, n) 返回最大值
  • min(x, y, z, …, n) 返回值最小的数字。
  • pow(x, y) 返回 x 的 y 次幂值。
  • round() 返回数字四舍五入到最接近的整数。
  • floor(x) 返回向下舍入为最接近的整数。
  • random() 返回0到1直接随机数

abs(x), 返回x的绝对值

let x = 3;
let y = -4;
let a = Math.abs(x);
let b = Math.abs(y);
console.log(a, b)   // 3 4

max(x, y, z, …, n) 返回最大值

let a = Math.max(2, 5);
let b = Math.max(0, 2, 4, 7, 12);
let c = Math.max(-2, 5);
console.log(a);  // 5
console.log(b);   // 12
console.log(c);   // 5

如何得到一个数组对象里面成员最大值和最小值?

let x = [2, 4, 7, 12];
console.log(Math.max(x));  // NaN
console.log(Math.min(x));  // NaN

如果 Math.max() 直接传数组对象,那么得到的结果是NaN.
max()和min()方法需要传多个参数,而不是一个数组,所以可以用到扩展运算符(…)

let x = [2, 4, 7, 12];
console.log(Math.max(...x));  // 12
console.log(Math.min(...x));  // 2

pow(x, y) 返回 x 的 y 次幂

let x = Math.pow(2, 3);  // 2的3次幂 8
let y = Math.pow(2, 4);  // 2的3次幂 16
console.log(x);
console.log(y);

指数运算还可以用到两个星号运算符**

let x = 2**3;
let y = 2**4;
console.log(x);  // 8
console.log(y);   // 16

round() 返回数字四舍五入到最接近的整数。

四舍五入取整

let a = Math.round(2.60);   // 3
let b = Math.round(2.50);   // 3
let c = Math.round(2.49);   // 2
let d = Math.round(-2.60);   // -3
let e = Math.round(-2.50);  // -2
let f = Math.round(-2.49);  // -2

floor(x) 返回向下舍入为最接近的整数。

向下舍入取整

let a = Math.floor(0.60);   // 0
let b = Math.floor(0.40);   // 0
let c = Math.floor(5);    // 5
let d = Math.floor(5.1);   // 5
let e = Math.floor(-5.1);   // -6
let f = Math.floor(-5.9);    // -6

random() 返回随机数

random() 方法返回从 0(含)到 1(不含)的随机数。

let x = Math.random();
let y = Math.random();
console.log(x);  // 0.041820330842348596
console.log(y);  // 0.8178949610470638

返回 1 到 10 之间的随机整数:

let x = Math.floor((Math.random() * 10) + 1);
console.log(x); // 5

返回 1 到 100 之间的随机整数:

let x = Math.floor((Math.random() * 100) + 1);
console.log(x); // 80
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Math.pow()和Mathf.Pow()是两种不同的数学函数,分别用于计算幂运算。 - Math.pow()是JavaScript中的函数,用于计算一个数字的指定次幂。它接受两个参数,第一个参数是底数,第二个参数是指数。例如,Math.pow(2, 3)将返回8,因为它计算了2的3次幂。 - Mathf.Pow()是C#中的函数,用于计算一个数字的指定次幂。它同样接受两个参数,第一个参数是底数,第二个参数是指数。例如,Mathf.Pow(2, 3)将返回8。与Math.pow()不同的是,Mathf.Pow()是在Unity游戏开发中使用的函数,用于处理游戏中的数学计算。 所以,Math.pow()和Mathf.Pow()是两个不同的函数,分别用于不同的编程语言环境。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Javascript四舍五入Math.round()与Math.pow()使用介绍](https://download.csdn.net/download/weixin_38665162/13052609)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [math.pow()函数用法](https://blog.csdn.net/qq_41024101/article/details/81058364)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值