really small values

D

Dee Asbury

In multiplying a value of xe^-325 with ye^-4, Python is returning zero. How
do I get it to give me back my tiny value?

Thanks!
Dee



"The most exciting phrase to hear in science, the one that
heralds new discoveries, is not 'Eureka!' but 'That's funny...'"

- Isaac Asimov
 
J

John Machin

In multiplying a value of xe^-325 with ye^-4, Python is returning zero.
How do I get it to give me back my tiny value?

It is difficult to understand what you mean by xe^-325 etc ... in
Python, ^ is the bitwise exclusive-or operator. The power operator is **

Consider this:

In other words, e ** (-325) is of the order of 7.15 * 10 ** (-142)

Also consider that the smallest positive number that can be represented
by the (almost universal) IEEE 754 64-bit floating-point representation
is about 10**(-308).

So under one interpretation of "e^-325" (1.0e-325), your result is
already effectively zero; under the other interpretaion (math.exp(-325))
it is nowhere near zero.

Perhaps you had better show us the (no more than 10) lines of actual
runable Python code that demonstrate your problem.

HTH,
John
 
M

mensanator

In multiplying a value of xe^-325 with ye^-4, Python is returning zero. How
do I get it to give me back my tiny value?

Use the right tool for the right job.
Help on built-in function mpf in module gmpy:

mpf(...)
mpf(n): builds an mpf object with a numeric value n (n may be any
Python number, or an mpz, mpq, or mpf object) and a
default
precision (in bits) depending on the nature of n
mpf(n,bits=0): as above, but with the specified number of bits (0
means to use default precision, as above)
mpf(s,bits=0,base=10): builds an mpf object from a string s made
up of
digits in the given base, possibly with fraction-part
(with
period as a separator) and/or exponent-part (with exponent
marker 'e' for base<=10, else '@'). If base=256, s must be
a gmpy.mpf portable binary representation as built by the
function gmpy.fbinary (and the .binary method of mpf
objects).
The resulting mpf object is built with a default precision
(in
bits) if bits is 0 or absent, else with the specified
number
of bits.
 
Z

Zentrader

In multiplying a value of xe^-325 with ye^-4, Python is returning zero. How
do I get it to give me back my tiny value?

Thanks!
Dee

Also, Python's decimal class allows theoretically unlimited
precision. I have extremely limited knowledge here. It gives the
following for 2**-325. I have no idea if the answer is correct.
You'll have to see if gmpy or decimal works better for this. One
piece of advice is to use whichever exclusively. If you use a float
and then covert to either one, the result will be corrupted.
import decimal
decimal.getcontext().prec = 375 ## set precision at 375
print "2**-325 =", decimal.Decimal(str(2**-325))

2**-325 = 1.46302386084E-98
 
M

mensanator

Also, Python's decimal class allows theoretically unlimited
precision. I have extremely limited knowledge here. It gives the
following for 2**-325. I have no idea if the answer is correct.

Looks ok, you should get about 0.3 as many decimal digits as bits.
You'll have to see if gmpy or decimal works better for this. One
piece of advice is to use whichever exclusively. If you use a float
and then covert to either one, the result will be corrupted.
import decimal
decimal.getcontext().prec = 375 ## set precision at 375
print "2**-325 =", decimal.Decimal(str(2**-325))

2**-325 = 1.46302386084E-98

Interestingly, using str() causes you to lose precision.'1.46302386084e-098'

Setting the precision to 375 didn't help because you
corrupted the value before you converted it to decimal.

Although the 375 will allow you to do this:
Decimal("0.0001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001")


You can also do this in gmpy, although you set precision in bits, not
decimal digits.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top