ios - NSNumbers with value 0.5 and 1.0 have the same hash -
ios - NSNumbers with value 0.5 and 1.0 have the same hash -
can please confirm, , explain, why happens:
on simulator (7.1, 32-bit):
nsnumber *a = [nsnumber numberwithfloat:0.5]; // hash = 506952114 nsnumber *b = [nsnumber numberwithfloat:1.0]; // hash = 2654435761 nsnumber *c = [nsnumber numberwithfloat:2.0]; // hash = 1013904226
on device (7.1, 32-bit):
nsnumber *a = [nsnumber numberwithfloat:0.5]; // hash = 2654435761 nsnumber *b = [nsnumber numberwithfloat:1.0]; // hash = 2654435761 - same! nsnumber *c = [nsnumber numberwithfloat:2.0]; // hash = 5308871522
i thought might 32-bit issue, when seek same thing on 64-bit simulator , device, same issue. simulator fine, device has identical hashes.
i trying add together unique objects nsmutableorderedset
, noticed 2 objects identical except differing values of 0.5 , 1.0 not both beingness added, , why. tried both floats , doubles same result.
but why?
i think excellent article mike ash might give insight:
for floats integer values, want same thing. since our isequal: considers integer-valued double equal int or uint of same value, must homecoming same hash int , uint equivalent. accomplish this, check see if double value integer, , homecoming integer value if so:
if(_value.d == floor(_value.d)) homecoming [self unsignedintegervalue];
(i won't quote whole section hash
, please read article total disclosure).
but, bottom line, looks using [nsnumber hash]
bad thought key in associative array/hash table. cannot explain why behaves differently under simulator , device; looks troubling...
ios objective-c nsnumber
Comments
Post a Comment