PageRenderTime 16ms CodeModel.GetById 8ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 22#ifndef FIRATIONAL_H
 23#define FIRATIONAL_H
 24
 25/**
 26Helper class to deal with rational numbers. 
 27NB: LONG data type is assumed to be a signed 32-bit number. 
 28*/
 29class FIRational {
 30private:
 31	/// numerator
 32	LONG _numerator;
 33	/// denominator
 34	LONG _denominator;
 35
 36public:
 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	LONG truncate() {
 66		// Return truncated rational
 67		return _denominator ? (LONG) (_numerator / _denominator) : 0;
 68	}
 69
 70	/**@name Implicit conversions */
 71	//@{	
 72	short shortValue() {
 73		return (short)truncate();
 74	}
 75	int intValue() {
 76		return (int)truncate();
 77	}
 78	LONG longValue() {
 79		return (LONG)truncate();
 80	}
 81	float floatValue() {
 82		return _denominator ? ((float)_numerator)/((float)_denominator) : 0;
 83	}
 84	double doubleValue() {
 85		return _denominator ? ((double)_numerator)/((double)_denominator) : 0;
 86	}
 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
 95private:
 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 // FIRATIONAL_H
108