0

0

JavaScript 数学对象备忘单

心靈之曲

心靈之曲

发布时间:2024-11-25 12:23:19

|

1178人浏览过

|

来源于dev.to

转载

javascript 数学对象备忘单

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


属性

math 对象有一组常量:

有一团购
有一团购

有一团是咱老陕自己的团购网站,是由几个大学毕业生自己筹备组建的。我们怀抱着梦想,为咱老陕每一位消费者每天提供一单精品消费,精选餐厅、酒吧、KTV、SPA、美发店、瑜伽馆等特色商家,只要凑够最低团购人数就能享受无敌折扣。我们希望,我们的有一团能够走出西安,走出陕西,走向中国每一个角落,我们希望听到每一位有一团的会员对我们有一团的评价就是——聊咋咧!

下载
property description value (approx.)
math.e euler's number 2.718
math.ln2 natural logarithm of 2 0.693
math.ln10 natural logarithm of 10 2.302
math.log2e base 2 logarithm of math.e 1.442
math.log10e base 10 logarithm of math.e 0.434
math.pi ratio of a circle's circumference to its diameter 3.14159
math.sqrt1_2 square root of 1/2 0.707
math.sqrt2 square root of 2 1.414

方法

1.舍入方法

method description example
math.round(x) rounds to the nearest integer math.round(4.5) → 5
math.floor(x) rounds down to the nearest integer math.floor(4.7) → 4
math.ceil(x) rounds up to the nearest integer math.ceil(4.1) → 5
math.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.53
custom random int generator random integer between min and max math.floor(math.random() * (max - min 1)) min

3.算术方法

method description example
math.abs(x) absolute value math.abs(-7) → 7
math.pow(x, y) raises x to the power of y math.pow(2, 3) → 8
math.sqrt(x) square root of x math.sqrt(16) → 4
math.cbrt(x) cube root of x math.cbrt(27) → 3
math.hypot(...values) square root of the sum of squares of arguments math.hypot(3, 4) → 5

4.指数和对数方法

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

5.三角函数

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

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

method description example
math.max(...values) returns the largest value math.max(5, 10, 15) → 15
math.min(...values) returns the smallest value math.min(5, 10, 15) → 5
custom clamping restrict a value to a range math.min(math.max(x, min), max)

7.其他方法

method description example
math.sign(x) returns 1, -1, or 0 based on sign of x math.sign(-10) → -1
math.fround(x) nearest 32-bit floating-point number math.fround(5.5) → 5.5
math.clz32(x) counts leading zero bits in 32-bit binary math.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 places
console.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)); // 92
console.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^2
const time = 3; // seconds
const 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)); // 15
console.log(clamp(5, 10, 20));  // 10

将负数转换为正数

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

查找数字的整数部分

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

8.解决问题

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

function ispoweroftwo(n) {
    return math.log2(n) % 1 === 0;
}
console.log(ispoweroftwo(8)); // true
console.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

相关专题

更多
js获取数组长度的方法
js获取数组长度的方法

在js中,可以利用array对象的length属性来获取数组长度,该属性可设置或返回数组中元素的数目,只需要使用“array.length”语句即可返回表示数组对象的元素个数的数值,也就是长度值。php中文网还提供JavaScript数组的相关下载、相关课程等内容,供大家免费下载使用。

552

2023.06.20

js刷新当前页面
js刷新当前页面

js刷新当前页面的方法:1、reload方法,该方法强迫浏览器刷新当前页面,语法为“location.reload([bForceGet]) ”;2、replace方法,该方法通过指定URL替换当前缓存在历史里(客户端)的项目,因此当使用replace方法之后,不能通过“前进”和“后退”来访问已经被替换的URL,语法为“location.replace(URL) ”。php中文网为大家带来了js刷新当前页面的相关知识、以及相关文章等内容

374

2023.07.04

js四舍五入
js四舍五入

js四舍五入的方法:1、tofixed方法,可把 Number 四舍五入为指定小数位数的数字;2、round() 方法,可把一个数字舍入为最接近的整数。php中文网为大家带来了js四舍五入的相关知识、以及相关文章等内容

730

2023.07.04

js删除节点的方法
js删除节点的方法

js删除节点的方法有:1、removeChild()方法,用于从父节点中移除指定的子节点,它需要两个参数,第一个参数是要删除的子节点,第二个参数是父节点;2、parentNode.removeChild()方法,可以直接通过父节点调用来删除子节点;3、remove()方法,可以直接删除节点,而无需指定父节点;4、innerHTML属性,用于删除节点的内容。

475

2023.09.01

JavaScript转义字符
JavaScript转义字符

JavaScript中的转义字符是反斜杠和引号,可以在字符串中表示特殊字符或改变字符的含义。本专题为大家提供转义字符相关的文章、下载、课程内容,供大家免费下载体验。

394

2023.09.04

js生成随机数的方法
js生成随机数的方法

js生成随机数的方法有:1、使用random函数生成0-1之间的随机数;2、使用random函数和特定范围来生成随机整数;3、使用random函数和round函数生成0-99之间的随机整数;4、使用random函数和其他函数生成更复杂的随机数;5、使用random函数和其他函数生成范围内的随机小数;6、使用random函数和其他函数生成范围内的随机整数或小数。

990

2023.09.04

如何启用JavaScript
如何启用JavaScript

JavaScript启用方法有内联脚本、内部脚本、外部脚本和异步加载。详细介绍:1、内联脚本是将JavaScript代码直接嵌入到HTML标签中;2、内部脚本是将JavaScript代码放置在HTML文件的`<script>`标签中;3、外部脚本是将JavaScript代码放置在一个独立的文件;4、外部脚本是将JavaScript代码放置在一个独立的文件。

656

2023.09.12

Js中Symbol类详解
Js中Symbol类详解

javascript中的Symbol数据类型是一种基本数据类型,用于表示独一无二的值。Symbol的特点:1、独一无二,每个Symbol值都是唯一的,不会与其他任何值相等;2、不可变性,Symbol值一旦创建,就不能修改或者重新赋值;3、隐藏性,Symbol值不会被隐式转换为其他类型;4、无法枚举,Symbol值作为对象的属性名时,默认是不可枚举的。

551

2023.09.20

c++主流开发框架汇总
c++主流开发框架汇总

本专题整合了c++开发框架推荐,阅读专题下面的文章了解更多详细内容。

80

2026.01.09

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
10分钟--Midjourney创作自己的漫画
10分钟--Midjourney创作自己的漫画

共1课时 | 0.1万人学习

Midjourney 关键词系列整合
Midjourney 关键词系列整合

共13课时 | 0.9万人学习

AI绘画教程
AI绘画教程

共2课时 | 0.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号