1 /*
2 Copyright (C) 2005-2007 Feeling Software Inc.
3 Portions of the code are:
4 Copyright (C) 2005-2007 Sony Computer Entertainment America
5
6 MIT License: http://www.opensource.org/licenses/mit-license.php
7 */
8
9 /**
10 @file FMFloat.h
11 The file containing functions and constants for floating point values.
12 */
13
14 #ifndef _FM_FLOAT_H_
15 #define _FM_FLOAT_H_
16
17 #if !defined(_INC_FLOAT) && (defined (WIN32) || defined (LINUX) || defined(__APPLE__))
18 #include <float.h>
19 #endif // _INC_FLOAT, WIN32 and LINUX
20
21 /** The default tolerance for double-sized floating-point comparison functions. */
22 #define DBL_TOLERANCE 0.0001
23 /** The default tolerance for single-sized floating-point comparison functions. */
24 #define FLT_TOLERANCE 0.0001f
25
26 /** Returns whether two floating-point values are equivalent within a given tolerance.
27 @param f1 A first floating-point value.
28 @param f2 A second floating-point value. */
IsEquivalent(float f1,float f2)29 inline bool IsEquivalent(float f1, float f2) { return f1 - f2 < FLT_TOLERANCE && f2 - f1 < FLT_TOLERANCE; }
30
31 /** Returns whether two floating-point values are equivalent within a given tolerance.
32 @param f1 A first floating-point value.
33 @param f2 A second floating-point value.
34 @param tolerance The tolerance in which to accept the two floating-point values as equivalent. */
IsEquivalent(float f1,float f2,float tolerance)35 inline bool IsEquivalent(float f1, float f2, float tolerance) { return f1 - f2 < tolerance && f2 - f1 < tolerance; }
36
37 /** Returns whether two double-sized floating-point values are equivalent.
38 @param f1 A first double-sized floating-point value.
39 @param f2 A second double-sized floating-point value. */
IsEquivalent(double f1,double f2)40 inline bool IsEquivalent(double f1, double f2) { return f1 - f2 < DBL_TOLERANCE && f2 - f1 < DBL_TOLERANCE; }
41
42 /** Returns whether two double-sized floating-point values are equivalent within a given tolerance.
43 @param f1 A first double-sized floating-point value.
44 @param f2 A second double-sized floating-point value.
45 @param tolerance The tolerance in which to accept the two double-sized floating-point values as equivalent. */
IsEquivalent(double f1,double f2,double tolerance)46 inline bool IsEquivalent(double f1, double f2, double tolerance) { return f1 - f2 < tolerance && f2 - f1 < tolerance; }
47
48 #ifndef FLT_MAX
49 /** The maximum value that can be expressed using a 32-bit floating point value.
50 This macro is pre-defined in the Windows API but is missing on MacOSX and other platforms. */
51 #define FLT_MAX __FLT_MAX__
52 #endif
53
54 #endif // _FM_FLOAT_H_
55
56