0

In a situation where we want to use floats instead of human-readable name strings for fast comparisons, are there usual methods for doing the conversion? The outcome of the comparison is a code-driven event, so there's no need to convert back again- it just has to be a very efficient process that creates a unique identifier.

Thanks!

Robbie Cooper
  • 483
  • 2
  • 5
  • 13
  • 1
    The question does not make any sense at all – Ed Heal Sep 04 '14 at 18:06
  • 2
    why floats? You could make a an integer hashcode, see http://stackoverflow.com/questions/114085/fast-string-hashing-algorithm-with-low-collision-rates-with-32-bit-integer – thumbmunkeys Sep 04 '14 at 18:06
  • 2
    You could use any hashing function to get an integer, then convert the integer to a double. But there's no guarantee that the hash will be unique. – Mark Ransom Sep 04 '14 at 18:07
  • Look up "string hash code". The values wouldn't be unique, but the search would be a lot faster. – Sergey Kalinichenko Sep 04 '14 at 18:08
  • @MarkRansom - Integers to floats is not precise either. – Ed Heal Sep 04 '14 at 18:08
  • Note that there's some floating point numbers such as NAN that aren't comparable, so you're better off using an integer if that's really the way you go. – Mark Ransom Sep 04 '14 at 18:08
  • 2
    @MarkRansom equality comparisons on floats are often problematic – thumbmunkeys Sep 04 '14 at 18:08
  • @thumbmunkeys, just got done pointing that out, thanks. Although it's not a problem with floats generated by conversion from int. – Mark Ransom Sep 04 '14 at 18:09
  • @MarkRansom but then theres no point using a float, isn't it :) I think the question needs to be clarified – thumbmunkeys Sep 04 '14 at 18:10
  • 1
    @EdHeal it is if the integer is small enough. A double can hold a 53 bit unsigned integer without loss. – Mark Ransom Sep 04 '14 at 18:10
  • Ok, not sure why that doesnt make sense. That was useful thumbmunkeys, thanks. Just found this as a result of your comment http://stackoverflow.com/questions/8094790/how-to-get-hash-code-of-a-string-in-c – Robbie Cooper Sep 04 '14 at 18:10
  • @MarkRansom - Depends on the platform. Some platforms the ints are 64 bits – Ed Heal Sep 04 '14 at 18:11
  • 1
    @RobbieCooper ok, I voted to close your question then – thumbmunkeys Sep 04 '14 at 18:12
  • 1
    @EdHeal just because you don't understand the question, doesn't automatically mean it doesn't make any sense. – wvdz Sep 04 '14 at 18:12
  • 1
    @popovitsj - It does not make any sense in as much as it does not have any logic to it – Ed Heal Sep 04 '14 at 18:13
  • Ok, this is all interesting stuff. So what do people do to create unique numbers? The strings are all generated before runtime, so is there another technique which addresses the same issue (speed) and guarantees unique results? – Robbie Cooper Sep 04 '14 at 18:13
  • 2
    You're looking for a perfect hash: http://en.wikipedia.org/wiki/Perfect_hash_function – Mark Ransom Sep 04 '14 at 18:16
  • 1
    @RobbieCooper _"So what do people do to create unique numbers?"_ They usually use [hashes](http://en.cppreference.com/w/cpp/utility/hash) (or [UUID](http://en.wikipedia.org/wiki/Universally_unique_identifier)'s) :P ... – πάντα ῥεῖ Sep 04 '14 at 18:17
  • 1
    Thanks all, that has completely answered my question! – Robbie Cooper Sep 04 '14 at 18:19

0 Answers0