Coverage Report

Created: 2023-12-08 06:53

/src/freeimage-svn/FreeImage/trunk/Source/Metadata/FIRational.h
Line
Count
Source (jump to first uncovered line)
1
// ==========================================================
2
// Helper class for rational numbers
3
//
4
// Design and implementation by
5
// - Hervé Drolon <drolon@infonie.fr>
6
//
7
// This file is part of FreeImage 3
8
//
9
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
10
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
11
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
12
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
13
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
14
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
15
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
16
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
17
// THIS DISCLAIMER.
18
//
19
// Use at your own risk!
20
// ==========================================================
21
22
#ifndef FREEIMAGE_FIRATIONAL_H
23
#define FREEIMAGE_FIRATIONAL_H
24
25
/**
26
Helper class to deal with rational numbers. 
27
NB: LONG data type is assumed to be a signed 32-bit number. 
28
*/
29
class FIRational {
30
private:
31
  /// numerator
32
  LONG _numerator;
33
  /// denominator
34
  LONG _denominator;
35
36
public:
37
  /// Default constructor
38
  FIRational();
39
40
  /// Constructor with longs
41
  FIRational(LONG n, LONG d = 1);
42
43
  /// Constructor with FITAG
44
  FIRational(const FITAG *tag);
45
46
  /// Constructor with a float
47
  FIRational(float value);
48
49
  /// Copy constructor
50
  FIRational (const FIRational& r);
51
52
  /// Destructor
53
  ~FIRational();
54
55
  /// Assignement operator
56
  FIRational& operator=(FIRational& r);
57
58
  /// Get the numerator
59
  LONG getNumerator();
60
61
  /// Get the denominator
62
  LONG getDenominator();
63
64
  /// Converts rational value by truncating towards zero
65
0
  LONG truncate() {
66
    // Return truncated rational
67
0
    return _denominator ? (LONG) (_numerator / _denominator) : 0;
68
0
  }
69
70
  /**@name Implicit conversions */
71
  //@{  
72
0
  short shortValue() {
73
0
    return (short)truncate();
74
0
  }
75
0
  int intValue() {
76
0
    return (int)truncate();
77
0
  }
78
0
  LONG longValue() {
79
0
    return (LONG)truncate();
80
0
  }
81
0
  float floatValue() {
82
0
    return _denominator ? ((float)_numerator)/((float)_denominator) : 0;
83
0
  }
84
0
  double doubleValue() {
85
0
    return _denominator ? ((double)_numerator)/((double)_denominator) : 0;
86
0
  }
87
  //@}
88
89
  /// Checks if this rational number is an integer, either positive or negative
90
  BOOL isInteger();
91
92
  /// Convert as "numerator/denominator"
93
  std::string toString();
94
95
private:
96
  /// Initialize and normalize a rational number
97
  void initialize(LONG n, LONG d);
98
99
  /// Calculate GCD
100
  LONG gcd(LONG a, LONG b);
101
  
102
  /// Normalize numerator / denominator 
103
  void normalize();
104
105
};
106
107
#endif // FREEIMAGE_FIRATIONAL_H
108