3 ms·
The goal of my article is to make them seem a little less fickle and mysterious. I'm trying to focus on what can be understood, and not the parts that defy eas
by haberman 12y ago
The goal of my article is to make them seem a little less fickle and mysterious. I'm trying to focus on what can be understood, and not the parts that defy easy understanding. :)
- forrestthewoods 12y agoMy current favorite float fact is that taking a valid integer and casting it to a float and back to int can cause an integer overflow. Alternatively the final value could be either less than or greater than the original! Floats are dumb. I use them every day but they're dumb and I hate them! <grumble>
- groby_b 12y agoThey might seem dumb, but on closer look IEEE754 (which is what you probably mean when you say float) is a pretty clever piece of engineering. Here's a great bit of info, interviewing professor Kahan (one of the principals behind it) http://www.cs.berkeley.edu/~wkahan/ieee754status/754story.html http://www.cs.berkeley.edu/~wkahan/ieee754status/754story.ht... Believe me, they might be painful, but they're definitely one of the least painful ways to handle non-integer values.
- chewxy 12y agoCome up with something that can represent every number then? IEEE754 may be a pain in the ass, but dumb is not a word I'd use to describe it
- ygra 12y agoThe problem is, I guess, that people assume that floating-point numbers represent real numbers and behave like those. But while they're an often useful approximation, they are governed by very different laws. The same holds for integers in computers (except bigints), but the only place where they bite you is overflow and not by unexpected behaviour over the whole range of numbers.
- acqq 12y agoWithout trying or thinking people just don't expect the effects which have sense once you know what is actually going on: void p( int i ) { float f = i; int i2 = f; printf( "%d\n%f\n%d\n\n", i, f, i2 ); } int main() { p( 0x7fffffbf ); p( 0x7fffffc0 ); } /* prints 2147483583 2147483520.000000 2147483520 2147483584 2147483648.000000 -2147483648 */ I wouldn't call the behavior dumb, nor would I involve hate. You get the results that correspond to what you programmed, not what you wish to happen and what would actually not be feasible, as long as you insist on using float.
- brucedawson 12y agoThe round-tripping behavior is certainly cool, but my favorite float-fact is still this one: double pi = 3.14159265358979323846; printf("pi = %.33f\n + %.33f\n", pi, sin(pi)); sin(double(pi)) is equal to the error in the double(pi) constant. If you manually add the two you can easily get pi to about 34 digits. Useless, but cool. I discuss this in more detail here: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ http://randomascii.wordpress.com/2012/02/25/comparing-floati...
- forrestthewoods 12y agoif I didn't encounter it personally it can't possibly be my favorite