首页 > 其他分享 >常用的math 方法

常用的math 方法

时间:2024-06-11 16:58:54浏览次数:7  
标签:常用 console log min max abs math 方法 Math

Math.abs(x)

函数返回一个数字的绝对值。(返回绝对值)

  function difference(a, b) {
        return Math.abs(a - b);
    }
    console.log(difference(3, 5));
    //  2
    console.log(difference(5, 3));
    //  2
    console.log(difference(1.23456, 7.89012));
    //  6.6555599999999995
Math.abs() 将其参数强制转换为数字。无法强制转换的值将变成 NaN,使 Math.abs() 也返回 NaN。

Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(""); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1, 2]); // NaN
Math.abs({}); // NaN
Math.abs("string"); // NaN
Math.abs(); // NaN

Math.floor()

函数总是返回小于等于一个给定数字的最大整数。(向下取整)

console.log(Math.floor(5.95));
// 5
console.log(Math.floor(5.05));
// 5
console.log(Math.floor(5));
// 5
console.log(Math.floor(-5.05));
// -6

Math.ceil()

静态方法总是向上舍入,并返回大于等于给定数字的最小整数。(向上取整)

console.log(Math.ceil(0.95));
//  1
console.log(Math.ceil(4));
// 4
console.log(Math.ceil(7.004));
//  8
console.log(Math.ceil(-7.004));
// -7

Math.random()

静态方法返回一个大于等于 0 且小于 1 的伪随机浮点数,并在该范围内近似均匀分布,然后你可以缩放到所需的范围。其实现将选择随机数生成算法的初始种子;它不能由用户选择或重置。(随机数)

  function getRandomInt(max) {
        return Math.floor(Math.random() * max);
    }
    console.log(getRandomInt(3));
    // 0, 1 or 2
    console.log(getRandomInt(1));
    //  0
    console.log(Math.random());
    // a number from 0 to <1

得到一个大于等于 0 小于 1 之间的随机数

function getRandom() {
  return Math.random();
}

得到一个两数之间的随机数

该示例返回一个在指定值之间的随机数。这个值不小于(且有可能等于)min,并且小于(且不等于)max

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

得到一个两数之间的随机整数

这个示例返回一个在指定值之间的随机整数。这个值不小于 min(如果 min 不是整数,则不小于 min 的向上取整数),且小于(但不等于)max

function getRandomInt(min, max) {
  const minCeiled = Math.ceil(min);
  const maxFloored = Math.floor(max);
  return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // 不包含最大值,包含最小值
}

得到一个两数之间的随机整数,包括两个数在内

虽然上面的 getRandomInt() 函数包含最小值,但不含最大值。如果你需要结果同时包含最小值和最大值怎么办?下面的 getRandomIntInclusive() 函数可以实现这一点。

function getRandomIntInclusive(min, max) {
  const minCeiled = Math.ceil(min);
  const maxFloored = Math.floor(max);
  return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // 包含最小值和最大值
}

Math.round()

函数返回一个数字四舍五入后最接近的整数。

x = Math.round(20.49); //20
x = Math.round(20.5); //21
x = Math.round(-20.5); //-20
x = Math.round(-20.51); //-21

Math.trunc()

方法会将数字的小数部分去掉,只保留整数部分。

Math.trunc(13.37); // 13
Math.trunc(42.84); // 42
Math.trunc(0.123); //  0
Math.trunc(-0.123); // -0
Math.trunc("-1.123"); // -1
Math.trunc(NaN); // NaN
Math.trunc("foo"); // NaN
Math.trunc(); // NaN

Math.sqrt()

函数返回一个数的平方根

function calcHypotenuse(a, b) {
  return Math.sqrt(a * a + b * b);
}
console.log(calcHypotenuse(3, 4));
// 5
console.log(calcHypotenuse(5, 12));
// 13
console.log(calcHypotenuse(0, 0));
// 0
​
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095
Math.sqrt(1);  // 1
Math.sqrt(0);  // 0
Math.sqrt(-1); // NaN
Math.sqrt(-0); // -0

Math.max()

函数返回作为输入参数的最大数字,如果没有参数,则返回 -Infinity

console.log(Math.max(1, 3, 2));
// 3
console.log(Math.max(-1, -3, -2));
//  -1
const array1 = [1, 3, 2];
console.log(Math.max(...array1));
// 3

获取数组的最大元素

Array.prototype.reduce() 可以用来查找最大值元素,通过比较每个值:

const arr = [1, 2, 3];
const max = arr.reduce((a, b) => Math.max(a, b), -Infinity);

下面的方法使用 Function.prototype.apply() 来获取数组的最大值。getMaxOfArray([1, 2, 3]) 相当于 Math.max(1, 2, 3),但是你可以使用 getMaxOfArray() 作用于任意长度的数组上。这应该只用于元素相对较少的数组。

function getMaxOfArray(numArray) {
  return Math.max.apply(null, numArray);
}

展开语法是编写 apply 解决方案的一种更简短的方法,可以最大限度地利用数组:

const arr = [1, 2, 3];
const max = Math.max(...arr);

Math.min()

函数返回作为输入参数的数字中最小的一个,如果没有参数,则返回 Infinity

console.log(Math.min(2, 3, 1));
// 1
console.log(Math.min(-2, -3, -1));
// -3
const array1 = [2, 3, 1];
console.log(Math.min(...array1));
// 1

使用 Math.min()

下例找出 xy 的最小值,并把它赋值给 z

const x = 10;
const y = -20;
const z = Math.min(x, y); // -20

使用 Math.min() 裁剪值

Math.min() 经常用于裁剪一个值,以便使其总是小于或等于某个边界值。例如:

let x = f(foo);
​
if (x > boundary) {
  x = boundary;
}
可以写成:

const x = Math.min(f(foo), boundary);

Math.log10()

函数返回一个数字以 10 为底的对数

Math.log10(10); // 1
Math.log10(100); // 2
Math.log10("100"); // 2
Math.log10(1); // 0
Math.log10(0); // -Infinity
Math.log10(-2); // NaN
Math.log10("foo"); // NaN

查看更多的方法,请参观

Math - JavaScript | MDN (mozilla.org)icon-default.png?t=N7T8https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math

标签:常用,console,log,min,max,abs,math,方法,Math
From: https://blog.csdn.net/weixin_72324200/article/details/139602744

相关文章

  • 使用使用rundll32 调用指定dll的方法
    使用使用rundll32调用指定dll的方法//顾名思义,"执行32位的DLL文件"。它的作用是执行DLL文件中的内部函数,这样在进程当中,只会有Rundll32.exe,而不会有DLL后门的进程,这样,就实现了进程上的隐藏。介绍一下Rundll32.exe这个文件,功能就是以命令行的方式调用动态链接程序库。系统中......
  • 爬取京东商品图片的Python实现方法
    引言在数据驱动的商业环境中,网络爬虫技术已成为获取信息的重要手段。京东作为中国领先的电商平台,拥有海量的商品信息和图片资源。本文将详细介绍如何使用Python编写爬虫程序,爬取京东商品的图片,并提供完整的代码实现过程。爬虫基础在开始编写爬虫之前,需要了解一些基本的网......
  • 【Go语言】面向对象编程(二):通过组合实现类的继承和方法重写
    通过组合实现类的继承和方法重写要实现面向对象的编程,就必须实现面向对象编程的三大特性:封装、继承和多态。1封装类的定义及其内部数据的定义可以看作是类的属性,基于类定义的函数方法则是类的成员方法。2继承Go语言中,没有直接提供继承相关的语法实现,可以通过组合......
  • Windows共享文件夹常见问题解决方法
    目录你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问允许自己电脑去访问局域网其他电脑的共享文件允许局域网内别人电脑访问自己电脑的共享文件你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问参考:https://blog.csdn.net/qq28574......
  • RT-thread 运维方法选择(基于开源组件)
    方案选择,需要支持4G和WIFI模组链接工具包优点缺点备注rtty1.功能全,兼容linux1.需要搭建一个服务器2.无成熟软件包支持嵌入式设备 webclient1.兼容性好,代码少。1.需要搭建一个服务器2.基于客户端做业务 webnet1.官方支持,兼容性好。1.需......
  • 3个将ofd电子发票转成word文档的方法
    OFD(OpenFileDescription)是一种电子文件格式,常用于存储重要的文档。然而,有时我们需要将OFD文件转换成word格式以便进行修改。那么ofd文件如何转换成word格式呢?本文将介绍三种将OFD文件转换成word格式的方法,帮助您轻松实现格式转换。方法一:使用在线转换工具在线转换工具是一种方......
  • C# 字段 属性 方法 构造函数 索引器 事件 嵌套类型 常量 运算符重载
    字段声明字段字段初始化静态字段常量字段只读字段字段的访问然而属性声明属性自动实现的属性只读属性只写属性属性的逻辑处理属性的访问修饰符属性和字段的区别属性的用途总结索引器索引器的基本语法使用索引器索引器的关键点语法参数访问和设置异常处理性能重载使用......
  • A Twisted Path to Renown联机报错/无法联机的解决方法
    成名之路/ATwistedPathtoRenown这款游戏的游戏背景是美国西部,包含了PvE、PvP成分,并且比较有意思的一点是,由于成名之路旨在还原年代感和真实感,所以玩家基本上没有全自动的武器道具或者能连发的,基本就是单发武器,也有弓箭可以选择。这款游戏也是比较像猎杀对决和塔科夫,目前游......
  • vue3 高德安徽省边界 密钥必须添加否则会出现无法使用DistrictSearch的方法也不报错
    <template> <divclass="centermap"ref="mapContainer"></div></template><scriptsetuplang="ts">import{ref,onMounted}from'vue';importAMapLoaderfrom'@amap/amap-jsapi-l......
  • antdv弹窗modal可拖动方法
    this.$nextTick(()=>{constmodal:any=document.getElementsByClassName('ant-modal')[0]constcontent:any=document.getElementsByClassName('ant-modal-content')[0]letleft=0lettop=0......