Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Unfortunately C++ STL does not offer anything close to isclose

maybe because you can trivially write it yourself with whatever precision you want:

   float a = 0.1;
   float b = 0.2;
  
   // now test for equality to within half of the smallest
   // value float can represent
 
   bool equal = fabs (0.3 - (a + b)) < (FLT_EPSILON/2);
Alternatively:

   bool isclose (float v1, float v2, float prec = FLT_EPSILON/2) { 
        return fabs (v1 - v2) < prec;
   }
         
Variations are left to the reader. There are a few optimizations one can do but this version conveys the basic point: you get to specify the semantics of isclose()



Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: