(PHP implementation) Use only ++ operations to implement addition, subtraction, multiplication, and division
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 code 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
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 and ASCII codes, and the program code is as follows: The code is as follows: <?php $str1=chr(88); echo $str1; //The return value is X $str2=chr(ord(X)+1); // echo $str2; //The return value is Y echo “\t”; $str3=ord(‘S’ ); echo $str3; //The return value is 83 ?> Operation result: X Y 83
php – perform OPERATION (plus, minus, multiplication, division) in UpdateSQL
How to add, subtract, multiply or divide selected tables and rows in SQL? Here is sample code where I manually subtract the original quantity to create a new quantity and update to the selected row: $idArr = $ _POST[‘checkboxId’];foreach($idArr as $index=>$value){ $id = mysql_real_escape_string($value); // Get Quantity from this item id $sql = “SELECT quantity FROM items WHERE item_id = ‘$id’”; $result = mysql_query($sql); $quantity = $row[‘quantity’]; // New quantity after minus by 1 $new_quantity = $row[‘quantity’] – 1; // Update new quantity to this item $sql = “UPDATE items SET quantity = ‘$new_quantity’ WHERE item_id = ‘$id’”; $result = mysql_query($sql);} Is it practical to update the quantity (integer) change in the preferred row? Can I do this with a single update query? Solution: Why not put the operation in the SQL update query? For example, you can use the following query: UPDATE items SET quantity = quantity – 1 WHERE item_id = ‘$id’ Or: UPDATE items SET quantity = quantity + 1 WHERE item_id = ‘$id’ Big advantage: this is done in one SQL query (no select, then update); this means there will be no problem if two users try to do this at the same time: SQL will…
PHP’s chr and ord functions implement character addition, subtraction, multiplication, and division operations to achieve code_PHP
The chr function is used to convert ASCII codes to charactersThe ord function is used to convert characters to ASCII codes ASCII codes are the codes that computers can display characters, and its value range is 0-255, including 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 chr() function and ord() function to perform the conversion operation between string and ASCII code, the program code is as follows: The code is as follows: <?php $str1=chr(88); echo $str1; //The return value is X $str2=chr( ord(X)+1); // echo $str2; //The return value is Y echo “\t”; $str3=ord(from 4 source gaodaimacom 做#代 %code*netGaodaima code‘S’); echo $str3; //The return value is 83 ?> Running result: X Y 83
Addition, subtraction, multiplication and division algorithms commonly used in BigDecimal, size comparison, and saving two decimal points_bigdecimal addition, subtraction, multiplication, and division operations keep two decimal places_wei_wenlong’s blog
Not much to say, let’s record it first. BigDecimal coins = new BigDecimal(“0”); BigDecimal one = new BigDecimal(“1”); BigDecimal two = new BigDecimal(“2”); coins = coins.add( two ); //coins plus 2 System.out.println( coins.doubleValue()); //output is 2.0 coins = coins.subtract( one ); //coins minus 1 System.out.println( coins.doubleValue()); //The output is 1.0 coins = coins.multiply( two ); //coins are multiplied by 2 System.out.println( coins.doubleValue()); //The output is 2.0 int i = coins .compareTo(BigDecimal.ZERO) ; //Judge whether coins is greater than 0 int k = coins .compareTo( one ) ;//If i = 0, it means they are equal; if i 0 means coins > one coins = coins.setScale(2,BigDecimal.ROUND_HALF_UP) //Save two decimal places Division is trickier: When the bigdecimal type does division, there is no problem when the result is an integer or a finite decimal. If the result cannot be divisible, an error will be reported for an infinite decimal Error code: Bigdecimal bd = num1.divide(c).setScale(6,ROUND_HALF_UP); Error message: Non-terminating decimal expansion; no exact representable decimal result” Error translation: infinite decimal expansion; no exact decimal result. Reason for error: temporarily unknown Code improvements: BigDecimal bd = num.divide(c,6,ROUND_HALF_UP);
Study Notes (2): Programmer’s Mathematics: Linear Algebra – Matrix Addition, Multiplication, Transposition_lailjx123’s Blog
Learn now:https://edu.csdn.net/course/play/26245/ 326282?utm_source=blogtoedu Vector * vector = scalar (1 dimension * 0 dimension = 0 dimension) Vector*Matrix=Vector (2D*1D=1D) The matrix establishes the mapping from vector to vector