My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) LibG.org. 2008. All rights reserved. //
// //
// Licensed under GPL, you may use this file except for commercial purpose. //
// //
// Permission to use, copy, modify, and distribute this software in object //
// code form for any purpose and without fee is hereby granted, provided //
// that the above copyright notice appears in all copies and that both that //
// copyright notice and the limited warranty and restricted rights notice //
// below appear in all supporting documentation. //
// //
//////////////////////////////////////////////////////////////////////////////

#ifndef HEADER_GIFLOAT_H
#define HEADER_GIFLOAT_H

#define EPSILON_FLOAT 1e-6f // loose tolerance ( 1.1929093e-7 )
#define EPSILON_DOUBLE 1e-12 // loose tolerance ( 2.220446049250313e-16 )

template<typename T1, typename T2>
class giReal
{
private:
// value
T1 mValue;

public:
// constructors and destructor
inline giReal() { mValue = 0.0f; }
inline giReal( const T1& v ) { mValue = v; }
inline giReal( const giReal& v ) { mValue = v.mValue; }
inline ~giReal() {}

// implicit conversion
inline operator T1() const { return mValue; }
inline operator T2() const { return (T2)mValue; }
inline operator int() const { return (int)( mValue + (T1)0.5 ); }
inline operator long() const { return (long)( mValue + (T1)0.5 ); }

// assignment operators
inline giReal& operator= ( const T1& v ) { mValue = v; return *this; }
inline giReal& operator= ( const giReal& v ) { mValue = v.mValue; return *this; }
inline T1& operator+= ( const T1& v ) { mValue += v; return mValue; }
inline T1& operator-= ( const T1& v ) { mValue -= v; return mValue; }
inline T1& operator*= ( const T1& v ) { mValue *= v; return mValue; }
inline T1& operator/= ( const T1& v ) { mValue /= v; return mValue; }

// relational and equality / control flow
inline bool operator== ( const T1& v ) const { register T1 x = mValue - v; return -kEpsilon < x && x < kEpsilon; }
inline bool operator!= ( const T1& v ) const { register T1 x = mValue - v; return x >= kEpsilon || x <= -kEpsilon; }
inline bool operator> ( const T1& v ) const { return mValue > v && *this != v; }
inline bool operator>= ( const T1& v ) const { return mValue > v || *this == v; }
inline bool operator< ( const T1& v ) const { return mValue < v && *this != v; }
inline bool operator<= ( const T1& v ) const { return mValue < v || *this == v; }

// arithmetic operators
inline T1 operator+ ( const T1& v ) const { return mValue + v; }
inline T1 operator- ( const T1& v ) const { return mValue - v; }
inline T1 operator* ( const T1& v ) const { return mValue * v; }
inline T1 operator/ ( const T1& v ) const { return mValue / v; }

// unary operators
inline giReal operator- () const { return -mValue; }
inline giReal operator+ () const { return mValue; }

// prefix
inline giReal& operator++ () { ++mValue; return *this; }
inline giReal& operator-- () { --mValue; return *this; }

// postfix
inline giReal operator++ (int) { giReal t(*this); ++mValue; return t; }
inline giReal operator-- (int) { giReal t(*this); --mValue; return t; }

// tolerance
static const T1 kEpsilon;
};

typedef giReal<float, double> giFloat;
const float giFloat::kEpsilon = EPSILON_FLOAT;

typedef giReal<double, float> giDouble;
const double giDouble::kEpsilon = EPSILON_DOUBLE;

#endif // HEADER_GIFLOAT_H

Change log

r9 by hao.hzhang on Sep 11, 2008   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r8 by hao.hzhang on Sep 10, 2008   Diff
giPoint3, giVector3, wait for unit
test
r7 by hao.hzhang on Sep 9, 2008   Diff
Adjusted giFloat class for better
performance and testing script
r6 by hao.hzhang on Sep 9, 2008   Diff
giFloat and giDouble, and unit test
All revisions of this file

File info

Size: 4012 bytes, 85 lines
Powered by Google Project Hosting