
javascript 中的 math 对象提供了一组用于执行数学任务的属性和方法。这是 math 对象的综合备忘单。
math 对象有一组常量:
Perl 基础入门中文教程,chm格式,讲述PERL概述、简单变量、操作符、列表和数组变量、文件读写、模式匹配、控制结构、子程序、关联数组/哈希表、格式化输出、文件系统、引用、面向对象、包和模块等知识点。适合初学者阅读和了解Perl脚本语言。
0
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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) |
| 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 |
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 对象有许多实际应用。以下列出了常见场景和示例,以说明如何有效使用它。
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());
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
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
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}
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
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
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
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
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 数学对象备忘单的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号