Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in astype float32 vs float64 for integer

I'm sure this is due to a lapse in my understanding in how casting between different precision of float works, but can someone explain why the value is getting cast as 3 less than its true value in 32 vs 64 bit representation?

>>> a = np.array([83734315])
>>> a.astype('f')
array([ 83734312.], dtype=float32)
>>> a.astype('float64')
array([ 83734315.])
like image 525
jsexauer Avatar asked Mar 22 '23 17:03

jsexauer


2 Answers

A 32-bit float can exactly represent about 7 decimal digits of mantissa. Your number requires more, and therefore cannot be represented exactly.

The mechanics of what happens are as follows:

A 32-bit float has a 24-bit mantissa. Your number requires 27 bits to be represented exactly, so the last three bits are getting truncated (set to zero). The three lowest bits of your number are 0112; these are getting set to 0002. Observe that 0112 is 310.

like image 182
NPE Avatar answered Mar 27 '23 19:03

NPE


A float32 only has 24 bits of significand precision, which is roughly seven digits (log10(2**24) = 7.22). You're expecting it to store an 8-digit number exactly, which in general is impossible.

like image 30
Fred Foo Avatar answered Mar 27 '23 18:03

Fred Foo