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 examplemath.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 examplemath.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 examplemath.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 examplemath.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 examplemath.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 examplemath.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 examplemath.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