views:

199

answers:

4

What's the best way to emulate single-precision floating point in python? (Or other floating point formats for that matter?) Just use ctypes?

+5  A: 

numpy has a float32 type.

Ignacio Vazquez-Abrams
A: 

If your application suits arrays/matrices, you can use numpy with float32

gnibbler
+1  A: 

If numpy (the excellent suggestion of other answers) is inapplicable for you (e.g. because you're in an environment that doesn't allow arbitrary third-party extensions), the array module in Python standard library is fine too -- type code 'f' gives you 32-bit floats. Besides those and the (usual) double precision floats, there isn't much for "other floating point formats" -- what did you have in mind? (e.g. gmpy offers GMP's modest support for floats with much longer, arbitrary bit sizes -- but it's modest indeed, e.g., no trig functions).

Alex Martelli
+1  A: 

how about ctypes.c_float from standard library?

Kugel