# How to format huge reals?

print '{0:.3e}'.format(float(2.23799235765712699))
print '{0:.3e}'.format(float(2.23799235765712699e47675))

gives

2.238e+00
inf

Clearly what I want in the second case is 2.238e+47675.

How can I achieve this?

edit retag close merge delete

Sort by ยป oldest newest most voted

Thank you Volker! So let's give it a try:

def SciFormat(f, n) :
s = f.str()
t = ""
count = 0
w = true
for c in s :
if c <> 'e' and w :
if count < n+2 :
t += c
count += 1
else :
t += c
w = false
return t
b = 2.2379923576571269975e4767529
print b.parent()
for i in (0..20): print SciFormat(b, i)
Real Field with 67 bits of precision
2.e4767529
2.2e4767529
2.23e4767529
2.237e4767529
2.2379e4767529
2.23799e4767529
2.237992e4767529
2.2379923e4767529
2.23799235e4767529
2.237992357e4767529
2.2379923576e4767529
2.23799235765e4767529
2.237992357657e4767529
2.2379923576571e4767529
2.23799235765712e4767529
2.237992357657126e4767529
2.2379923576571269e4767529
2.23799235765712699e4767529
2.237992357657126998e4767529
2.237992357657126998e4767529
2.237992357657126998e4767529

more

Python floating point numbers are limited in their exponent, this is why float(2.23799235765712699e47675) is inf. Since python doesn't have arbitrary precision floating point numbers, you can't use python string formatting to achieve what you want. You can, however, write your own printing routine for arbitrary precision (MPFR) reals.

more