问题
I have two numbers X and Y, i don't know their values but i know that the maximal number of digits that can contain X is 10 (for example) and for Y is 3 (for example)
I need to know the maximal number of digits of Z that equals to X * Y. Same for Z = X / Y
回答1:
If X
can have at most n
digits and Y
can have at most m
digits, then
X < 10 ^ n and Y < 10 ^ m
This means that
X * Y < 10 ^ n * 10 ^ m = 10 ^ (n + m)
In other words,
X * Y can have at most n + m digits.
With division, we need to take the worst case for Y
- that is value 1. So maximum number of digits for X / Y
is n
.
However, if you know Y has exactly m digits, then we can say that
Y >= 10 ^ (m - 1)
in that case we get:
X / Y < (10 ^ n) / (10 ^ (m-1)) => X / Y < (10 ^ (n - m + 1))
This means that X / Y
has at most n - m + 1
digits when Y has exactly m digits.
回答2:
assume the number of digit in X
is n
and for Y
is m
and values are rounded so
the maximum number of digits in (x*y)
is n+m
and
the maximum number of digits in (x/y)
is n-m+1
来源:https://stackoverflow.com/questions/24303813/number-of-digits-after-multiplication-dividing