First time here? Check out the FAQ!

Ask Your Question
0

How to format huge reals?

asked 13 years ago

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?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
0

answered 13 years ago

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.

Preview: (hide)
link
0

answered 13 years ago

Volker Braun gravatar image

updated 13 years ago

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.

Preview: (hide)
link

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: 13 years ago

Seen: 491 times

Last updated: Apr 28 '12