0

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.

  • Finite representations? Like $.123$ but not $.\bar{3}$? Because you can "remove the decimal point" which needn't be exactly the same as multiplication on a programming level and then use whatever system you have to multiply these new integers and then put the decimal point back... – Mason Dec 18 '18 at 22:34

1 Answers1

1

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)}$.

CopyPasteIt
  • 11,366