[Tutor] How to assert a float?

dman dsh8290@rit.edu
Tue, 8 Jan 2002 18:18:26 -0500


On Mon, Jan 07, 2002 at 11:50:08PM +0100, Willi Richert wrote:
 
| 2**log2(5) yields 4.9999999999999991 which is not 5.0

Welcome to finite precision binary floating point.  Try performing the
operations in finite precision binary floating point.  It is the same
effect as doing 2/3 in finite precision decimal floating point
(commonly just called "decimal").

The solution is to never check floats for equality -- they will more
often (after some computations, of course) be close enough, but not
equal, to what you wanted.

-D

-- 

Even youths grow tired and weary,
    and young men stumble and fall;
but those who hope in the Lord 
    will renew their strength.
They will soar on wings like eagles;
    they will run and not grow weary,
    they will walk and not be faint.

        Isaiah 40:31