473,513 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

really small values

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
Jul 17 '07 #1
3 1307
On Jul 17, 4:13?pm, "Dee Asbury" <deeasb...@msn.comwrote:
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.
>>import gmpy
help(gmpy.mpf)
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.
>>a = gmpy.mpf('3e-325')
a
mpf('3.e-325')
>>b = gmpy.mpf('2e-4')
b
mpf('2.e-4')
>>a*b
mpf('6.e-329')

>
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

Jul 19 '07 #2
On Jul 17, 2:13 pm, "Dee Asbury" <deeasb...@msn.comwrote:
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

Jul 19 '07 #3
On Jul 19, 5:11?pm, Zentrader <zentrad...@gmail.comwrote:
On Jul 17, 2:13 pm, "Dee Asbury" <deeasb...@msn.comwrote:
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.
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.
>>a = 2**-325
a
1.463023860841312e-098
>>b = str(a)
b
'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:
>>a = decimal.Decimal('1e-325')
b = decimal.Decimal('1e-4')
a
Decimal("1E-325")
>>b
Decimal("0.0001")
>>a+b
Decimal("0.000100000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 000000000000000000000000000000000001")
You can also do this in gmpy, although you set precision in bits, not
decimal digits.

Jul 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
4906
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
5
1700
by: Rob R. Ainscough | last post by:
This is more of a conceptual question: 1. More and more companies are using VPN's and locking out internet connectivity (for a host of reasons, security, productivity, etc.). 2. ASP.NET...
1
2528
by: qvx | last post by:
I'm writing a small project and I decided to try pysqlite. The database consists of one master table with five columns and two detail tables with one and two columns each (not counting foreign key...
131
6021
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
15
2395
by: sparks | last post by:
We get more and more data done in excel and then they want it imported into access. The data is just stupid....values of 1 to 5 we get a lot of 0's ok that alright but 1-jan ? we get colums...
8
2327
by: Vincent RICHOMME | last post by:
Hi, first I would like to apologize about my question because usually I hate specific implementation but in this case I really would like an answer. Besides my example is an example of what's...
9
6584
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
9
2001
by: Sidraa | last post by:
hello i hav come across this small problem...i made a small project using Access...now what happens is when i enter the data through a form it gets calculated by a query and then gets stored in a...
50
2480
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
0
2178
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.