top | item 31979854

(no title)

lzsiga | 3 years ago

Off: floating point numbers can be used to store integer values, so equality comparison might be perfectly valid in some cases. For example, if the embedded Sql doesn't support 'int64_t' (only int32_t), itt might still support 'double' which can store 52-bit integers exactly.

discuss

order

ajuc|3 years ago

Yes, also there are deep magic gamedev tricks exploiting that (see quake sqrt :) ). There should be a way to do them. Something like bit_compare(x, y). I'd just prefer if rarely useful operations shouldn't use the most commonly used API. Too easy to make a mistake.

BTW I'd also love to have a built in float type that fails when you assign 0 to it.

Anyway, I guess in Java operator == is a lost cause anyway. My favorite example:

    // true
    System.out.println(BigDecimal.valueOf(10) == BigDecimal.valueOf(10));
    // false
    System.out.println(BigDecimal.valueOf(11) == BigDecimal.valueOf(11));
    // true
    System.out.println(BigDecimal.valueOf(11).equals(BigDecimal.valueOf(11)));

lelanthran|3 years ago

> Off: floating point numbers can be used to store integer values, so equality comparison might be perfectly valid in some cases.

Yeah, but then you're having to learn all the special cases for when it silently gives wrong answers, and hope to hell that you didn't miss any.

Much better to have consistency and behave the same way all the time, than to optimise for 3 keystrokes and introduce all sorts of special exceptions that the programmer must memorise.

Gibbon1|3 years ago

I think if you want to perform binary operations on floats you should have to cast them to a binary type.