erap129
erap129

Reputation: 960

auto-casting to 32 bit in Python

I wish to emulate a 32 bit environment in Python. Thus, I must make all operations occur as if they were happening on a 32 bit machine.
Example -
Take the following random calculation:

1.2229 * ((290 * (7323412.64 - 0.994 * 50)) / (1000 * (-22.9 + 273.15))) / 170

The output is: 61.04871026396052.

Now, if I cast each argument using numpy, it turns out a bit different (each argument must be cast so no operation whatsoever will occur in the default 64 bit):

np.float32(1.2229) * ((np.float32(290) * (np.float32(7323412.64) - 
np.float32(0.994) * np.float32(50))) / (np.float32(1000) * 
(np.float32(-22.9) + np.float32(273.15)))) / np.float32(170)  

The output is: 61.048714

Is there any way to do what I just did above in a less verbose and explicit way? Thanks.

Upvotes: 1

Views: 466

Answers (1)

dawg
dawg

Reputation: 104092

You can make an array of 32 bit types:

a=np.array([1.2229,290,7323412.64, 0.994,50,1000,-22.9,273.15,170],dtype=np.float32)

Then:

>>> a[0] * ((a[1] * (a[2] - a[3] * a[4])) / (a[5] * (a[6] + a[7]))) / a[8]
61.048714

Upvotes: 1

Related Questions