Python Forum
large ints and floats
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
large ints and floats
#1
what is the largest int that can be converted to float such that 1 larger cannot be converted to float?

hint: it is not a power of 2

suggestion: don't bother trying to step by 1 unless you start very close to the right number
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
>>> a
179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791
>>> float(a)
1.7976931348623157e+308
>>> float(a + 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: int too large to convert to float
Reply
#3
Sad!

I was thinking that there are no limitations if you have enough memory.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
the issue is that float has a limited size which i understand can vary by implementation. cpython, the most commonly distributed implementation, uses type double in the C language. the limitation is what the hardware does for C double (not long double).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
One can use the gmpy2 library to handle larger numbers
>>> import gmpy2
>>> gmpy2.mpfr(1<<10000)
mpfr('1.9950631168807584e+3010')
Play with the context instance to control the mpfr's precision.
wavic likes this post
Reply
#6
Love gmpy2
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
is there a way to do int with gmp to do extreme ints faster? i still have to translate extreme int stuff to Pike to get the speed (it has indefinite ints like Python but uses gmplib to implement them).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
The gmpy2.mpz type is just an interface to gmp's integers. With them you should get the same performance as another interface to the gmplib. You could perhaps write a benchmark program with some heavy large integers computation to see the difference between Pike and gmpy2.
Skaperen likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020