Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force python float operation on float32 rather than float64?

I want to do some math operations (+, -, *, /) on float32 rather than on float64 type. I need do these operations on number or numpy.array, and also some numpy math functions, such as sqrt mean. How do I do this?

like image 470
Samuel Avatar asked Dec 25 '13 02:12

Samuel


People also ask

Should I use float32 or float64?

float32 is less accurate but faster than float64, and flaot64 is more accurate than float32 but consumes more memory. If accuracy is more important than speed , you can use float64. and if speed is more important than accuracy, you can use float32.

Is float32 same as float?

float is one of the available numeric data types in Go used to store decimal numbers. float32 is a version of float that stores decimal values composed of 32 bits of data.

Is float32 faster than float64?

At least on intel, float64 should be faster than float32 since all math is done on the fpu in 64 bits, so it needs to be converted, but the memory bus also comes into play.

Is Python float 32 or 64-bit?

Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to np. float64 . In some unusual situations it may be useful to use floating-point numbers with more precision.


1 Answers

Will numpy.float32 help?

>>>PI=3.1415926535897
>>> print PI*PI
9.86960440109
>>> PI32=numpy.float32(PI)
>>> print PI32*PI32
9.86961

If you want to do math operation on float32, convert the operands to float32 may help you.

like image 145
WKPlus Avatar answered Sep 27 '22 18:09

WKPlus