How can I keep python from truncating large numbers after division

555 Views Asked by At

I am trying to do division with very large numbers. I know that python can handle them before the division, but is there a way to keep python from truncating the answer?

an example follows:

s = 68729682406644277238837486231747530924247154108646671752192618583088487405790957964732883069102561043436779663935595172042357306594916344606074564712868078287608055203024658359439017580883910978666185875717415541084494926500475167381168505927378181899753839260609452265365274850901879881203714

M = 2047

s/(2*M) = 1.6787904837968803e+289

It can remember the 292 digit number s but when it divides the large number it gets truncated.

Is there any way that I can get an exact answer?

Thanks

1

There are 1 best solutions below

3
On

If you are only concerned with the integer part of the answer, you can use // which is the integer division operator:

s // (2*M)

It looks like your s is a multiple of M so it sounds like this is what you are looking for.

In Python (3 and later), the / operator is the floating point division operator, while // is the integer division operator. Previous versions of Python had only / and would do different things depending on whether the operands were both integers or not. This was confusing, so a new // operator was introduced and / was redefined to be always floating point.