[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [IMP-dev] Be very careful with floating point numbers



Or, more generally, never count on two floating point operations to return consistent answers.
Recently someone posted to one of the C++ newsgroups about a crash  
while sorting points based on the distance from another point. The  
sorting was done by, at each comparison, computing the distance of the  
two points being compared to the reference point and then comparing  
this sorted distance. Unfortunately, the results of this comparison  
depended on details such as whether the compiler chose to store the  
result of the computation in an 80 bit or 64 bit register (since  
truncation could change the order of two values) and so the  
comparisons did not form a total ordering on the points and as a  
result sorting would bomb out with some compilers and some  
optimization settings.
On Mar 25, 2009, at 9:11 PM, Ben Webb wrote:

Perhaps a useful rule of thumb: if you ever find yourself writing "=="
with floating point numbers, stop and think: what you are doing is
probably wrong. ;)

	Ben
--
ben@salilab.org                      http://salilab.org/~ben/
"It is a capital mistake to theorize before one has data."
	- Sir Arthur Conan Doyle
_______________________________________________
IMP-dev mailing list
IMP-dev@salilab.org
https://salilab.org/mailman/listinfo/imp-dev