Ask Your Question
0

How to format huge reals?

asked 2012-04-28 00:40:18 -0600

petropolis gravatar image
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 flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2012-04-28 07:43:02 -0600

petropolis gravatar image

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

Please suggest improvements.

edit flag offensive delete link more
0

answered 2012-04-28 06:53:52 -0600

Volker Braun gravatar image

updated 2012-04-28 06:54:01 -0600

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-04-28 00:40:18 -0600

Seen: 105 times

Last updated: Apr 28 '12