Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ comparing two floating point values

I'm wondering what is the difference for comparing two double between this two manner :

double a1 = ...;
double a2 = ....;
  1. fabs(a1-a2) < epsilon
  2. (fabs(a1-a2)/a2) < epsilon

Is there a prefering way to do that ?

thanks

like image 917
Guillaume Paris Avatar asked Feb 21 '11 09:02

Guillaume Paris


1 Answers

This article answers your question quite thoroughly, I think. You might want to skip ahead to the section "Epsilon comparisons".

like image 161
Martin Stone Avatar answered Oct 29 '22 22:10

Martin Stone