ISSUE WITH MORE THAN 15 digits

Hello… I am doing some computation on a file containing numbers using Python Language but i am facing issue that a time i can take maximum 15 digits if i am taking more than it. It gives some values. Please help me with this and if it is platform issue than please suggest me any platform.

Hi Vaishali Bhatia,

I’m sorry, your question is too vague for me to understand what your
actual problem is. Can you show an example of the problem? Python can
easily handle numbers with more than 15 digits:

> 123456789012345678901234567890 * 12 + 3
1481481468148148146814814814683

Please show us a small and simple example of:

  • your input data
  • what you are trying to do
  • the result you expect
  • the result you actually get

plus tell us your platform.

a = math.log(123456789123456789)
b = exp(a)
int(round(abs(b)))
123456789123457168

There is data loss when I do log and then antlog

[Vaishali]

"
a=1234567891234566*1234567891234566

a
1524157878067363184895633208356
"

What makes you think that result is inaccurate? What do you think the
result should be?

This was the actual problem

math.log converts the integer to a floating-point number, then does floating point arithmetic to take its logarithm. So you lose information in the computation of both math.log and math.exp. Most likely you are using a 64-bit Python, which can only have about 16 digits of accuracy in the base of the representation of the float (base*10^exponent).

Python can actually store an arbitrarily long integer (as long as it fits in RAM, of course)

Hi
Could you please suggest some source/application/cloud service which can solve this issue?

It seems like your main issue is the specific lack of precision with floats, so you likely want to look into the decimal stdlib module for an arbitrary number of digits of precision (defaults to 28 digits, but it’s configurable). See decimal — Decimal fixed point and floating point arithmetic — Python 3.8.17 documentation.

P.S. There’s no need to have your topic title in ALL CAPS. Instead, I’d suggest using either “Title Case” or “Sentence case”, with “Sentence case” being more common. For example: “Issue with more than 15 digits”. That’s typical forum/discussion board etiquette.

Also, for future reference (as Steven was suggesting earlier in the thread), it helps a lot with providing an accurate and timely answer to the problem if you provide the specific details of the problem in your question. Otherwise, it ends up taking up additional time and effort just to figure out what the real problem actually is, or in some cases can result in not receiving an answer at all.

I’d recommend reading over https://stackoverflow.com/help/how-to-ask, as most of it applies broadly to any question-answer platform.

1 Like

Forget log and exp. The issue is that CPython floats use C doubles, which typically use 64 bits total and, I believe, about 53 bit of precision, which is about 17 decimal digits. So there is a large range of 20 digit ints that get converted to the same float.

>>> def xif(n):
	return int(float(n))

>>> xif(12345678901234566144)
12345678901234565120
>>> xif(12345678901234566145)  # Lowest
12345678901234567168
>>> xif(12345678901234567890)  # Yours
12345678901234567168
>>> xif(12345678901234568191)  # Highest
12345678901234567168
>>> xif(12345678901234568192)
12345678901234569216
1 Like