1

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.

erap129
  • 910
  • 1
  • 8
  • 17

1 Answers1

1

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
dawg
  • 98,345
  • 23
  • 131
  • 206
  • Thanks, though I was kinda hoping for something more general that would really emulate a 32-bit environment without me having to think about it all the time. Maybe my wish can't be granted so easily... – erap129 Jun 01 '21 at 15:18
  • You can force your Python into 32 bits which will force Numpy into 32 bits. You can do this by 1) [setting an environment variable](https://stackoverflow.com/questions/33709391/using-multiple-python-engines-32bit-64bit-and-2-7-3-5) when you compile or 2) download a 32 bit version. You may have to downgrade several versions of both Python and Numpy. – dawg Jun 01 '21 at 16:49