/src/FreeImage/Source/Metadata/FIRational.h

https://bitbucket.org/cabalistic/ogredeps/ · C Header · 108 lines · 42 code · 21 blank · 45 comment · 0 complexity · 41cffca2cbe245b8ffdd6006a8d1899b MD5 · raw file

  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. #ifndef FIRATIONAL_H
  22. #define FIRATIONAL_H
  23. /**
  24. Helper class to deal with rational numbers.
  25. NB: LONG data type is assumed to be a signed 32-bit number.
  26. */
  27. class FIRational {
  28. private:
  29. /// numerator
  30. LONG _numerator;
  31. /// denominator
  32. LONG _denominator;
  33. public:
  34. /// Default constructor
  35. FIRational();
  36. /// Constructor with longs
  37. FIRational(LONG n, LONG d = 1);
  38. /// Constructor with FITAG
  39. FIRational(const FITAG *tag);
  40. /// Constructor with a float
  41. FIRational(float value);
  42. /// Copy constructor
  43. FIRational (const FIRational& r);
  44. /// Destructor
  45. ~FIRational();
  46. /// Assignement operator
  47. FIRational& operator=(FIRational& r);
  48. /// Get the numerator
  49. LONG getNumerator();
  50. /// Get the denominator
  51. LONG getDenominator();
  52. /// Converts rational value by truncating towards zero
  53. LONG truncate() {
  54. // Return truncated rational
  55. return _denominator ? (LONG) (_numerator / _denominator) : 0;
  56. }
  57. /**@name Implicit conversions */
  58. //@{
  59. short shortValue() {
  60. return (short)truncate();
  61. }
  62. int intValue() {
  63. return (int)truncate();
  64. }
  65. LONG longValue() {
  66. return (LONG)truncate();
  67. }
  68. float floatValue() {
  69. return _denominator ? ((float)_numerator)/((float)_denominator) : 0;
  70. }
  71. double doubleValue() {
  72. return _denominator ? ((double)_numerator)/((double)_denominator) : 0;
  73. }
  74. //@}
  75. /// Checks if this rational number is an integer, either positive or negative
  76. BOOL isInteger();
  77. /// Convert as "numerator/denominator"
  78. std::string toString();
  79. private:
  80. /// Initialize and normalize a rational number
  81. void initialize(LONG n, LONG d);
  82. /// Calculate GCD
  83. LONG gcd(LONG a, LONG b);
  84. /// Normalize numerator / denominator
  85. void normalize();
  86. };
  87. #endif // FIRATIONAL_H