python reference about floating point number -


this question has answer here:

before question, here sample code. take @ first,please.

>>> id(1) 1636939440 >>> = 1 >>> b = 1 >>> c = 1 >>> id(a) 1636939440 >>> id(b) 1636939440 >>> id(c) 1636939440   >>> id("hello") 43566560 >>> = "hello" >>> b = "hello" >>> c = "hello" >>> id(a) 43566560 >>> id(b) 43566560 >>> id(c) 43566560    >>> id(3.14) 34312864 >>> = 3.14 >>> b = 3.14 >>> c = 3.14 >>> id(a) 34312864 >>> id(b) 34312600 >>> id(c) 34312432 

as see above, in terms of integer , string, python variable references object same way. floating point number works in different way. why that? there special reason that?

for small integers , strings python uses internal memory optimization. since variable in python reference memory object, python puts such small values memory once. then, whenever same value assigned other variable, makes variable point object kept in memory. works strings , integers immutable , if variable value changes, it's reference used variable changed, object in memory original value not affected.

first of all, floating point numbers not 'small', and, second, same 3.14 in memory depending on calculations might kept 3.14123123456789 , 3.14123987654321 (just example numbers explain). these 2 values 2 different objects, during calculations , displaying meaningful part looks same, i.e. 3.14 (in fact there's many more possible values in memory same floating point number). that's why reusing same floating point number object in memory problematic , doesn't worth after all.

see more on how floating point numbers kept in memory here:

http://floating-point-gui.de/

http://docs.python.org/2/tutorial/floatingpoint.html

also, there's big article on floating point numbers @ oracle docs.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -