"JavaScript Zero-based" type, value, variable----number

Source: Internet
Author: User
Tags natural logarithm root math square root

node. js is hot, and the ex-section is good to fly ....

So I decided ....

Start learning JavaScript from scratch
Some are more special, or before we notice that

Or something that's going to go wrong, I'll write it down.

Arithmetic operations in JavaScript

JavaScript also claims to be more complex arithmetic operations, which are implemented by functions and constants defined as properties of the Math object:

Math.pow (2,53)//power of =>9007199254740992:2 53 timesMath.Round (. 6)//=>1.0: RoundingMath.ceil (. 6)//=>1.0: Rounding upMath.floor (. 6)//=>0.0: Rounding downMath.Abs (-5)//=>5: Seeking absolute valueMath.max (x, Y, z)//= = Returns the maximum valueMath.min (x, Y, z)//= = Returns the minimum valuemath.random ()//generates a pseudo-random number greater than or equal to 0 less than 1.0Math.PI//π: PiMath.e//e: base of natural logarithmMATH.SQRT (3)//square root of 3Math.pow (3,1/3)//3the cube root Math.sin (0)//Trigonometric Functions: There are math.cos and so on .Math.log (10)//Natural logarithm of 10Math.log (+)/MATH.LN10//logarithm with base 100 of 10Math.log (/MATH.LN2)//logarithm Math.exp with base 512 of 2 (3)//three power of e                

JavaScript uses IEEE-754 floating-point notation (almost all modern languages are used),

This is a binary notation that can accurately represent fractions such as 1/2, 1/8, and 1/1024. Unfortunately,

The scores we use (especially in financial calculations) are decimal fractions of 1/10, 1/100, and so on.

Binary floating-point notation does not accurately represent numbers like 0.1.

The numbers in JavaScript are accurate enough and can be approximated by 0.1. But the fact is that the numbers don't exactly represent some of the problems.

Take a look at this piece of code:

var x=.3-.2  ;                 // 30 cents minus 20 cents . var y=.2-.1;                  // 20 cents minus 10 cents x==y                            //=>false: Two values are not equal! x==1                            //=>false:. 3-.2 not equal to. 1y==1                            //=>true:. 2-.1 equals. 1     

Due to rounding errors, the approximate difference between 0.3 and 0.2 is actually not equal to the myopic difference between 0.2 and 0.1.

Note: In JavaScript's really running environment, 0.3-0.2=0.099 999 999 999 999 98

This problem is not only in JavaScript , but it is important to understand that:

This problem can be found in any programming language that uses binary floating-point numbers.

It is also important to note that the values of x and y in the above code are very close to each other and to the final correct value.

This calculation can do most of the computational tasks: This problem only occurs when you compare two values for equality.

Future versions of JavaScript may support decimal number types to avoid these rounding issues. Before that, you might prefer to use only large integers for important financial calculations.

For example, to use the integer "Min" Do not use the decimal "meta" for the currency unit-based operation.

In the calculation, we also ask you to use fewer decimals, that is, when the amount is 1.6 yuan, please use 16 points.

"JavaScript Zero-based" type, value, variable----number

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.