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

相关推荐

  • WebAssembly中导入JavaScript函数:无胶水代码集成指南

    本文深入探讨了在WebAssembly模块中直接导入和使用JavaScript函数的机制,特别是当使用Emscripten的STANDALONE_WASM和SIDE_MODULE编译模式时。文章详细分析了TypeError: import object field ‘GOT.mem&#8…

    2025年12月20日
    000
  • React Native表单验证:实现实时错误消息显示

    本教程详细阐述了如何在React Native应用中,利用React Context API和Styled Components,实现表单字段(如邮箱)的实时验证及错误消息显示。文章通过具体代码示例,指导开发者如何将验证逻辑与UI组件有效结合,确保用户输入时即时获得反馈,从而提升用户体验。 引言:R…

    2025年12月20日
    000
  • 深入理解React状态管理:解决map is not a function错误

    本文深入探讨了React类组件中常见的TypeError: this.state.articles.map is not a function错误。该错误通常源于组件状态的初始值类型与后续操作不匹配。文章详细分析了React组件生命周期中constructor、render和componentDid…

    2025年12月20日
    000
  • React Native表单:实现输入框级别的实时错误消息显示

    本教程详细讲解如何在React Native应用中,利用React Context和组件化思想,为表单输入框(特别是邮件地址)实现实时、精准的错误消息显示。我们将探讨如何管理验证状态,并通过自定义输入组件将错误信息直观地呈现给用户,提升用户体验。 1. 引言:React Native表单错误处理的挑…

    2025年12月20日
    000
  • React Native表单实时错误提示:实现邮箱格式验证与显示

    本教程详细阐述如何在React Native应用中实现实时的表单输入验证,特别是邮箱格式验证,并向用户显示具体的错误提示。文章将深入探讨如何利用React Context API管理验证逻辑和错误状态,以及如何改造自定义输入组件(如AuthInput)以接收并渲染字段级的错误信息,从而提供即时、友好…

    2025年12月20日
    000
  • 解决Next.js useSession 错误:清理.next 缓存的实践指南

    本文深入探讨了Next.js应用中useSession钩子报错“useSession must be wrapped in a ”的问题,即使代码结构看似正确。教程分析了next-auth的会话提供者机制,并指出该错误在正确配置下仍可能出现的原因,最终提供了清除.next缓存作为核心解决方案,并辅以…

    2025年12月20日 好文分享
    000
  • JavaScript 多按钮控制图片切换:灵活实现与最佳实践

    本教程详细介绍了如何使用JavaScript实现多个按钮控制网页图片切换的功能。文章首先回顾了单个按钮的实现方式,进而探讨了两种多按钮场景:一是多个按钮触发相同的图片变化,通过类选择器和querySelectorAll实现;二是每个按钮触发不同的图片变化,利用HTML data-* 属性传递动态参数…

    2025年12月20日 好文分享
    000
  • JavaScript中处理多按钮事件与动态图片切换指南

    本教程详细介绍了如何在JavaScript中优雅地处理多个按钮触发图片切换的场景。我们将探讨两种主要策略:一是当多个按钮需要触发相同的图片变化时,如何通过共享类和 querySelectorAll 进行事件绑定;二是如何利用HTML data-* 属性,使每个按钮能够触发不同的图片变化,实现更灵活的…

    2025年12月20日
    000
  • JavaScript 高效处理多按钮事件:从共享行为到动态内容切换

    本教程探讨了在JavaScript中高效管理多个按钮事件的策略。首先,介绍如何通过为按钮添加通用类并结合querySelectorAll和forEach方法,实现多个按钮触发相同功能。接着,深入讲解如何利用HTML的data-*属性,为每个按钮传递特定的数据,从而实现动态内容(如图片)的切换。文章旨…

    2025年12月20日 好文分享
    000
  • JavaScript中事件循环和网络请求的关系

    网络请求不会阻塞javascript主线程,是因为其由浏览器web api异步处理,完成后回调通过事件循环调度。具体来说,1. 网络请求如fetch或xmlhttprequest被委托给浏览器底层模块,2. 请求完成后,回调被放入任务队列:promise回调入微任务队列,xmlhttprequest…

    2025年12月20日 好文分享
    000
  • 前端开发:利用JavaScript和HTML数据属性实现多按钮图片动态切换

    本文详细介绍了如何使用JavaScript处理多个按钮的点击事件,以实现动态图片切换功能。首先,探讨了通过统一类名和querySelectorAll方法为多个按钮绑定相同行为的策略。接着,进一步讲解了如何利用HTML的data-*属性,使每个按钮能够控制图片切换到不同的目标源,从而实现更灵活的交互效…

    2025年12月20日 好文分享
    000
  • JavaScript的String.prototype.match方法是什么?怎么用?

    match()方法用于在字符串中搜索匹配正则表达式的内容并返回结果;1.若正则表达式带g标志,match()返回所有完整匹配项的数组;2.若无g标志,则返回第一个匹配及其捕获组等详细信息的对象;3.若未找到任何匹配项,返回null而非空数组;4.match()与exec()的区别在于match()适…

    2025年12月20日 好文分享
    000
  • JavaScript中如何利用事件循环处理大任务

    javascript中处理大任务的核心策略是将任务拆分为小块并利用事件循环实现异步执行,避免主线程阻塞。1. 使用settimeout(fn, 0)将任务分片,每执行完一小块后让出主线程,使浏览器有机会处理渲染和用户事件;2. 使用requestanimationframe进行与视觉更新同步的任务分…

    2025年12月20日 好文分享
    000
  • Azure Blob 存储 SAS 令牌生成及签名不匹配问题排查

    本文档旨在帮助开发者解决在使用 JavaScript 生成 Azure Blob 存储的共享访问签名 (SAS) 令牌时遇到的签名不匹配问题。通过本文,你将了解如何正确构建签名字符串,并生成有效的 SAS 令牌,从而成功访问 Azure Blob 存储资源。 理解 Azure SAS 令牌 Azur…

    2025年12月20日
    000
  • Azure Blob 存储 SAS 令牌生成及签名错误排查指南

    本文旨在帮助开发者理解如何使用 JavaScript 生成 Azure Blob 存储的共享访问签名 (SAS) 令牌,并解决常见的签名不匹配错误。通过本文,你将了解 SAS 令牌的构成、签名字符串的生成方法,以及如何避免常见的错误配置,从而成功生成可用的 SAS URL。 理解 Azure Blo…

    2025年12月20日
    000
  • 实现 Bootstrap Select 中 Optgroup 的多选限制

    本文介绍了如何在使用 Bootstrap Select 插件时,实现只允许 Optgroup 中的选项多选,而普通选项与 Optgroup 选项互斥的特殊需求。通过监听 change 事件,判断选中项的类型,并动态调整其他选项的选中状态,最终达到预期的效果。本文将提供详细的代码示例和实现思路,帮助开…

    2025年12月20日
    000
  • 实现 Bootstrap Select 仅 Optgroup 多选的技巧

    本文旨在解决在使用 Bootstrap Select 插件时,如何实现仅允许 optgroup 中的选项进行多选,而普通 option 选项与 optgroup 选项互斥选择的问题。通过监听 change 事件并动态控制选项的 selected 属性,提供了一种可行的解决方案,并附带示例代码,方便开…

    2025年12月20日
    000
  • 使用 Bootstrap Select 实现 Optgroup 多选限制

    本文旨在介绍如何使用 Bootstrap Select 插件,实现仅允许 Optgroup 中的选项进行多选,而普通选项和 Optgroup 选项互斥的选择效果。我们将通过监听 change 事件,动态控制选项的选中状态,最终实现预期的交互行为。 实现原理 核心思路是监听 select 元素的 ch…

    2025年12月20日
    000
  • 在React中正确处理和获取Select下拉框的值

    本教程详细讲解了在React应用中如何正确管理和获取select下拉框的值。我们将深入探讨React中受控组件的概念,分析在渲染option标签时常见的错误,并提供正确的代码实现和最佳实践,确保select元素的值能够准确地绑定到组件状态并响应用户交互,从而解决e.target.value无法正确获…

    2025年12月20日
    000
  • React 中获取 Select 元素值的正确方法

    本文旨在解决 React 应用中获取 元素值时遇到的问题。通过分析常见的错误原因,提供清晰的代码示例,并详细解释如何正确地处理 onChange 事件,最终帮助开发者轻松获取下拉菜单的选中值,并将其应用于状态管理或其他业务逻辑中。 在 React 中,获取 元素的值通常涉及到监听 onChange …

    2025年12月20日
    000

发表回复

登录后才能评论
关注微信