Skip to content
Snippets Groups Projects
Commit e8c61eaa authored by Stéphane Poirier's avatar Stéphane Poirier
Browse files

fp_is_equal: if no precision is provided then use std::numeric_limits::epsilon

parent 4869fed9
Branches
Tags
No related merge requests found
//----------------------------------------------------------------------------
// Copyright (c) 2004-2021 Synchrotron SOLEIL
// Copyright (c) 2004-2024 Synchrotron SOLEIL
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the GNU Lesser Public License v3
// which accompanies this distribution, and is available at
......@@ -48,6 +48,7 @@
#endif
#include <assert.h>
#include <limits>
#include <yat/Portability.h>
#include <yat/Inline.h>
#include <yat/LogHelper.h>
......@@ -81,7 +82,7 @@ namespace yat
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Free function fuzzy float point numbers comparison
//! Fuzzy float point numbers comparison
//-----------------------------------------------------------------------------
template<typename T>
inline bool fp_is_equal(T a, T b, T precision)
......@@ -95,6 +96,21 @@ inline bool fp_is_equal(T a, T b, T precision)
return std::abs(b-a) <= ( std::abs(precision) * std::abs(b) );
}
//-----------------------------------------------------------------------------
//! Accurate comparison of float numbers
//-----------------------------------------------------------------------------
template<typename T>
inline bool fp_is_equal(T a, T b)
{
if( std::abs(a-b) < std::numeric_limits<T>::epsilon() ) // work better near zero
return true;
if( std::abs(a) > std::abs(b) )
return std::abs(a-b) <= ( std::numeric_limits<T>::epsilon() * std::abs(a) );
else
return std::abs(b-a) <= ( std::numeric_limits<T>::epsilon() * std::abs(b) );
}
//-----------------------------------------------------------------------------
// Expanding Macros into string constants
// The MACRO_TO_STRING macro calls _STR_EXPAND with its argument. The parameter is checked for macro
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment