Easy way to test equality
(its been a while since i've been coding in c++, so bear with me)
int32 *a;int32 *b*a = &floata*b = &gloatbif !(a ^ b) { // Floats are equal, exactly} else { // Floats are not equal}
Easy way.
hard way
int32 *aint32 *bbool sabool sbint32 fracaint32 fracbint32 mantaint32 mantb*a = &floata*b = &floatbSa = a & b10000... you get the piectureSb = b & b100000 same hereFraca = a & b011111111fracb = b & b011111111Manta = a & b0000000001111111111111111111111Mantb = b & b0000000001111111111111111111111
This seperates the sign bits, fractions and mantissas from the floats.
You first check wiether the sign bits are the same, if there not then they arn't equal (i know this is a generalisation, but for most numbers that you'll be using, it'll hold true).
You can then use the mantissa to do the first equality test, ie, they should be resimably similar. just suptract them, & out the sign bit, and check if its < 2 or 3.
you then find out the difference between the two fractions, (just subract them, & out the sign bit, and see if there close enough).
And if it takes all that, then actually calculate both as integers.
You then get both ints, take the difference between them (& out the sign bit again), and divide that by the sum of the floats.
You then check weither this error is small enough to be ok.
Quote:Original post by Dmytry
Branching is costly, so handling such cases specially is not really much faster.
Actually brancing is quite fast nowdays. maybe on an 8080 it may have been very costly, but now, its really cheap. (read up on branch prediction)
HTH
From,
Nice coder