32 bit or 64 bit?

R

ram.rachum

Quick question:
I have python code that does a lot of floating point arithmetic. How
do I make it do the arithmetic in 64 bit? (I have a 64 bit CPU.) If
I'll install a 64-bit operating system, will that do the trick?
 
P

Peter Otten

Quick question:
I have python code that does a lot of floating point arithmetic. How
do I make it do the arithmetic in 64 bit? (I have a 64 bit CPU.) If
I'll install a 64-bit operating system, will that do the trick?

The Python float type uses a C double internally which is 64 bit even on 32
bit CPUs.

Peter
 
R

ram.rachum

The Python float type uses a C double internally which is 64 bit even on 32
bit CPUs.

Peter

Does it mean that even now it does arithmetic in 64 bit?
I'm not getting enough precision. Is there any way to increase it?

Ram.
 
P

Peter Otten

C

Christian Heimes

Does it mean that even now it does arithmetic in 64 bit?
I'm not getting enough precision. Is there any way to increase it?

Buy a good book about numerics or take a course. ;)

Seriously, computers and IEEE 754 floating point numbers have a lot of
pit falls. If you chose the wrong algorithm you *will* get wrong
results. With floats a*b*c and a*c*b can give you a different result.

The decimal module is not an option if you need speed.
 
R

ram.rachum

Buy a good book about numerics or take a course. ;)

Seriously, computers and IEEE 754 floating point numbers have a lot of
pit falls. If you chose the wrong algorithm you *will* get wrong
results. With floats a*b*c and a*c*b can give you a different result.

The decimal module is not an option if you need speed.

I do need speed. Is there an option?
 
R

ram.rachum

Mind telling us what you *actually* want to achieve? (What do you want to
calculate?)

Christian

Physical simulations of objects with near-lightspeed velocity.
 
P

Peter Otten

Physical simulations of objects with near-lightspeed velocity.

How did you determine that standard python floats are not good enough?
Everything beyond that is unlikely to be supported by the hardware and will
therefore introduce a speed penalty.

Did you try gmpy?

Peter
 
C

Christian Meesters

Peter said:
How did you determine that standard python floats are not good enough?
Everything beyond that is unlikely to be supported by the hardware and
will therefore introduce a speed penalty.

Did you try gmpy?
I would like to add: If Python's precision (or that of additional modules)
won't do it for you, look around what other people in the physics community
are using.

Christian
 
R

ram.rachum

How did you determine that standard python floats are not good enough?

I have a physical system set up in which a body is supposed to
accelerate and to get very close to lightspeed, while never really
attaining it. After approx. 680 seconds, Python gets stuck and tells
me the object has passed lightspeed. I put the same equations in
Mathematica, again I get the same mistake around 680 seconds. So I
think, I have a problem with my model! Then I pump up the
WorkingPrecision in Mathematica to about 10. I run the same equations
again, and it works! At least for the first 10,000 seconds, the object
does not pass lightspeed.
I concluded that I need Python to work at a higher precision.
Everything beyond that is unlikely to be supported by the hardware and will
therefore introduce a speed penalty.

I have thought of that as well. However I have no choice. I must do
these calculations. If you know of any way that is supported by the
hardware, it will be terrific, but for now the slower things will have
to do.


Did you try gmpy?

Not yet: I was kind of set back when I saw their homepage was last
updated 2002. But I'll give it a try. You think it's the best thing
there is?

Thanks,
Ram.
 
P

Peter Otten

I have a physical system set up in which a body is supposed to
accelerate and to get very close to lightspeed, while never really
attaining it. After approx. 680 seconds, Python gets stuck and tells
me the object has passed lightspeed. I put the same equations in
Mathematica, again I get the same mistake around 680 seconds. So I
think, I have a problem with my model! Then I pump up the
WorkingPrecision in Mathematica to about 10. I run the same equations
again, and it works! At least for the first 10,000 seconds, the object
does not pass lightspeed.

That the values are possible doesn't mean that you can trust them.
I concluded that I need Python to work at a higher precision.

How is WorkingPrecision defined? Python floats have about 16 significant
digits in base 10, so at first glance I would guess that you switched to
a /lower/ precision.

But I've come to agree with Christian that it would be good to show your
model to a physics and/or numerical maths expert. Perhaps you can find a
way for the errors to cancel out rather than accumulate.

Peter
 
C

casevh

Not yet: I was kind of set back when I saw their homepage was last
updated 2002. But I'll give it a try. You think it's the best thing
there is?

Thanks,
Ram.

gmpy has moved to Google.

http://code.google.com/p/gmpy/

gmpy only support the basic floating point operations so it may not be
sufficient for your needs.

A couple alternatives:

1) mpmath is a pure-Python, arbitrary precision floating-point
package. It will be faster than Decimal but slower than gmpy.
http://code.google.com/p/mpmath/

2) Sage is an open-source mathematics software package. It uses Python
as it glue/scripting language. It includes support for MPFR, a
multiple-precision floating point library based on GMP. www.sagemath.org

casevh
 
M

Mensanator

I have a physical system set up in which a body is supposed to
accelerate and to get very close to lightspeed, while never really
attaining it. After approx. 680 seconds, Python gets stuck and tells
me the object has passed lightspeed. I put the same equations in
Mathematica, again I get the same mistake around 680 seconds. So I
think, I have a problem with my model! Then I pump up the
WorkingPrecision in Mathematica to about 10. I run the same equations
again, and it works! At least for the first 10,000 seconds, the object
does not pass lightspeed.
I concluded that I need Python to work at a higher precision.


I have thought of that as well. However I have no choice. I must do
these calculations. If you know of any way that is supported by the
hardware, it will be terrific, but for now the slower things will have
to do.


Not yet: I was kind of set back when I saw their homepage was last
updated 2002.

Try looking here:

http://code.google.com/p/gmpy/

The developers have abandoned SourceForge.
But I'll give it a try. You think it's the best thing
there is?

I haven't tried everything, but it's very good.
You might also want to go to the GMP site itself
and get their manual. Likee anything else, your
results will be no better than your algorithms.
 
T

Terry Reedy

|> How did you determine that standard python floats are not good enough?

| I have a physical system set up in which a body is supposed to
| accelerate and to get very close to lightspeed, while never really
|attaining it.

Just a thought. You might do better if you can rearrange your equations in
terms of c-v instead of v. Letting c=1, you cannot accutrately express
v = .99999999999999999999
in Python, but can easily express
1-v = .00000000000000000001.
And so on.

tjr
 
R

ram.rachum

|> How did you determine that standard python floats are not good enough?

| I have a physical system set up in which a body is supposed to
| accelerate and to get very close to lightspeed, while never really
|attaining it.

Just a thought.  You might do better if you can rearrange your equations in
terms of c-v instead of v.  Letting c=1, you cannot accutrately express
v = .99999999999999999999
in Python, but can easily express
1-v = .00000000000000000001.
And so on.

tjr

Good idea Terry, I'll think about it.
 
R

ram.rachum

That the values are possible doesn't mean that you can trust them.

I do not understand this comment.
How is WorkingPrecision defined? Python floats have about 16 significant
digits in base 10, so at first glance I would guess that you switched to
a /lower/ precision.

I don't know how WorkingPrecision is defined. However, I think it's
not lower, it's higher.
But I've come to agree with Christian that it would be good to show your
model to a physics and/or numerical maths expert. Perhaps you can find a
way for the errors to cancel out rather than accumulate.

I might try that.
 
R

ram.rachum

gmpy has moved to Google.

http://code.google.com/p/gmpy/

gmpy only support the basic floating point operations so it may not be
sufficient for your needs.

A couple alternatives:

1) mpmath is a pure-Python, arbitrary precision floating-point
package. It will be faster than Decimal but slower than gmpy.http://code.google.com/p/mpmath/

2) Sage is an open-source mathematics software package. It uses Python
as it glue/scripting language. It includes support for MPFR, a
multiple-precision floating point library based on GMP.  www.sagemath.org

casevh

Thanks a bundle!
 
R

ram.rachum

Try looking here:

http://code.google.com/p/gmpy/

The developers have abandoned SourceForge.


I haven't tried everything, but it's very good.
You might also want to go to the GMP site itself
and get their manual. Likee anything else, your
results will be no better than your algorithms.

I'll check it out, thanks.
 
C

Christian Heimes

I have a physical system set up in which a body is supposed to
accelerate and to get very close to lightspeed, while never really
attaining it. After approx. 680 seconds, Python gets stuck and tells
me the object has passed lightspeed. I put the same equations in
Mathematica, again I get the same mistake around 680 seconds. So I
think, I have a problem with my model! Then I pump up the
WorkingPrecision in Mathematica to about 10. I run the same equations
again, and it works! At least for the first 10,000 seconds, the object
does not pass lightspeed.
I concluded that I need Python to work at a higher precision.

I conclude that your algorithm is numerical wrong. It probably suffers
from a rounding error which increases itself in every iteration.
Increasing the precision doesn't solve your problem. It's only going to
hide the fact that your algorithm doesn't do its job.

Please don't get me wrong. I don't want to imply that you are an idiot
who doesn't know what he is doing. :] Most likely you weren't taught how
to write numerical sound algorithms. Let's all blame your school or
university. *g*

Numerics is a complex area and it took me more than a year to learn the
basics. Don't be embarrassed!
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top