Re: [Dev-C++] Why don't you ever compare floats for equality?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-01-09 20:38:38
|
Because ((1.0 / 3.0) * 3.0) == 1.0 isn't guaranteed to be true because of round-off errors in evaluation. Floating point nubmers are able to store a number of significant digits - about 7-8 for float and 15 for double. However, only some numbers are possible to store exactly in a floating point variable. Most values are only possible to store as approximations, i.e. rounded or truncated to the number of available significant digits. You need an infinite number of digits to store the value 1/3 for example. Also, note that while we normally think of our floats and doubles as decimal values, they are stored in binary format in the computer, resulting in conversion errors when switching number base, i.e. when reading in or printing a number. /Per W On Thu, 9 Jan 2003, Daniel Glenfield wrote: > I read that you should never compare floating point numbers for equality. I > was wondering why? > > > > > > _________________________________________________________________ > Help STOP SPAM: Try the new MSN 8 and get 2 months FREE* > http://join.msn.com/?page=features/junkmail > > > > ------------------------------------------------------- > This SF.NET email is sponsored by: > SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See! > http://www.vasoftware.com > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |