How can you multiply decimal values without using a multiplication or division operator?

119 Views Asked by At

I figured out a problem challenging me to write code to do this with integers, but I was wondering how I would have done it if it had been decimals and I couldn't figure it out.

1

There are 1 best solutions below

0
On BEST ANSWER

Let

$a_0 = a_1 \times 10^{a_2}$

$b_0 = b_1 \times 10^{b_2}$

with $a_1, b_1 \in \Bbb Z$ and $a_2, b_2 \in \Bbb Z$ and $a_2 \le 0$ and $b_2 \le 0$ and

$a_2 \lt 0 \text{ iff } a_0 \notin \Bbb Z$

and

$b_2 \lt 0 \text{ iff } b_0 \notin \Bbb Z$

Both the numbers $a_0$ and $b_0$ are either integers or actual (finite length) decimals with this representation.

Now

$\tag 1 a_0 b_0 = a_1 b_1 \times 10^{a_2+b_2}$

Of course if $a_2 + b_2 \lt 0$ we have to make sure that the form is 'fixed up' to give a pure representation - you have to check/cancel at least one trailing zero (least significant digit) in the product $a_0 b_0$ when represented as a string of digits.


Example: $a_0 = 5$ and $b_0 = 0.2$:

$\quad a_0 = 5 \times 10^0$

$\quad b_0 = 2 \times 10^{-1}$

$\quad a_0 b_0 = (5)(2) \times 10^{(0)+(-1)}= 10 \times 10^{-1}$

The exponent is negative and the integer part has $0$ at the far right end. So you remove a zero and increment the exponent.

$\tag 2 a_0 b_0 = 1 \times 10^{0}$

Since the exponent isn't negative there is nothing to check - the product is an integer and we are writing it in standard $\text{integer or decimal}$ form using $\text{(2)}$.