Math对象
Math对象是在高中数学课就学过的内置对象。它知道解决最复杂的数学问题的所有公式,如果给它要处理的数字,即能计算出结果。
Math对象有几个属性,主要是数学界的专用值。下表类出了这些属性:
属 性 |
说 明 |
E |
值e,自然对数的底 |
LN10 |
10的自然对数 |
LN2 |
2的自然对数 |
LOG2E |
以2为底E的对数 |
LOG10E |
以10为底E的对数 |
PI |
值π |
SQRT1_2 |
1/2的平方根 |
SQRT2 |
2的平方根 |
虽然这些值的意义与用法不在本书讨论范围内,但如果清楚它们是什么,在需要时,即可使用它们。
Math对象还包括许多专门用于执行简单的及复杂的数学计算的方法。
方法min()和max()用于判断一组数中的最大值和最小值。这两个方法都可接受任意多个参数:
对于数字3、54、32和16,max()返回54,min()返回3。用这些方法,可免去用循环或if语句来判断一组数中的最大值。
另一个方法abs()返回数字的绝对值。绝对值是负数的正值版本(正数的绝对值就是它自身)。
这个例子中,abs(-1)返回1,abs(1)也返回1。
下一组方法用于把小数舍入成整数。处理舍入操作的方法有三个,即ceil()、floor()和round(),它们的处理方法不同:
q 方法ceil()表示向上舍入函数,总是把数字向上舍入到最接近的值。
q 方法floor()表示向下舍入函数,总是把数字向下舍入到最接近的值。
q 方法round()表示标准的舍入函数,如果数字与下一个整数的差不超过0.5,则向上舍入,否则向下舍入。这是在初中学过的舍入规则。
为说明每种方法的处理方式,考虑使用值25.5:
对于ceil()和round(),传递25.5,返回的是26,而floor()返回的是25。注意不要交替使用这些方法,因为最后可能得到与预期不符的结果。
另一组方法与指数的用法有关。这些方法包括exp(),用于把Math.E升到指定的幂;log()用于返回特定数字的自然对数;pow()用于把指定的数字升到指定的幂;sqrt()用于返回指定数字的平方根。
方法exp()和log()本质上功能相反,exp()把Math.E升到特定的幂,log()则判断Math.E的多少次指数才等于指定的值。例如:
这里,首先用exp()把Math.E升到10次幂,然后log()返回10,即等于数字iNum必需的指数。很多人都对此感到迷茫。全世界的高中生和数学系的大学生都被此类问题难倒过。如果你对自然对数一无所知,那么有可能永远都不需要为它编写代码。
方法pow()用于把数字升到指定的幂,如把2升到10次幂(在数学中表示为210):
pow()的第一个参数是基数,此例子中是2。第二个参数是要升到的幂,此例子中是10。
不建议把Math.E作为pow()方法的基数。最好使用exp()对Math.E进行升幂运算,因为它是专用运算,计算出的值更精确。
The last method in this set of methods is sqrt(), which returns the square root of the specified number. It has only one parameter, the number whose square root is required. To ask for the square root of 4, you only need to use one line of code:
Of course, the square root of 4 is 2, which is the output of this line of code.
You may ask "Why does the square root have to use an exponent"? In fact, the square root of a number is its 1/2 power. For example, 21/2 is the square root of 2.
The Math object also has a complete set of trigonometric function methods. The following table lists these methods:
Fang method
|
Say Ming |
||||||||||||||||
acos(x) |
Returns the arc cosine of x |
||||||||||||||||
asin(x) |
Returns the arcsine of x | ||||||||||||||||
atan(x) |
![]() |
||||||||||||||||
atan2(y,x) |
Returns the inverse cosine of y/x |
||||||||||||||||
cos(x) | Returns the cosine value of x | ||||||||||||||||
![]() |
Returns the sine value of x |
||||||||||||||||
|
Returns the tangent value of x |

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools