Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Genetic Algorithm Library

, 7 Apr 2012 GPL3
A framework for genetic algorithms
galdemoapps.zip
TSP.exe
GeneticLibrary.dll
TestApp1.exe
TestApp2.exe
GaSchedule.exe
GaSchedule.cfg
GalDemoSource.zip
TestApp1
TestApp1.vcxproj.filters
TestApp2
TestApp2.vcxproj.filters
TSP
res
.svn
entries
prop-base
Toolbar.bmp.svn-base
TSP.ico.svn-base
props
text-base
Toolbar.bmp.svn-base
TSP.ico.svn-base
TSP.rc2.svn-base
tmp
prop-base
props
text-base
Toolbar.bmp
TSP.ico
TSP.vcxproj.filters
GaSchedule
Algorithm
GaSchedule.cfg
GaSchedule.vcxproj.filters
res
GaSchedule.ico
galdocumentation.zip
doxygen.png
Graphic
ab_cp.png
alg_st.png
a_cr.png
c_cp.png
i_cp.png
mp_cr.png
mv_cr.png
r_cp.png
s_cp.png
s_cr.png
galsource.zip
GeneticLibrary
build
release
gcc_bsd
gcc_linux
gcc_macos
gcc_solaris
icc_linux
icc_macos
icc_win
mingw
msvc
scc_solaris
makefiles
gcc_bsd_debug
gcc_bsd_release
gcc_linux_debug
gcc_linux_release
gcc_macos_debug
gcc_macos_release
gcc_solaris_debug
gcc_solaris_release
icc_linux_debug
icc_linux_release
icc_macos_debug
icc_macos_release
scc_solaris_debug
scc_solaris_release
source
vs
/*! \file FPEqual.h
    \brief This file contains declaration and implementation of functions that check floating point numbers for equalty.
*/

/*
 * 
 * website: http://www.coolsoft-sd.com/
 * contact: support@coolsoft-sd.com
 *
 */

/*
 * Genetic Algorithm Library
 * Copyright (C) 2007-2008 Coolsoft Software Development
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef __FPEQUAL_H__
#define __FPEQUAL_H__

#include <stdlib.h>
#include "Platform.h"

/// <summary>This macro interprets <c>float</c> as <c>int</c> for easier bitwies access.</summary>
#define FLOAT_AS_INT(x) ( ( *(int*)&( x ) ) )

/// <summary>This macro interprets <c>double</c> as <c>long long</c> for easier bitwies access.</summary>
#define DOUBLE_AS_LONGLONG(x) ( ( *(long long*)&( x ) ) )

/// <summary>Extracts sign from single precision floating point number.</summary>
#define FLOAT_SIGN 0x80000000UL

/// <summary>Extracts sign from double precision floating point number.</summary>
#define DOUBLE_SIGN 0x8000000000000000ULL

/// <summary>Contains common classes, functions, datatypes and objects used by the library.</summary>
namespace Common
{

	/// <summary>This method compares two single precision floating point numbers for equality with defined level of tolerance.</summary>
	/// <param name="x">the first number</param>
	/// <param name="y">the second number</param>
	/// <param name="ulpTolerance">maximum difference between these two numbers  in ULPs that is tolerated for almost equality. Default value is 10.</param>
	/// <returns>Returns <c>true</c> if the two values are almost equal.</returns>
	inline bool GACALL FloatsEqual(float x,
		float y,
		int ulpTolerance = 10)
	{
		// to avoid equality of small numbers with opposite signs
		if( ( FLOAT_AS_INT( x ) & FLOAT_SIGN ) != ( FLOAT_AS_INT( y ) & FLOAT_SIGN ) )
			return x == y;

		// make x lexicographically ordered as a twos-complement
		if( FLOAT_AS_INT( x ) < 0 )
			FLOAT_AS_INT( x ) = 0x80000000 - FLOAT_AS_INT( x );

		// make y lexicographically ordered as a twos-complement
		if( FLOAT_AS_INT( y ) < 0 )
			FLOAT_AS_INT( y ) = 0x80000000 - FLOAT_AS_INT( y );

		// comparing x and y
		return abs( FLOAT_AS_INT( x ) - FLOAT_AS_INT( y ) ) <= ulpTolerance;
	}

	/// <summary>This method compares two double precision floating point numbers for equality with defined level of tolerance.</summary>
	/// <param name="x">the first number</param>
	/// <param name="y">the second number</param>
	/// <param name="ulpTolerance">maximum difference between these two numbers  in ULPs that is tolerated for almost equality. Default value is 10.</param>
	/// <returns>Returns <c>true</c> if the two values are almost equal.</returns>
	inline bool GACALL DoublesEqual(double x,
		double y,
		int ulpTolerance = 10)
	{
		// to avoid equality of small numbers with opposite signs
		if ( ( DOUBLE_AS_LONGLONG( x ) & DOUBLE_SIGN ) != ( DOUBLE_AS_LONGLONG( y ) & DOUBLE_SIGN ) )
			return x == y;

		// make x lexicographically ordered as a twos-complement
		if( DOUBLE_AS_LONGLONG( x ) < 0 )
			DOUBLE_AS_LONGLONG( x ) = 0x80000000 - DOUBLE_AS_LONGLONG( x );

		// make y lexicographically ordered as a twos-complement
		if( DOUBLE_AS_LONGLONG( y ) < 0 )
			DOUBLE_AS_LONGLONG( y ) = 0x80000000 - DOUBLE_AS_LONGLONG( y );

		// comparing x and y
		long long r = DOUBLE_AS_LONGLONG( x ) - DOUBLE_AS_LONGLONG( y );
		return ( r < 0 ? -r : r ) <= ulpTolerance;
	}

} // Common


#endif // __FPEQUAL_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Mladen Janković
Software Developer
Serbia Serbia
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 7 Apr 2012
Article Copyright 2008 by Mladen Janković
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid