NSNumbers with a value of 0.5 and 1.0 have the same hash

Can someone please acknowledge and explain why this is happening:

In the 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 the 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 it might be a problem with the 32-bit version, but when I try to do the same on a 64-bit simulator and device, I get an EXACT problem. The simulator is in order, the device has the same hashes.

I tried to add unique objects to NSMutableOrderedSetand noticed that both of my objects, which were identical, except for different values ​​of 0.5 and 1.0, were not added by both, and that is why. I tried both floats and doubles with the same result.

But why?

+4
source share
2

, :

float, , . isEqual: DOUBLE, INT UINT , , INT UINT . , , value , :

    if(_value.d == floor(_value.d))
        return [self unsignedIntegerValue];

( hash, , , ).

[NSNumber hash] - /. , - ; ...

+2

, .

, 2 ^ 32 -, NSSNumbers, .

, , . , , NSNumber isEqual.

NSSet - , @trojanfoe, , NSNumber .

, SHA512, , . MD5 SHA2 .

+2

Source: https://habr.com/ru/post/1545052/


All Articles