JavaScript Deep learning and common tool methods----01. Floating-point number

Source: Internet
Author: User
Tags pow

In JavaScript, integer values and floating-point values are not distinguished, and all numbers are represented by floating-point values. JavaScript uses the IEEE 754 standard (interested in browsing the IEEE 754 standard under the Network specification classification, need the original file please contact me in the message) the defined 64-bit floating-point format represents the number.

Currently, only floating-point numbers are calculated. Other content will be completed in the following time, but also hope that everyone actively provide resources, so that you learn more.

Floating-point direct quantities can be expressed in the following syntax:

[Digits] [. Digits] [(E|e) [(+|-)]digits]

IEEE754 is a binary notation that can be accurately represented (1/2,1/8, and so on), while commonly used decimal fractions (1/10, 1/100) do not accurately represent

The following tool methods are available for everyone to use.

Previously found some information on the Internet, using the process found that division a bit of a problem, here are two ways to implement:

//Description: This function returns a more accurate addition result. //Floatadd (ARG1,ARG2)//return value: Arg1 plus arg2 's exact resultfunctionFloatadd (arg1,arg2) {varR1=0,r2=0; Try{r1=arg1.tostring (). Split (".") [1].length}Catch(e) {}Try{r2=arg2.tostring (). Split (".") [1].length}Catch(e) {} m=math.pow (10, Math.max (R1,R2))return(arg1*m+arg2*m)/M}//This function returns a more accurate subtraction result. //floatsub (ARG1,ARG2)//return value: Arg1 minus the exact result of Arg2functionfloatsub (arg1,arg2) {returnFloatadd (arg1,-arg2); }//This function returns a more accurate multiplication result. //Floatmul (ARG1,ARG2)//return value: arg1 times the exact result of Arg2functionFloatmul (arg1,arg2) {varM=0,s1=arg1.tostring (), s2=arg2.tostring (); Try{M+=s1.split (".") [1].length}Catch(e) {}Try{M+=s2.split (".") [1].length}Catch(e) {}returnNumber (S1.replace (".", "")) *number (S2.replace (".", ""))/math.pow (10,m)}//This function returns a more accurate division result. //Call: Floatdiv (ARG1,ARG2)//return value: Arg1 divided by the exact result of Arg2functionFloatdiv (arg1,arg2) {varT1=0,t2=0, R1,R2; Try{t1=arg1.tostring (). Split (".") [1].length}Catch(e) {}Try{t2=arg2.tostring (). Split (".") [1].length}Catch(e) {} r1=number (Arg1.tostring (). Replace (".", "")) R2=number (Arg2.tostring (). Replace (".", ""))     returnFloatmul (R1/R2), Math.pow (10,T2-T1)); Call multiplication}functionFloatDiv2 (arg1,arg2) {varM=0,s1=arg1.tostring (), s2=arg2.tostring (), R1,SR1; Try{M+=s1.split (".") [1].length}Catch(e) {}Try{M+=s2.split (".") [1].length}Catch(e) {} r1=number (S1.replace (".", ""))/number (S2.replace (".", ""))Try{m+=r1.tostring (). Split (".") [1].length}Catch(e) {}returnNumber (r1.tostring (). Replace (".", ""))/math.pow (10,m)}

JavaScript Deep learning and common tool methods----01. Floating-point 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.