Yes, I've done my research, and the closest page I could find to my question is: this page and it is not in the least helpful.
That said, is it safe (reliable) to use floating-point values as keys in a Swift Dictionary ? Specifically, CGFloat, which the Apple docs say is actually a Double on 64-bit systems.
P.S. (This is important) - My CGFloat values will always end with .0 or .5 as fractional parts. Examples - 13, 13.5, 14, 14.5, etc. Will this make a difference to the reliability of such Dictionary lookups ?
var dictionary: [CGFloat: MyObject] = [:]
...
// Could this type of lookup fail ?
if let mappedObject = dictionary[13.5] {
...
}
I have a unique use case that requires mapping certain objects to these CGFloat values for quick lookups later.
I know from my experience in unit testing, that XCTAssertEqual sometimes fails when comparing two floating-point types, without the use of an "accuracy" parameter. So, I'm afraid that using CGFloats as keys will cause failed subsequent lookups as the keys passed in may be off by 0.0000000001 or something like that, because of the underlying representation of floating-point types.
Thank you.
EDIT - (Thanks @Sweeper) Yes, I did think of using Int keys by converting the CGFloat to Int using the following conversion:
intKey = Int(lroundf(Float(cgFloatKey * 2.0))
But then, I'm concerned about this ^ conversion (which has to be done on each lookup) requiring enough overhead to make it not worth it.
I might be totally wrong here and maybe these operations are lightning-fast. Can anyone comment on the efficiency of such an operation every single time a lookup is performed ?