Jeff Epler writes: > Can't you do it like so: > def cmp_long_vs_float(l, f): > try: > lf = float(l) > except OverflowError: > if special_float_value(f): # NaN, Inf > return cmp(0.0, f) > return cmp(l, 0L) > else: > return cmp(lf, f) I don't think so. There are three "problems" with comparing floats to longs. The float might have a fraction part (not be an integer), in which case we really need pay attention to it (perhaps by comparing floats). The long might be larger than the largest float, in which case we really need to pay attention to THAT (perhaps by comparing longs). Your code handles these. But there's another situation too... the long could be less than MAX_FLOAT, but still not be precisely representably as a float. In a case like this, we really need to convert both to longs and compare (or something equivalent) and your code fails to do this. What Andrew Koenig was suggesting (and I hope I'm understanding it properly, since I'd never realized this fact before I read his post) is that for any (sane) implementation of float, there is a crossover value. Below that value, the precision is larger than the exponent and there might be a fractional part. Above that value, the precision is smaller than the exponent, and so floats above that value must all be integers... and some integers above that value will not be representable as floats. Andrew's suggestion is to (losslessly) convert both to floats and compare if the float is below that value, and (losslessly) convert both to longs and compare if the float is above that value. The only bit that seems tricky to me is determining the crossover value in pure C. How do we do that? -- Michael Chermside This email may contain confidential or privileged information. If you believe you have received the message in error, please notify the sender and delete the message without copying or disclosing it.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4