Live data from Hacker News

Comparing Integers and Doubles

databasearchitects.blogspot.com

11–14 of 14 posts

Re: Comparing Integers and Doubles

#11

or you could learn about how to do comparisons with floating point numbers

like multiplying them by the precision that you'd like to compare and comparing them as integers? /s

That won't work; as integers, 100.02 and 99.997 are unequal, but 1.0002 and 0.99997 are equal at 0.01 precision. (And indeed also equal at 0.001 precision!) You'd need to round.

I had the impression that the usual way to compare floats is to define a precision and check for -p < (a - b) < p. In this case 0.99997 - 1.0002 = -0.00023, which correctly tells us that the two numbers are equal at 0.001 precision and unequal at 0.0001.

Re: Comparing Integers and Doubles

#12

Earlier quoted context omitted.

like multiplying them by the precision that you'd like to compare and comparing them as integers? /s

That won't work; as integers, 100.02 and 99.997 are unequal, but 1.0002 and 0.99997 are equal at 0.01 precision. (And indeed also equal at 0.001 precision!) You'd need to round. I had the impression that the usual way to compare floats is to define a precision and check for -p < (a - b) < p. In this case 0.99997 - 1.0002 = -0.00023, which correctly tells us that the two numbers are equal at 0.001 precision and unequa…

Rounding won't work either, at least if you're trying to find a way to do a hash join on float-comparison-within-epsilon. You would need to have a function such that |a-b|You can do it if you produce two hash values for each key (and clean up your duplicates later), but not if you produce only one.

Of course most of the time if you are doing equality comparisons on floats you have a fundamental conceptual problem with your code.

Re: Comparing Integers and Doubles

#14

Both ints and floats represent real, rational values, but every operation in no way matches math. Associative? No. Commutative? No. Partially Ordered? No. Weakly Ordered? No. Symmetric? No. Reflexive? No. Antisymmetric? No. Nothing. The only reasonable way to compare rationals is the decimal expansion of the string.

> The only reasonable way to compare rationals is the decimal expansion of the string.

Why decimal? I don’t see why any other integer base wouldn’t work, and, on about any system, doing 2^n for any n > 0* will be both easier to implement and faster to run.

And that, more or less, is what the suggested solution does. It first compares the first 53 bits and, if that’s not conclusive, it compares 64 bits.

Also, of course, if your number has more than n bits, you’d only generate digits until you know the answer.

Post reply on HN