Precise calculation of js floating point numbers (addition, subtraction, multiplication, division)_javascript skills

The code is as follows: / /Note: The addition result of Javascript will have errors, which will be more obvious when adding two floating point numbers. This function returns a more accurate addition result. //Call: accAdd(arg1,arg2) //Return value: the exact result of arg1 plus arg2function accAdd(arg1,arg2){ var r1,r2,m; try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0} try{r2=arg2.toString().split(“. “)[1].length}catch(e){r2=0} m=Math.pow(10,Math.max(r1,r2)) return (arg1*m+arg2*m) /m } The code is as follows: //Description: There will be an error in the subtraction result of Javascript. Between two floating point numbers It will be more obvious when added together. This function returns a more accurate subtraction result. //Call: accSub(arg1,arg2) //Return value: the exact result of arg1 minus arg2function accSub(arg1,arg2){ return accAdd(arg1,-arg2) ; } The code is as follows: //Description: The multiplication result of Javascript will have errors. When two floating point numbers are compared, It will be more obvious when multiplying. This function returns a more accurate multiplication result. //Call: accMul(arg1,arg2) //Return value: the exact result of arg1 multiplied by arg2function accMul(arg1,arg2) { var m=0, s1=arg1.toString(),s2=arg2.toString(); try{m+=s1.split(“.”)[1].length}catch(e){} try{m+= s2.split(“.”)[1].length}catch(e){} return Number(s1.replace(“.”,””))*Number(s2.replace(“.”,” “))/Math.pow(10,m) } The code is as follows: //Description: Javascript division The result will have errors, which will be more obvious when dividing two floating point numbers. This function returns a more accurate…

Precise calculation of js floating point numbers (addition, subtraction, multiplication, division)_javascript skills

The code is as follows: / /Note: The addition result of Javascript will have errors, which will be more obvious when adding two floating point numbers. This function returns a more accurate addition result. //Call: accAdd(arg1,arg2) //Return value: the exact result of arg1 plus arg2function accAdd(arg1,arg2){ var r1,r2,m; try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0} try{r2=arg2.toString().split(“. “)[1].length}catch(e){r2=0} m=Math.pow(10,Math.max(r1,r2)) return (arg1*m+arg2*m) /m } The code is as follows: //Description: There will be an error in the subtraction result of Javascript. Between two floating point numbers It will be more obvious when added together. This function returns a more accurate subtraction result. //Call: accSub(arg1,arg2) //Return value: the exact result of arg1 minus arg2function accSub(arg1,arg2){ return accAdd(arg1,-arg2) ; } The code is as follows: //Description: The multiplication result of Javascript will have errors. When two floating point numbers are compared, It will be more obvious when multiplying. This function returns a more accurate multiplication result. //Call: accMul(arg1,arg2) //Return value: the exact result of arg1 multiplied by arg2function accMul(arg1,arg2) { var m=0, s1=arg1.toString(),s2=arg2.toString(); try{m+=s1.split(“.”)[1].length}catch(e){} try{m+= s2.split(“.”)[1].length}catch(e){} return Number(s1.replace(“.”,””))*Number(s2.replace(“.”,” “))/Math.pow(10,m) } The code is as follows: //Description: Javascript division The result will have errors, which will be more obvious when dividing two floating point numbers. This function returns a more accurate…

Addition, subtraction, multiplication, division, size comparison and usage precautions of BigDecimal in Java

Foreword To borrow words from the book “Effactive Java”, the main design goals of the float and double types are for scientific computing and engineering calculations. They perform binary floating-point operations, which are carefully designed to provide fast approximate calculations with high accuracy over a wide range of values. However, they do not provide completely accurate results and should not be used where accurate results are required. However, business calculations often require accurate results. In Java, the maximum value of int is: 2147483647. Now what if you want to use a number larger than this? In other words, the value is larger, so BigDecimal is used. There are many introductions to BigDecimal. Friends who need to know more can refer to this article: https://www.jb51.net/article/55395.htm 1. Addition, subtraction, multiplication and division of BigDecimal BigDecimal bignum1 = new BigDecimal(“10”); BigDecimal bignum2 = new BigDecimal(“5”); BigDecimal bignum3 = null; //addition bignum3 = bignum1.add(bignum2); System.out.println(“sum is: ” + bignum3); //Subtraction bignum3 = bignum1.subtract(bignum2); System.out.println(“The difference is: ” + bignum3); //Multiplication bignum3 = bignum1.multiply(bignum2); System.out.println(“The product is: ” + bignum3); //division bignum3 = bignum1.divide(bignum2); System.out.println(“Business is: ” + bignum3); The running result is: 2. Comparative size of BigDecimal. BigDecimal num1 = new BigDecimal(“0”); BigDecimal num2…

Examples of Java implementation of matrix addition, subtraction, multiplication, division and conversion functions

The examples in this article describe Java’s implementation of matrix addition, subtraction, multiplication, division and conversion functions. Share it with everyone for your reference, the details are as follows: As a beginner in Java, write a matrix budget program as a tool for future use when writing algorithms. public class MatrixOperation { public static int[][] add(int[][] matrix_a, int[][] matrix_b) { int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; if (row != matrix_b.length || col != matrix_b[0].length) { System.out.println(“Fault”); } else { for (int i = 0; i <row; i++) { for (int j = 0; j <col; j++) { result[i][j] = matrix_a[i][j] + matrix_b[i][j]; } } } return result; } public static int[][] sub(int[][] matrix_a, int[][] matrix_b) { int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; if (row != matrix_b.length || col != matrix_b[0].length) { System.out.println("Fault"); } else { for (int i = 0; i <row; i++) { for (int j = 0; j <col; j++) { result[i][j] = matrix_a[i][j] – matrix_b[i][j]; } } } return result; } public static int[][] dot(int[][] matrix_a, int[][] matrix_b) { /* * matrix_a's dimention m*p matrix_b's dimention p*n. return dimention *m*n */ int row…

Problems with inaccurate addition, multiplication, and division in JavaScript

Take division as an example: If you need to calculate the result of dividing 0.3 by 0.1, you can directly report the result equal to 3 without even thinking about it. But great js just can’t calculate this simple division. The result it gets is: 2.9999999999999….. This is a common problem with js. When multiple floating point numbers are involved, many times it cannot be calculated accurately. We can only use other methods to get the results we need.  DivisionWe can get the exact results we need through this js:   //Division function division(num1,num2){ var t1=0,t2=0,r1,r2;        try{t1=num1.toString().split(“.”)[1].length}catch( e){} try{t2=num2.toString().split(“.”)[1].length}catch( e){} with(Math){ r1=Number(num1 .toString().replace(“.”,“”)) R2 = Number (NUM2.TOSTRING (). Replace ( “.” , “” ))               return (r1/r2)*pow(10,t2-t1); }  }  MultiplicationWe can get the exact results we need through this js:   //Multiplication Function multiplication(num1,num2)  { var m=0,s1=num1.toString(),s2=num2.toString(); try {m+=s1.split(“.”)[1].length}catch(e){} try{m+=s2.split(“.”)[1].length}catch (e){} return Number(s1.replace(“.”,“”))*Number(s2 .replace(“.”,“”))/Math.pow(10,m) }  AdditionWe can get the exact results we need through this js:   //Addition function addition(num1,num2){ var r1,r2,m; try{r1=num1.toString().split(“.”)[1].length}catch (e){r1=0} try{r2=num2.toString().split(“.”)[1].length}catch (e){r2=0} m=Math.pow(10,Math.max(r1,r2)); return (num1*m+num2*m)/m; } so-font-kerning: 0pt;” lang=”EN-US”> try{r1=num1.toString().split(“.”)[1].length}catch(e){r1=0} try{r2=num2.toString().split(“.”)[1].length}catch (e){r2=0} m=Math.pow(10,Math.max(r1,r2)); return (num1*m+num2*m)/m; }

Computer integer class x, Java writes a computer class to implement the addition, subtraction, multiplication, and division operations of two integers. How to implement…

Satisfactory answer luuky51812 2013.01.04 Adoption rate: 59% Level: 7 Already helped: 960 people Class Computer { int a,b; int add(int a,int b){return a+b;} int sub(int a,int b){return a-b;} int mul(int a,int b){return a*b;} double div(int a,int b){return double(a)/double(b);} public static void main(String args[]) { Computer cm= new Computer(); int a=100,b=35; System.out.println(“a+b=”+cm.add (a,b)); System.out.println(“a-b=”+cm.sub(a,b)); System. out.println(“a*b=”+cm.mul(a,b)); System.out.println(“a/b=&# 8221;+cm.div(a,b)); } } 00Share report

Use only ++ operations to implement addition, subtraction, multiplication, and division in PHP

Addition [php] function jiafa($a,$b) { for($i=0;$i<$b;$i++) {          $a++; }   Return $a; } //echo jiafa(4,2); Subtraction [php] function jianfa($a,$b) { $c=0; While($b!=$a) {          $b++;          $c++; }   echo $c; } // end func //jianfa(10,3); Multiplication [php] function chengfa($a,$b) { $c=0; for($j=0;$j<$b;$j++) {           $c=jiafa($c,$a); }   Return $c; } // end func //chengfa(9,3); Division [php] function chufa($d,$e) { $k=0; $f=0; While($f<$d) {          $k++;           $f=chengfa($e,$k);                                                               }   Return $k; } // end func echo chufa(16,2);

(PHP implementation) Only use ++ operations to implement addition, subtraction, multiplication, and division_PHP tutorial-php tutorial

Addition The code is as follows: function jiafa($a,$b){ for($i=0;$i<$ b;$i++) { $a++; } return $a;}//echo jiafa(4,2); Subtraction The code is as follows: function jianfa($a,$b){ $c=0; while($b!=$a) { $b++; $c++; } echo $c;} // end func//jianfa (10,3); Multiplication The code is as follows: function chengfa($a,$b){ $c=0; for($j=0;$j<$b;$j++) { $c=jiafa($c,$a); } return $c;} // end func//chengfa(9,3); Division The code is as follows: function chufa($d,$e){ $k=0; $f=0; while($f<$ d) { $k++; $f=chengfa($e,$k); } return $k;} // end funcecho chufa(16,2); http://www.bkjia.com/PHPjc/327885.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327885.htmlTechArticleThe addition code is as follows: function jiafa($a,$b) { for($i=0;$i$b;$i++) { $a++; } return $a; } //echo jiafa(4, 2); The subtraction code is as follows: function jianfa($a,$b) { $…

(PHP implementation) Only use ++ operations to implement addition, subtraction, multiplication, and division_php tips

Addition The code is as follows: function jiafa($a,$b){ for($i=0;$i<$b;$i++) { $a++; } return $a;}//echo jiafa(4,2); Subtraction The code is as follows: function jianfa($a,$b ){ $c=0; while($b!=$a) { $b++; $c++; } echo $ c;} // end func//jianfa(10,3); Multiplication The code is as follows: function chengfa($a,$b) { $c=0; for($j=0;$j<$b;$j++) { $c=jiafa($c,$a); } return $c;} // end func//chengfa(9,3); Division The code is as follows: function chufa($d, $e){ $k=0; $f=0; while($f<$d) { $k++; $f= chengfa($e,$k); } return $k;} // end funcecho chufa(16,2);

PHP’s chr and ord functions implement character addition, subtraction, multiplication, and division operations to achieve code

What is the use of these two functions? It is most suitable for character addition and subtraction. Ordinary characters cannot be added or subtracted to point to the next character. After converting to ASCII Can do addition, subtraction, multiplication and addition. After processing, it can be converted into characters. Many current string encryption and decryption use this function! The chr function is used to convert ASCII codes to charactersord function is used to convert characters to ASCII codes ASCII codes are the codes of characters that can be displayed by the computer, and its value range is 0-255. These include punctuation, letters, numbers, Chinese characters, etc. In the programming process, the specified characters are often converted into ASCII codes for comparison. The following is the function provided by PHP to convert ASCII codes and characters. 1. chr() function This function is used to convert the ASCII code value into a string. Its function is declared as follows: string chr (int ascii); 2. ord() function This function is used to convert a string into an ASCII code value. Its function declaration is as follows: int ord(string str); Example: Use the chr() function and ord() function to perform conversion operations between strings…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索