Ask Your Question
0

How to format huge reals?

asked 2012-04-28 07:40:18 +0100

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 14:43:02 +0100

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 13:53:52 +0100

Volker Braun gravatar image

updated 2012-04-28 13:54:01 +0100

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 07:40:18 +0100

Seen: 459 times

Last updated: Apr 28 '12