Hailiang Zhang
Hailiang Zhang

Reputation: 18950

Is there anyway I can ask python to explicitly treat all floats as 32bits even on a 64bit machine?

Is there anyway I can ask python to explicitly treat all floats as 32bits even on a 64bit machine?

I don't want to change the codes...

Upvotes: 2

Views: 895

Answers (3)

wisty
wisty

Reputation: 7061

http://docs.python.org/tutorial/floatingpoint.html

Almost all machines today (July 2010) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”. 754 doubles contain 53 bits of precision, so on input the computer strives to convert 0.1 to the closest fraction it can of the form J/2**N where J is an integer containing exactly 53 bits.

It looks like you will have the same sized floats on a 64 and 32 bit machine, neither of which will be 32 bits.

If you must have 32 bit floats, you can either use a virtual machine to run a version of python that does use 32 bit floats (though I've no idea if such a thing exists), or compile it yourself (no idea how).

You should be able to use the 64 bit python version on a 32 bit machine, though it shouldn't change the way floats work. I think the main difference between 32 bit and 64 bit python is RAM use - 64 bit can use more RAM, but it's also more RAM hungry.

Upvotes: 1

Raymond Hettinger
Raymond Hettinger

Reputation: 226644

This size of the float is baked in at compilation time. You can pass in a compile option to treat a C double as a float in a fresh python executable file.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613451

Python's float is built-in and set in stone. You can't change that. You could use float32 from numpy.

Upvotes: 3

Related Questions