Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The real difference between float32 and float64

I want to understand the actual difference between float16 and float32 in terms of the result precision. For instance, Numpy allows you to choose the range of the datatype you want (np.float16, np.float32, np.float64). My concern is that if I decide to go with float 16 to reserve memory and avoid possible overflow, would that create a loss of the final results comparing with float32 for instance?

Thank you

like image 567
A_D Avatar asked Apr 16 '17 18:04

A_D


People also ask

Is float32 faster than float64?

float64 is much slower than Python's float, and numpy. float32 is even slower (even though I'm on a 32-bit machine).

How accurate is float32?

Looks float32 has a resolution of 1e-6 and the abs value is valid down to as small as 1.2e-38 . The relative error is at the order of 1e-8 for values above 1e-38, lower than 1e-6 proposed by np. finfo and the error is still acceptable even if the value if lower than the tiny value of np. finfo .

What is the difference between Numpy float64 and float?

float64 are numpy specific 32 and 64-bit float types. Thus, when you do isinstance(2.0, np. float) , it is equivalent to isinstance(2.0, float) as 2.0 is a plain python built-in float type... and not the numpy type.

What is the difference between a 32 bit and 64-bit floating point value?

Floating Point Numbers Floats generally come in two flavours: “single” and “double” precision. Single precision floats are 32-bits in length while “doubles” are 64-bits. Due to the finite size of floats, they cannot represent all of the real numbers - there are limitations on both their precision and range.


2 Answers

a = np.array([0.123456789121212,2,3], dtype=np.float16) print("16bit: ", a[0])  a = np.array([0.123456789121212,2,3], dtype=np.float32) print("32bit: ", a[0])  b = np.array([0.123456789121212121212,2,3], dtype=np.float64) print("64bit: ", b[0]) 
  • 16bit: 0.1235
  • 32bit: 0.12345679
  • 64bit: 0.12345678912121212
like image 109
Furkan Gulsen Avatar answered Sep 19 '22 16:09

Furkan Gulsen


float32 is a 32 bit number - float64 uses 64 bits.

That means that float64’s take up twice as much memory - and doing operations on them may be a lot slower in some machine architectures.

However, float64’s can represent numbers much more accurately than 32 bit floats.

They also allow much larger numbers to be stored.

For your Python-Numpy project I'm sure you know the input variables and their nature.

To make a decision we as programmers need to ask ourselves

  1. What kind of precision does my output need?
  2. Is speed not an issue at all?
  3. what precision is needed in parts per million?

A naive example would be if I store weather data of my city as [12.3, 14.5, 11.1, 9.9, 12.2, 8.2]

Next day Predicted Output could be of 11.5 or 11.5164374

do your think storing float 32 or float 64 would be necessary?

like image 21
Gurupratap Avatar answered Sep 16 '22 16:09

Gurupratap