JavaScript 数学对象备忘单

javascript 数学对象备忘单

javascript 中的 math 对象提供了一组用于执行数学任务的属性和方法。这是 math 对象的综合备忘单。

属性

math 对象有一组常量:

property description value (approx.)

math.eeuler’s number2.718math.ln2natural logarithm of 20.693math.ln10natural logarithm of 102.302math.log2ebase 2 logarithm of math.e1.442math.log10ebase 10 logarithm of math.e0.434math.piratio of a circle’s circumference to its diameter3.14159math.sqrt1_2square root of 1/20.707math.sqrt2square root of 21.414

方法

1.舍入方法

method description example

math.round(x)rounds to the nearest integermath.round(4.5) → 5math.floor(x)rounds down to the nearest integermath.floor(4.7) → 4math.ceil(x)rounds up to the nearest integermath.ceil(4.1) → 5math.trunc(x)removes the decimal part (truncates)math.trunc(4.9) → 4

2.随机数生成

method description example

math.random()generates a random number between 0 and 1 (exclusive)math.random() → 0.53custom random int generatorrandom integer between min and maxmath.floor(math.random() * (max – min 1)) min

3.算术方法

method description example

math.abs(x)absolute valuemath.abs(-7) → 7math.pow(x, y)raises x to the power of ymath.pow(2, 3) → 8math.sqrt(x)square root of xmath.sqrt(16) → 4math.cbrt(x)cube root of xmath.cbrt(27) → 3math.hypot(…values)square root of the sum of squares of argumentsmath.hypot(3, 4) → 5

4.指数和对数方法

method description example

math.exp(x)e^xmath.exp(1) → 2.718math.log(x)natural logarithm (ln(x))math.log(10) → 2.302math.log2(x)base 2 logarithm of xmath.log2(8) → 3math.log10(x)base 10 logarithm of xmath.log10(100) → 2

5.三角函数

method description example

math.sin(x)sine of x (x in radians)math.sin(math.pi / 2) → 1math.cos(x)cosine of x (x in radians)math.cos(0) → 1math.tan(x)tangent of x (x in radians)math.tan(math.pi / 4) → 1math.asin(x)arcsine of x (returns radians)math.asin(1) → 1.57math.acos(x)arccosine of xmath.acos(1) → 0math.atan(x)arctangent of xmath.atan(1) → 0.785math.atan2(y, x)arctangent of y / xmath.atan2(1, 1) → 0.785

6.最小值、最大值和钳位

method description example

math.max(…values)returns the largest valuemath.max(5, 10, 15) → 15math.min(…values)returns the smallest valuemath.min(5, 10, 15) → 5custom clampingrestrict a value to a rangemath.min(math.max(x, min), max)

7.其他方法

method description example

math.sign(x)returns 1, -1, or 0 based on sign of xmath.sign(-10) → -1math.fround(x)nearest 32-bit floating-point numbermath.fround(5.5) → 5.5math.clz32(x)counts leading zero bits in 32-bit binarymath.clz32(1) → 31

示例

1 到 100 之间的随机整数

const randomint = math.floor(math.random() * 100) + 1;console.log(randomint);

计算圆面积

const radius = 5;const area = math.pi * math.pow(radius, 2);console.log(area); // 78.54

将度数转换为弧度

const degrees = 90;const radians = degrees * (math.pi / 180);console.log(radians); // 1.57

找到数组中最大的数字

const nums = [5, 3, 9, 1];console.log(math.max(...nums)); // 9

数学对象的扩展用例

math 对象有许多实际应用。以下列出了常见场景和示例,以说明如何有效使用它。

1.随机化

生成一个范围内的随机整数

function getrandomint(min, max) {    return math.floor(math.random() * (max - min + 1)) + min;}console.log(getrandomint(1, 10)); // random number between 1 and 10

打乱数组

function shufflearray(arr) {    return arr.sort(() => math.random() - 0.5);}console.log(shufflearray([1, 2, 3, 4, 5])); // shuffled array

模拟掷骰子

function rolldice() {    return math.floor(math.random() * 6) + 1; // random number between 1 and 6}console.log(rolldice());

2.几何和形状

计算圆的面积

const radius = 5;const area = math.pi * math.pow(radius, 2);console.log(area); // 78.54

计算三角形的斜边

const a = 3, b = 4;const hypotenuse = math.hypot(a, b);console.log(hypotenuse); // 5

将度数转换为弧度

function degreestoradians(degrees) {    return degrees * (math.pi / 180);}console.log(degreestoradians(90)); // 1.57

3.金融与商业

复利公式

function compoundinterest(principal, rate, time, n) {    return principal * math.pow((1 + rate / n), n * time);}console.log(compoundinterest(1000, 0.05, 10, 12)); // $1647.01

四舍五入货币值

const amount = 19.56789;const rounded = math.round(amount * 100) / 100; // round to 2 decimal placesconsole.log(rounded); // 19.57

计算折扣

function calculatediscount(price, discount) {    return math.floor(price * (1 - discount / 100));}console.log(calculatediscount(200, 15)); // $170

4.游戏和动画

模拟抛硬币

function cointoss() {    return math.random() < 0.5 ? 'heads' : 'tails';}console.log(cointoss());

平滑动画的缓动函数

function easeoutquad(t) {    return t * (2 - t); // simple easing function}console.log(easeoutquad(0.5)); // 0.75

二维网格中的随机生成坐标

function randomcoordinates(gridsize) {    const x = math.floor(math.random() * gridsize);    const y = math.floor(math.random() * gridsize);    return { x, y };}console.log(randomcoordinates(10)); // e.g., {x: 7, y: 2}

5.数据分析

求数组中的最大值和最小值

const scores = [85, 90, 78, 92, 88];console.log(math.max(...scores)); // 92console.log(math.min(...scores)); // 78

标准化数据

function normalize(value, min, max) {    return (value - min) / (max - min);}console.log(normalize(75, 0, 100)); // 0.75

6.物理与工程

计算自由落体后的速度

const gravity = 9.8; // m/s^2const time = 3; // secondsconst velocity = gravity * time;console.log(velocity); // 29.4 m/s

钟摆周期

function pendulumperiod(length) {    return 2 * math.pi * math.sqrt(length / 9.8);}console.log(pendulumperiod(1)); // 2.006 seconds

7.数字操纵

将数字限制在某个范围内

function clamp(value, min, max) {    return math.min(math.max(value, min), max);}console.log(clamp(15, 10, 20)); // 15console.log(clamp(5, 10, 20));  // 10

将负数转换为正数

console.log(math.abs(-42)); // 42

查找数字的整数部分

console.log(math.trunc(4.9)); // 4console.log(math.trunc(-4.9)); // -4

8.解决问题

检查一个数字是否是 2 的幂

function ispoweroftwo(n) {    return math.log2(n) % 1 === 0;}console.log(ispoweroftwo(8)); // trueconsole.log(ispoweroftwo(10)); // false

生成斐波那契数

function fibonacci(n) {    const phi = (1 + math.sqrt(5)) / 2;    return math.round((math.pow(phi, n) - math.pow(-phi, -n)) / math.sqrt(5));}console.log(fibonacci(10)); // 55

9.杂项

生成随机颜色 (rgb)

function getrandomcolor() {    const r = math.floor(math.random() * 256);    const g = math.floor(math.random() * 256);    const b = math.floor(math.random() * 256);    return `rgb(${r}, ${g}, ${b})`;}console.log(getrandomcolor()); // e.g., rgb(123, 45, 67)

根据出生日期计算年龄

function calculateAge(birthYear) {    const currentYear = new Date().getFullYear();    return currentYear - birthYear;}console.log(calculateAge(1990)); // e.g., 34

以上就是JavaScript 数学对象备忘单的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1498962.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月19日 20:55:15
下一篇 2025年12月19日 20:55:25

相关推荐

  • Uniapp 中如何不拉伸不裁剪地展示图片?

    灵活展示图片:如何不拉伸不裁剪 在界面设计中,常常需要以原尺寸展示用户上传的图片。本文将介绍一种在 uniapp 框架中实现该功能的简单方法。 对于不同尺寸的图片,可以采用以下处理方式: 极端宽高比:撑满屏幕宽度或高度,再等比缩放居中。非极端宽高比:居中显示,若能撑满则撑满。 然而,如果需要不拉伸不…

    2025年12月24日
    400
  • 如何让小说网站控制台显示乱码,同时网页内容正常显示?

    如何在不影响用户界面的情况下实现控制台乱码? 当在小说网站上下载小说时,大家可能会遇到一个问题:网站上的文本在网页内正常显示,但是在控制台中却是乱码。如何实现此类操作,从而在不影响用户界面(UI)的情况下保持控制台乱码呢? 答案在于使用自定义字体。网站可以通过在服务器端配置自定义字体,并通过在客户端…

    2025年12月24日
    800
  • 如何在地图上轻松创建气泡信息框?

    地图上气泡信息框的巧妙生成 地图上气泡信息框是一种常用的交互功能,它简便易用,能够为用户提供额外信息。本文将探讨如何借助地图库的功能轻松创建这一功能。 利用地图库的原生功能 大多数地图库,如高德地图,都提供了现成的信息窗体和右键菜单功能。这些功能可以通过以下途径实现: 高德地图 JS API 参考文…

    2025年12月24日
    400
  • 如何使用 scroll-behavior 属性实现元素scrollLeft变化时的平滑动画?

    如何实现元素scrollleft变化时的平滑动画效果? 在许多网页应用中,滚动容器的水平滚动条(scrollleft)需要频繁使用。为了让滚动动作更加自然,你希望给scrollleft的变化添加动画效果。 解决方案:scroll-behavior 属性 要实现scrollleft变化时的平滑动画效果…

    2025年12月24日
    000
  • 如何为滚动元素添加平滑过渡,使滚动条滑动时更自然流畅?

    给滚动元素平滑过渡 如何在滚动条属性(scrollleft)发生改变时为元素添加平滑的过渡效果? 解决方案:scroll-behavior 属性 为滚动容器设置 scroll-behavior 属性可以实现平滑滚动。 html 代码: click the button to slide right!…

    2025年12月24日
    500
  • 如何选择元素个数不固定的指定类名子元素?

    灵活选择元素个数不固定的指定类名子元素 在网页布局中,有时需要选择特定类名的子元素,但这些元素的数量并不固定。例如,下面这段 html 代码中,activebar 和 item 元素的数量均不固定: *n *n 如果需要选择第一个 item元素,可以使用 css 选择器 :nth-child()。该…

    2025年12月24日
    200
  • 使用 SVG 如何实现自定义宽度、间距和半径的虚线边框?

    使用 svg 实现自定义虚线边框 如何实现一个具有自定义宽度、间距和半径的虚线边框是一个常见的前端开发问题。传统的解决方案通常涉及使用 border-image 引入切片图片,但是这种方法存在引入外部资源、性能低下的缺点。 为了避免上述问题,可以使用 svg(可缩放矢量图形)来创建纯代码实现。一种方…

    2025年12月24日
    100
  • 旋转长方形后,如何计算其相对于画布左上角的轴距?

    绘制长方形并旋转,计算旋转后轴距 在拥有 1920×1080 画布中,放置一个宽高为 200×20 的长方形,其坐标位于 (100, 100)。当以任意角度旋转长方形时,如何计算它相对于画布左上角的 x、y 轴距? 以下代码提供了一个计算旋转后长方形轴距的解决方案: const x = 200;co…

    2025年12月24日
    000
  • 旋转长方形后,如何计算它与画布左上角的xy轴距?

    旋转后长方形在画布上的xy轴距计算 在画布中添加一个长方形,并将其旋转任意角度,如何计算旋转后的长方形与画布左上角之间的xy轴距? 问题分解: 要计算旋转后长方形的xy轴距,需要考虑旋转对长方形宽高和位置的影响。首先,旋转会改变长方形的长和宽,其次,旋转会改变长方形的中心点位置。 求解方法: 计算旋…

    2025年12月24日
    000
  • 旋转长方形后如何计算其在画布上的轴距?

    旋转长方形后计算轴距 假设长方形的宽、高分别为 200 和 20,初始坐标为 (100, 100),我们将它旋转一个任意角度。根据旋转矩阵公式,旋转后的新坐标 (x’, y’) 可以通过以下公式计算: x’ = x * cos(θ) – y * sin(θ)y’ = x * …

    2025年12月24日
    000
  • 如何让“元素跟随文本高度,而不是撑高父容器?

    如何让 元素跟随文本高度,而不是撑高父容器 在页面布局中,经常遇到父容器高度被子元素撑开的问题。在图例所示的案例中,父容器被较高的图片撑开,而文本的高度没有被考虑。本问答将提供纯css解决方案,让图片跟随文本高度,确保父容器的高度不会被图片影响。 解决方法 为了解决这个问题,需要将图片从文档流中脱离…

    2025年12月24日
    000
  • 如何计算旋转后长方形在画布上的轴距?

    旋转后长方形与画布轴距计算 在给定的画布中,有一个长方形,在随机旋转一定角度后,如何计算其在画布上的轴距,即距离左上角的距离? 以下提供一种计算长方形相对于画布左上角的新轴距的方法: const x = 200; // 初始 x 坐标const y = 90; // 初始 y 坐标const w =…

    2025年12月24日
    200
  • CSS元素设置em和transition后,为何载入页面无放大效果?

    css元素设置em和transition后,为何载入无放大效果 很多开发者在设置了em和transition后,却发现元素载入页面时无放大效果。本文将解答这一问题。 原问题:在视频演示中,将元素设置如下,载入页面会有放大效果。然而,在个人尝试中,并未出现该效果。这是由于macos和windows系统…

    2025年12月24日
    200
  • 为什么 CSS mask 属性未请求指定图片?

    解决 css mask 属性未请求图片的问题 在使用 css mask 属性时,指定了图片地址,但网络面板显示未请求获取该图片,这可能是由于浏览器兼容性问题造成的。 问题 如下代码所示: 立即学习“前端免费学习笔记(深入)”; icon [data-icon=”cloud”] { –icon-cl…

    2025年12月24日
    200
  • 如何利用 CSS 选中激活标签并影响相邻元素的样式?

    如何利用 css 选中激活标签并影响相邻元素? 为了实现激活标签影响相邻元素的样式需求,可以通过 :has 选择器来实现。以下是如何具体操作: 对于激活标签相邻后的元素,可以在 css 中使用以下代码进行设置: li:has(+li.active) { border-radius: 0 0 10px…

    2025年12月24日
    100
  • 如何模拟Windows 10 设置界面中的鼠标悬浮放大效果?

    win10设置界面的鼠标移动显示周边的样式(探照灯效果)的实现方式 在windows设置界面的鼠标悬浮效果中,光标周围会显示一个放大区域。在前端开发中,可以通过多种方式实现类似的效果。 使用css 使用css的transform和box-shadow属性。通过将transform: scale(1.…

    2025年12月24日
    200
  • 如何计算旋转后的长方形在画布上的 XY 轴距?

    旋转长方形后计算其画布xy轴距 在创建的画布上添加了一个长方形,并提供其宽、高和初始坐标。为了视觉化旋转效果,还提供了一些旋转特定角度后的图片。 问题是如何计算任意角度旋转后,这个长方形的xy轴距。这涉及到使用三角学来计算旋转后的坐标。 以下是一个 javascript 代码示例,用于计算旋转后长方…

    2025年12月24日
    000
  • 为什么我的 Safari 自定义样式表在百度页面上失效了?

    为什么在 Safari 中自定义样式表未能正常工作? 在 Safari 的偏好设置中设置自定义样式表后,您对其进行测试却发现效果不同。在您自己的网页中,样式有效,而在百度页面中却失效。 造成这种情况的原因是,第一个访问的项目使用了文件协议,可以访问本地目录中的图片文件。而第二个访问的百度使用了 ht…

    2025年12月24日
    000
  • 如何用前端实现 Windows 10 设置界面的鼠标移动探照灯效果?

    如何在前端实现 Windows 10 设置界面中的鼠标移动探照灯效果 想要在前端开发中实现 Windows 10 设置界面中类似的鼠标移动探照灯效果,可以通过以下途径: CSS 解决方案 DEMO 1: Windows 10 网格悬停效果:https://codepen.io/tr4553r7/pe…

    2025年12月24日
    000
  • 使用CSS mask属性指定图片URL时,为什么浏览器无法加载图片?

    css mask属性未能加载图片的解决方法 使用css mask属性指定图片url时,如示例中所示: mask: url(“https://api.iconify.design/mdi:apple-icloud.svg”) center / contain no-repeat; 但是,在网络面板中却…

    2025年12月24日
    000

发表回复

登录后才能评论
关注微信