当前位置:网站首页>JS foundation -- math
JS foundation -- math
2022-07-25 02:48:00 【H5_ ljy】
Here's the catalog title
What is? Math object
Math Object allows you to perform mathematical tasks .
Math Not a constructor .Math All attributes of / Methods can be used Math Call... As an object , Without creating it :
Math Object properties
| attribute | describe |
|---|---|
| E | Returns the Euler number ( about 2.718). |
| LN2 | return 2 The natural logarithm of ( about 0.693). |
| LN10 | return 10 The natural logarithm of ( about 2.302). |
| LOG2E | return E The to 2 Log base ( about 1.442). |
| LOG10E | return E The to 10 Log base ( about 0.434). |
| PI | return PI( about 3.14). |
| SQRT1_2 | return 1/2 The square root of ( about 0.707). |
| SQRT2 | return 2 The square root of ( about 1.414). |
Math Object methods
| attribute | describe |
|---|---|
| abs(x) | return x The absolute value of . |
| acos(x) | return x The arccosine of , In radians . |
| acosh(x) | return x Hyperbolic arccosine of . |
| asin(x) | return x The arcsine of , In radians . |
| asinh(x) | return x Hyperbolic arcsine value of . |
| atan(x) | return x The arctangent of , The value returned is -PI/2 To PI/2 The radian value between . |
| atan2(y, x) | Returns the arctangent of its parameter quotient . |
| atanh(x) | return x Hyperbolic arctangent of . |
| cbrt(x) | return x The third root of . |
| ceil(x) | return x, Round up to the nearest integer . |
| clz32(x) | return x Of 32 The number of leading zeros in a bit binary representation . |
| cos(x) | return x Cosine of (x In radians ). |
| cosh(x) | return x The hyperbolic cosine value of . |
| exp(x) | return Ex Value . |
| expm1(x) | return Ex subtract 1 Value . |
| floor(x) | return x, Round down to the nearest integer . |
| fround(x) | Returns the closest of the numbers (32 Bit single precision ) Floating point means . |
| log(x) | return x The natural logarithm of . |
| log10(x) | return x The to 10 Log base . |
| log1p(x) | return 1 + x The natural logarithm of . |
| log2(x) | return x The to 2 Log base . |
| max(x, y, z, …, n) | Return the highest number . |
| min(x, y, z, …, n) | Returns the number with the smallest value . |
| pow(x, y) | return x Of y Power value . |
| random() | return 0 To 1 Random number between . |
| round(x) | take x Round to the nearest integer . |
| sign(x) | Returns the sign of a number ( Check that it is a positive number 、 Negative or zero ). |
| sin(x) | return x The sine of (x In radians ). |
| sinh(x) | return x The hyperbolic sine of . |
| sqrt(x) | return x The square root of . |
| tan(x) | Returns the tangent of the angle . |
| tanh(x) | Returns the hyperbolic tangent of a number . |
| trunc(x) | Return to digital (x) The integral part of . |
Several commonly used method cases
ceil(x) Round up logarithm .
floor(x) The logarithm is rounded down .
round(x) Round a number to the nearest whole number .
var a=66.18
console.log(Math.ceil(a))
console.log(Math.floor(a))
console.log(Math.round(a))

cos(x) Returns the cosine of a number
sin(x) Returns the sine of a number
// return 90 Sine value of degree
var deg=Math.PI/180 // representative 1 Degrees , Again JS The unit of middle angle is Math.PI/180 To express
var re=Math.sin(90*deg)
console.log(re)
max(x,y) return x and y The highest value in .
min(x,y) return x and y The lowest of .
pow(x,y) return x Of y The next power .
sqrt(x) Returns the square root of a number .
random() Method to return from 0( contain ) To 1( Not included ) The random number .
var re=Math.random()// Pseudo random number [0,1)
console.log(re*10)//[0,10)

How to represent a random -66 To 66 Random integer between ?
console.log(Math.ceil(Math.random()*(66-(-66))+(-66)))

Add :
toFixed Returns a few decimal places after the decimal point , The parameter represents the specific decimal digits
Be careful : Take the rounded value .
var n=8.66
var re=n.toFixed(1)
var re2=n.toFixed(2)
console.log(re)
console.log(re2)

parseFloat Returns a floating-point number in a string
parseInt Returns an integer in a string
var re2=parseFloat("1239.23px123")
console.log(re2)
var re3=parseInt("1239.93px123")
console.log(re3)

round Rounding off negative numbers ( a key )
round() Negative numbers, such as :-11.5 This is in the middle of the decimal .5 Rounding calculation of :
Math.round(-11.51) = -12
however Math.round(-11.5) = -11; Not at all -12
Math.round() The underlying source code calculation is :
public static long round(double a) {
return (long)floor(a + 0.5d);
}
It can be seen that ,round() The calculation rule is :
Returns the closest parameter long. The result will be rounded to an integer : add 0.5, Call... On the result floor And force the result to long type .
therefore :
floor(-11.5+0.5) = -11;
floor(-11.51+0.5) = floo(-12.01) = -12
therefore Math.round(-11.5) = -11, instead of -12
边栏推荐
- Work arrangement of generator set
- Large number processing -- use case
- After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability
- Several dpdk control frameworks
- How to use blender to make 360 degree panorama and panoramic video?
- Js a simple way to store several objects in an array
- Physical experiment simulation
- Simulation Implementation of string function (Part 1)
- Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming
- Can PostgreSQL CDC only connect to the main database? Connection from the library reports an error logical decoden
猜你喜欢

Classic network learning RESNET code implementation

Conceptual distinction between Po, Bo, VO, dto and POJO

R language uses logistic regression, ANOVA, outlier analysis and visual classification iris iris data set

【C】 Advanced knowledge of file operation

Example demonstration of "uncover the secrets of asp.net core 6 framework" [02]: application development based on routing, MVC and grpc

Is redis'module'not an internal or external command?
![ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience](/img/26/18fd3106f887f503479f081e29dac3.jpg)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience

Is it necessary to increase the number of milliseconds and save several KB of memory in the program?

How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
Failed to create data snapshot: lock file [/siyuan/data/assets/image- 2022070216332-jijwccs.png failed: open /siyuan/data/assets/image- 2022070216332-jijwccs.png: permission denied; unable to lock fil
随机推荐
Talk about what's going on with C # backstage GC?
Redux best practices "Redux toolkit"
Go multiplexing
Componentization and modularization
DLL load failed: the page file is too small to complete the operation
Arthas case: dynamic update application logger level
TS uses a third-party library, and there is no type declaration file error handling
Nuscenes data set summary
Routing policy interferes with routing
[circular array] JS method collection of circular array
Several dpdk control frameworks
Jenkins configuration plug-in interface displays "suggestions collection" in Chinese
【C】 Advanced knowledge of file operation
HAC cluster is modified to stand-alone
Codeforces:d1. choosing carrots (easy version) [max min problem + control one side to make the other side as close as possible + thinking]
6.0 cancellation of member registration verification code
Three ways to solve your performance management problems
Js a simple way to store several objects in an array
Jedispoolconfig parameter configuration from the perspective of source code
Tp5.1 include include files (reference public files)