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 数学对象备忘单的详细内容,更多请关注【创想鸟】其它相关文章!

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

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2648496.html

(0)
上一篇 2025年3月7日 08:13:52
下一篇 2025年2月25日 13:38:11

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 如何使用计算器:完整指南

    计算器是日常生活和数学、科学、工程和金融等各个知识领域的必备工具。随着技术的发展,计算器已经从简单的手持设备发展成为复杂的数字应用程序。下面,我们从基本模型到最高级的模型,解释如何正确使用计算器。 1. 了解计算器的类型 计算器有很多种类型…

    2025年3月7日
    200
  • NgSysV响应式/自适应设计

    该帖子系列已在 ngatesystems.com 上建立索引。您还可以在那里找到超级有用的关键字搜索工具。 最后评论:24 年 11 月 一、简介 帖子 4.2 透露,如果您希望您的 web 应用程序出现在网络搜索中,您必须确保: 您的网络…

    2025年3月7日
    200
  • 带有 UI 的 Github 图像托管 API

    该项目是使用 github 存储库作为图像托管服务的概念证明。该 api 允许用户从存储库上传、列出和删除图像文件,提供了一个简单的界面来管理 github 上托管的文件。 github:masfana/github-image-bucke…

    2025年3月7日
    200
  • 渐进式 Web 应用程序:现代 Web 开发的终极指南

    渐进式 Web 应用程序:概述渐进式 Web 应用程序是可以使用离线缓存安装的独立应用程序。它们可以安装在单个代码库上的所有设备上,为您提供类似本机的体验。它们于 2016 年推出,是作为特定于设备的应用程序的替代方案而构建的,但现在可以在…

    2025年3月7日
    200
  • 在 React 中为 graphQL 请求设置 Apollo 客户端

    介绍 本文将演示如何使用 apolloclient 库为 graphql 请求设置 react 应用程序。目标是展示如何配置应用程序并提供如何发出请求的示例。 库 @apollo/client:启用状态管理并发出 graphql 请求的库g…

    2025年3月7日
    200
  • React:ReCAPTCHA vlient 和服务器演示

    在此演示中,我将在基于 next.js 构建的 react 应用程序中使用 google recaptcha v3 凭据。 recaptcha 令牌将在客户端生成并在服务器端验证。 链接 演示 代码库 第 1 步:生成您的 recaptch…

    2025年3月7日
    200
  • Chunk-Busters:不要跨越溪流!

    ⚠️ 如果您有光敏性,您可能想跳过此操作。请参阅下面的静态图片,这些灯将开始快速闪烁! 互联网如何运作? 记住标题……我们在这里讨论的是流。 我可以谈论协议、数据包、排序、acks 和 nacks…但我们在这里谈论流,正如你可能猜对了(我相…

    编程技术 2025年3月7日
    200
  • 顶级笔-来源 Nextjs Boilerplates/Starter

    next.js 是一个构建在 node.js 之上的开源 web 开发框架,支持基于 react 的 web 应用程序功能,例如服务器端渲染和生成静态网站。 虽然Next.js。官方允许我们通过 npx create-next-app@la…

    2025年3月7日 编程技术
    200
  • 在 Nodejs 中进行身份验证的正确方法 [uide]

    身份验证是后端开发中最关键但经常被误解的方面之一。由于其复杂性,开发人员经常转向第三方解决方案,例如 auth0 或 supabase。虽然这些都是优秀的工具,但构建您自己的身份验证系统可以提供更大的灵活性和控制力。 在本指南中,您将了解如…

    2025年3月7日
    200
  • 表单事件绑定在 KnockoutJs 中如何工作

    此内容基本上是原始材料的翻译。目的是了解 magento 2 的 knockoutjs 并用葡萄牙语创建有关 knockoujs 的内容。 文档 数据绑定语法绑定上下文“点击”绑定“事件”绑定“提交”绑定“启用”和“禁用”绑定“值”绑定“t…

    2025年3月7日
    200

发表回复

登录后才能评论