Multiply Function [x]
|
08-03-2018, 09:27 AM
(This post was last modified: 08-03-2018 09:32 AM by Gamo.)
Post: #1
|
|||
|
|||
Multiply Function [x]
I'm wondering what algorithm is use in calculator for the multiply function.
So I came up with the basic whole number multiplication by using the adding up method. 7 x 4 = (7 + 7 + 7 + 7) = 28 Then I came up with this program to do the multiply function. This program will multiply two whole numbers and do multiply with the first whole number with decimal like (21.75 x 5) This program will not give correct result when multiply both decimal numbers like (10.2 x 11.2) Example: 6903 x 103 6903 [ENTER] 103 [R/S] ---> 711009 Step can be reverse: 103 [ENTER] 6903 [R/S] ---> 711009 Program: Multiply Function using HP-12C Code:
Is there any other more efficient way to do this better and faster? Anyone get a better solution so that this can do multiplication with both decimal numbers like (123.45 x 12.34) Thank You Gamo |
|||
08-03-2018, 11:31 AM
Post: #2
|
|||
|
|||
RE: Multiply Function [x]
Code:
Greetings, Massimo -+×÷ ↔ left is right and right is wrong |
|||
08-03-2018, 01:04 PM
Post: #3
|
|||
|
|||
RE: Multiply Function [x] | |||
08-03-2018, 01:07 PM
Post: #4
|
|||
|
|||
RE: Multiply Function [x]
Hello Gamo,
what is the problem with the decimal numbers? Remove the decimal points, multiply the intergers and add the (correct) decimal point to the result. Most of the time decimal numbers are stored in a computer as floating point numbers with significand (digits) and exponent. For example in IEEE754 format x = m * 2^e where m is the significand and e is the exponent. Multiplication is done by multiplying the significands, adding the exponents and normalize the result. The multiplication of the significands could be done by a shift and add algorithm. Wikipedia |
|||
08-03-2018, 01:13 PM
Post: #5
|
|||
|
|||
RE: Multiply Function [x]
(08-03-2018 11:31 AM)Massimo Gnerucci Wrote: Excellent my friend!! Great way to start a weekend, with a good laugh... --Bob Prosperi |
|||
08-03-2018, 02:10 PM
Post: #6
|
|||
|
|||
RE: Multiply Function [x]
I like this algorithm; it's so simple you can do it with a couple pieces of wood.
Code: 01 LBL"MULT |
|||
08-03-2018, 02:40 PM
(This post was last modified: 08-03-2018 05:32 PM by Dieter.)
Post: #7
|
|||
|
|||
RE: Multiply Function [x]
(08-03-2018 09:27 AM)Gamo Wrote: I'm wondering what algorithm is use in calculator for the multiply function. There was a series of HP Journal articles that described how calculators do their calculations. I remember something about log, exponential and trig functions, and maybe there also was something about multiplication and division. (08-03-2018 09:27 AM)Gamo Wrote: Example: 6903 x 103 When I ran this it returned 711012,14. Why is that? Register 2 happened to have pi stored in it (from a previous calculation – the 12C has continuous memory). And your program does not clear this register before it starts adding! I have mentioned this point several times: always clear a sum before you start adding data! Clearing the sum afterwards, as your program does it, is not a solution. (08-03-2018 09:27 AM)Gamo Wrote: Step can be reverse: 103 [ENTER] 6903 [R/S] ---> 711009 But the program takes much longer to finish. You should start with the smaller number in X. Why not have the program care for this? Simply start with X<=Y? X<>Y X<>Y You can also speed up (and shorten) the program by using storage arithmetics. Edit: here's my attempt of such a program. Only one data register is required. Code: 01 X<=Y? Note: the smaller (!) of the two factors must be an integer ≥0. If there is a fractional part it is discarded. The larger factor may be fractional. 123,4 [ENTER] 17 [R/S] => 2097,80 17 [ENTER] 123,4 [R/S] => 2097,80 Dieter |
|||
08-03-2018, 05:33 PM
Post: #8
|
|||
|
|||
RE: Multiply Function [x]
(08-03-2018 01:07 PM)wynen Wrote: The multiplication of the significants could be done by a shift and add algorithm. Wikipedia Peasant or binary multiplication Here's a program for the HP-11C: Code: LBL A ; times This is a translation of the following Python program: Code: def times(a, b): It works for positive integers \(a\) and \(b\) and calculates \(a\times b\). Cheers Thomas |
|||
08-03-2018, 06:45 PM
Post: #9
|
|||
|
|||
RE: Multiply Function [x]
(08-03-2018 02:40 PM)Dieter Wrote: There was a series of HP Journal articles that described how calculators do their calculations. I remember something about log, exponential and trig functions, and maybe there also was something about multiplication and division. This is the relevant MCODE of the HP-41 for the multiplication: Code: ***************************************************** This line adds the exponents: Code: 144 125 1006 C=A+C X The algorithm uses the same digit by digit method we use when we manually calculate the product of two numbers. The accumulator A keeps track of the sum. The mantissa of one number is kept in B and continuously added to A while the mantissa of the other number in C is decremented digit by digit: Code: 154 137 MPY130 456 A=A+B W Once a digit of C is done the pointer PT is incremented and the accumulator A is shifted: Code: 151 134 MPY120 1734 INC PT Easiest of course is to follow the program step by step in the debugger of an emulator. Cheers Thomas PS: The algorithm is similar to the binary multiplication of my previous post. However base 10 instead of base 2 is used. |
|||
08-03-2018, 10:54 PM
Post: #10
|
|||
|
|||
RE: Multiply Function [x]
I asked a similar question on the HPCC Forum last year.
Firstly Google CORDIC. The best information I found is in the attached Algorithms PDF. Denny Tuckerman |
|||
08-03-2018, 11:05 PM
(This post was last modified: 08-03-2018 11:05 PM by ijabbott.)
Post: #11
|
|||
|
|||
RE: Multiply Function [x]
Multiplication is simple in binary:
0 * 0 = 0 0 * 1 = 0 1 * 0 = 0 1 * 1 = 1 Basically, just an AND gate. Then you need to shift and add everything.... And HP calculators work in decimal anyway, not binary. — Ian Abbott |
|||
08-04-2018, 01:41 AM
Post: #12
|
|||
|
|||
RE: Multiply Function [x]
Thank You Thomas Klemm
I was still figuring out on how to program "Peasant or binary multiplication" on HP-12C in awhile then I try the adding up method to do the multiplication. The peasant method is very interesting one and thank for the code for HP-11C Thank You Gamo |
|||
08-04-2018, 04:23 AM
Post: #13
|
|||
|
|||
RE: Multiply Function [x]
Leviset
Thank You for more in-depth information on algorithm. Gamo |
|||
08-04-2018, 08:26 AM
Post: #14
|
|||
|
|||
RE: Multiply Function [x]
You can think of any number (even with decimals) as a polynomial of digits. Each digit is multiplied by a power of 10. For example, the number 123.4 is a polynomial ranging from 10 to the power 2 to 10 to the power -1. The digits of the number are the coefficients of the polynomial:
N(2,-1) = 1 * 10^2 + 2 * 10^1 + 3 * 10^0 + 4 * 10^(-1) So you can multiply two numbers by multiplying the polynomials that represent them. |
|||
08-04-2018, 09:03 AM
(This post was last modified: 08-04-2018 09:20 AM by Thomas Puettmann.)
Post: #15
|
|||
|
|||
RE: Multiply Function [x]
Hello Gamo,
nice to see that you are interested in programming elementary arithmetics! Maybe you would also enjoy to take a look into the history of computing? The book "Computing before Computers" is a very nice starting point. It is available for free http://ed-thelen.org/comp-hist/CBC.html. One comment regarding your initial post: I know that some textbooks all over the world teach that 7 x 4 is defined to be 7 + 7 + 7 + 7. But, morally, this is wrong. 7 x 4 should always be defined to be 4 + 4 + 4 + 4 + 4 + 4 + 4. If you go seven times running, you do it seven times. If you have seven times a cake, you have seven cakes. And if you have seven times four cakes, you have 4 + 4 + 4 + 4 + 4 + 4 + 4 cakes by definition, not the other way around. We are lucky that multiplication of natural or even real numbers is commutative. But to fully appreciate this fact, it is good to be precise and consistent with the basic definitions. Of course, you did not state that you wanted to use the definition. So this comment is just a comment and not criticism. Thomas |
|||
08-05-2018, 03:12 AM
Post: #16
|
|||
|
|||
RE: Multiply Function [x]
(08-04-2018 09:03 AM)Thomas Puettmann Wrote: ... Ugh. <smell like="Common Core" />. (Post 265) Regards, BrickViking HP-50g |Casio fx-9750G+ |Casio fx-9750GII (SH4a) |
|||
08-05-2018, 03:33 AM
(This post was last modified: 08-05-2018 06:21 AM by Gamo.)
Post: #17
|
|||
|
|||
RE: Multiply Function [x]
Thank You to everyone who gave more information about this topic.
I came across to this "Division Function" as well by using the "Repeated Subtraction" algorithm I decided to program this division function by using the repeated substraction. This algorithm will work only with perfect division result so I make this program that if division is not perfect then return result as (-1) Example: First Start f [REG] 24÷3 24 [ENTER] 3 [R/S] ---> 8 2475÷25 2475 [ENTER] 25 [R/S] ---> 99 2475÷24 2475 [ENTER] 24 [R/S] ---> -1 // Not a perfect division Program: Division by Repeated Subtraction Code:
Gamo |
|||
08-05-2018, 04:07 PM
Post: #18
|
|||
|
|||
RE: Multiply Function [x]
(08-04-2018 09:03 AM)Thomas Puettmann Wrote: It is available for free http://ed-thelen.org/comp-hist/CBC.html. In Chapter One: Early Calculation on page 18 I noticed a special bone each for the square root and the cubic root. It wasn't obvious to me how they were used: Extracting square roots Raíz cúbica The 2nd link is in Spanish but you can probably still follow it when translated. Thanks for sharing the linked documents. Kind regards Thomas |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)